Homeyscript über Öffnungszustand von Fenster und Türen

Hallo miteinander,

ich habe folgendes Script in diesen Foren gefunden, welches ich einem Flow verwende:

// Check if certain doors or windows is open?

let devices = await Homey.devices.getDevices();
let windowText = "";
let currentText = "";
let logText = "";
let windowOpen = false;

_.some(devices, device => 
{
if ( device.class == 'sensor' ) 
// device.class is found at https://tools.developer.homey.app/tools/devices
  {
  if( device.capabilitiesObj &&
  device.capabilitiesObj.alarm_contact &&
  device.capabilitiesObj.alarm_contact.value )
    {
    currentText = "";
    // to compare and see if there is no typing error
    //console.log(`- Open door/window script check: \nName: ${device.name} \nZone: ${device.zone} \n(ZoneName: ${device.zoneName}) \n[device.class: ${device.class} \ndevice.capabilities: ${device.capabilities}]`); 

      if(device.name != ""){
      windowOpen = true;
      if(windowText=="")
      windowText = device.name + "\t(Zone: " + device.zoneName + ")";
      
      else
      windowText = windowText + "\n" + device.name + "\t(Zone: " + device.zoneName + ")";
      }
    }
  }
});

if(windowOpen == true)
windowText = "\nDiese Türen oder Fenster sind offen:\n" + windowText + "\n";
else
// comment this out if you don't want a msg when all's fine:
windowText = "Alle Türen und Fenster sind geschlossen.";

Homey.flow.runFlowCardAction({
        uri: 'homey:manager:notifications',
        id: 'create_notification',
        args: {
          text: windowText + '\n[Bernd Unterwegs - Tür/Fenster Offen Meldung]'
        },
      });

//just to check
console.log("\nOutput:", windowText);

// Create / update HomeyScript variabele
await tag("door_windowText", windowText);

return (true);

Als Ausgabe gibt er bei einem Test folgendes aus:

Output:
Diese Türen oder Fenster sind offen:
Tür Garage (Zone: undefined)

———————————————————
:white_check_mark: Script Success
:leftwards_arrow_with_hook: Returned: true

Die Meldung ist korrekt, die “Tür Garage” ist offen, so wurde die Tür von mir benannt.

Was muss ich im Script aber verändern, dass die Meldung “(Zone: undefined)” nicht erscheint, sonder die Zone mit angegeben wird?

Gibt es eigentlich irgendwo Hilfe zu Verwendung und Programmierung in Homeyscript?

Vielen Dank

Erstellen Sie eine Garage-Zone und stecken Sie das Garagentor hinein.
Oder löschen Sie im Code alles, was mit „Zone“ zu tun hat.

Wenn ich mich richtig erinnere, hat Athom die API geändert. Man kann nun nicht mehr direkt den Zonenname aus dem Gerät lesen.

Wenn der Text stört, dann erstmal diese Zeile

      windowText = windowText + "\n" + device.name + "\t(Zone: " + device.zoneName + ")";

ersetzen durch diese:

      windowText = windowText + "\n" + device.name;

Wenn du die Zone benötigst, dann muss die nochmal mit einem separaten API-Aufruf über die ID gelesen werden. Müsste ich mir später man anschauen…

Bitteschön :sunglasses:

// Check if certain doors or windows is open?

let devices = await Homey.devices.getDevices();
let zones = await Homey.zones.getZones();

let windowText = "";
let currentText = "";
let logText = "";
let windowOpen = false;

_.some(devices, device => 
{
if ( device.class == 'sensor' ) 
// device.class is found at https://tools.developer.homey.app/tools/devices
  {
  if( device.capabilitiesObj &&
  device.capabilitiesObj.alarm_contact &&
  device.capabilitiesObj.alarm_contact.value )
    {
    currentText = "";
    // to compare and see if there is no typing error
    //console.log(`- Open door/window script check: \nName: ${device.name} \nZone: ${device.zone} \n(ZoneName: ${device.zoneName}) \n[device.class: ${device.class} \ndevice.capabilities: ${device.capabilities}]`); 

      if(device.name != ""){
        let zone = zones[device.zone];
        windowOpen = true;
        if(windowText=="")
          windowText = device.name + "\t(Zone: " + zone.name + ")";        
        else
          windowText = windowText + "\n" + device.name + "\t(Zone: " + zone.name + ")";
      }
    }
  }
});

if(windowOpen == true)
windowText = "\nDiese Türen oder Fenster sind offen:\n" + windowText + "\n";
else
// comment this out if you don't want a msg when all's fine:
windowText = "Alle Türen und Fenster sind geschlossen.";

Homey.flow.runFlowCardAction({
        uri: 'homey:manager:notifications',
        id: 'create_notification',
        args: {
          text: windowText + '\n[Bernd Unterwegs - Tür/Fenster Offen Meldung]'
        },
      });

//just to check
console.log("\nOutput:", windowText);

// Create / update HomeyScript variabele
await tag("door_windowText", windowText);

return (true);
3 Likes

Vielen Dank Ronny!
Das ist die Lösung. Jetzt zeigt er die offene Garagentür in der Zone Garage richtig an.

Diesen Teil habe noch gelöscht (der wurde von mir nicht gebraucht):

Homey.flow.runFlowCardAction({
        uri: 'homey:manager:notifications',
        id: 'create_notification',
        args: {
          text: windowText + '\n[Bernd Unterwegs - Tür/Fenster Offen Meldung]'
        },
      });

Falls es jemanden interessiert, ist hier ein möglicher Flow dazu (das Script wurde als kontakte-pruefung gespeichert):

Bildschirmfoto 2024

1 Like