Report any open doors/windows

I am trying to create a flow that will tell me if there are any open door or windows when I activate my alarm, and I want to know exactly which one(s) are still open. I have created a flow for only three contact sensors, but this wil rapidly become a mess if I want to add 5 more sensors. Is there a better way of doing this?

Hi Marcel,

I use this Homeyscript script for that. It sends a timeline report, so you just have to call this script in a flow:
(I use the same script for rain/snow/storm alerts)

// 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: ${device.name} (Zone: ${device.zoneName}), [device.class: ${device.class}, device.capabilities: ${device.capabilities}]`); 
    
      if(device.name != ""){
      windowOpen = true;
      if(windowText=="")
      //windowText = device.name + "\t(" + device.zoneName + ")";
      windowText = device.name + "\t(" + device.zoneName + ")";
      
      else
      windowText = windowText + "\n" + device.name + "\t(" + device.zoneName + ")";
      }
    }
  }
});

if(windowOpen == true)
windowText = "\nDe volgende ramen of deuren staan nog open:\n" + windowText + "\n";
else
// comment this out if you don't want a msg when all's fine:
windowText = "Alle deuren en ramen zijn dicht";

Homey.flow.runFlowCardAction({
        uri: 'homey:manager:notifications',
        id: 'create_notification',
        args: {
          text: windowText
        },
      });

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

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

return (true);

// Initial script by RonnyW.

( Credits to @RonnyW !)

4 Likes

Zojuist de flow aangemaakt. Werkt perfect. Bedankt voor de tip en het script.

1 Like

Nice script! Although on the new Homey it doesn’t seem to recognize Device.Zonename. Not sure how to retrieve the correct zone name on HP2023?

Ps; I can get the zone as a guid by using device.zone but that’s not very helpful

1 Like

make sure to use device.zoneName
It’s case-sensitive

@Homeyscript tab: When you type device.z in the line below, like the screenshot, what’s in the dropdown which appears? You can enter every one of them in this line, and look which one returns the correct zone name:

device.zoneName has been removed from a device, it’s back to just the zoneId. So you’ll need to get all zones, and match the zoneId to retrieve the zonename.

Like this in an app: com.uc.heimdall/app.js at d1c329d6ed5f79907a66c52bc318ca0a2b013bda · daneedk/com.uc.heimdall · GitHub

1 Like

Thanks Danee!

Perfect, this makes sense. Thanks Danee :slight_smile: !

Thank you so much for your quick reply. I am not familiar with coding so it will take me some time to figure this out and understand how this all works. But I was able to get this to work with your instructions at hand. Awsome!

  1. Install homeyscript
  2. Copy/paste the code into a new script.
  3. Use a tag in the “then” column to grab output and do with it as you please

No need to understand the code in this case :slight_smile:

Unless you want to get rid of the “zoneName” part that’s not working anyway. Then you’d need to comment out parts on line 24 and 27;

  windowText = device.name // + "\t(" + device.zoneName + ")";
  
  else
  windowText = windowText + "\n" + device.name // + "\t(" + device.zoneName + ")";

(STILL) Working fine on Pro 201x models :upside_down_face:

Hmmm, shouldn’t that be like:

//windowText = device.name + "\t(" + device.zoneName + ")";
windowText = device.name;  
  else
  //windowText = windowText + "\n" + device.name + "\t(" + device.zoneName + ")";
  windowText = windowText + "\n" + device.name;

[/quote]

Just wait till you get firmware 10.x.x :sunglasses:

1 Like

Ah hehe, didn’t count on that to happen :crazy_face:

patato potata :slight_smile:

Well no, with your way, the ending " ;" sign gets lost when you comment out halfway the line :wink:
(regardless if it is truly needed or not :crazy_face: )

Nerd :kissing_heart:!

ASI fixes it so; “potato, potata” still stands firm :slight_smile: