Email when sensor still ON after some time

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
xurg
Posts: 52
Joined: 17 Aug 2020 22:38

Email when sensor still ON after some time

Post by xurg »

Running 3.1.0, email notification seems to be working. But here's my problem: I want to realize a seemingly simple task, and that's watching a door sensor and when it's been open for longer than X seconds, I want to send an email notification. A simple If-Delayed-Then doesn't work, as it will always take the action even if the source sensor has gone back to OFF. I then tried a attaching a dummy sensor to If-Delayed-Then to be able to take it back on Else. But when it triggers the (new notification system) email, the mail is sent repeatedly. It seems my sensors are stuck in ON position and so the engine repeatedly triggers the Email action. How best stop such cycle from happening while the root cause (sensor) is still active?

So I think I'm approaching this wrong, but in what way? Surely a simple task like this must be solvable easy without needing 3 rules and a dummy device!? I'm absolutely willing to read and I tried, but did't find help in the ZWayManual.pdf (which also does not seem to cover the new Automation Tasks screens, btw.)

Any help or pointers greatly appreciated!
Raspberry Pi 3 Model B Rev 1.2
Raspbian GNU/Linux 10 (buster)
RaZberry ZW0500 1024/2 SDK: 6.82.01 API: 05.39
Z-Way version v4.1.0
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Email when sensor still ON after some time

Post by PoltoS »

I would use EasyScripting with the following script:

Code: Select all

## ZWayVDev_zway-XX-YY-CC // (select the sensor from Events menu)

if (vdev("ZWayVDev_zway-XX-YY-CC").value() === "on") {
 setTimer("myTimer", function() {
  vdev("NotificationSend_xxxxx").on(); // This scene should be created using Notification Send app with corresponding notification channel selected
 }, 10)
} else {
 stopTimer("myTimer);
}
xurg
Posts: 52
Joined: 17 Aug 2020 22:38

Re: Email when sensor still ON after some time

Post by xurg »

Thanks for the response. I haven't looked at EasyScripting yet - I was somehow hoping to solve this with more traditional means. :-) I understand your E/S code, but out of curiosity, what would be the scene/conditional approach to this? More precisely: what is the best practise to cancel a timer? I always thought stuff like this is the core of all these smarthome things.
Raspberry Pi 3 Model B Rev 1.2
Raspbian GNU/Linux 10 (buster)
RaZberry ZW0500 1024/2 SDK: 6.82.01 API: 05.39
Z-Way version v4.1.0
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Email when sensor still ON after some time

Post by PoltoS »

Exactly canceling the timer is the corner stone that pushes to write scripts or make own app that implements this logic
xurg
Posts: 52
Joined: 17 Aug 2020 22:38

Re: Email when sensor still ON after some time

Post by xurg »

Ok, fair enough.

FWIW, here is my EasyScript based on your suggestion. I fixed the syntax and changed the looks a bit but it's basically the same. I tied a hardware sensor to a dummy device and monitor that so I can more easily test the setup.

Note: it is critical to place the timeout id in "self" so it lives long enough to reach the clearTimeout called in the release/off trigger event.

Thanks again. I was never a friend of all that scene and IFTTT stuff so regular coding like this really feels like home again.

Any chance to get the UI editor working with Firefox?

Code: Select all

### DummyDevice_11
if (vdev("DummyDevice_11").value() == "on") {
    self.myTimer = setTimeout(fn, 15*60*1000);
    vdev("ZWayVDev_zway_2-0-37").on();
}
else {
    clearTimeout(self.myTimer);
    vdev("ZWayVDev_zway_2-0-37").off();
}

function fn() {
        vdev("NotificationSend_9").on();
}
Raspberry Pi 3 Model B Rev 1.2
Raspbian GNU/Linux 10 (buster)
RaZberry ZW0500 1024/2 SDK: 6.82.01 API: 05.39
Z-Way version v4.1.0
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Email when sensor still ON after some time

Post by PoltoS »

We have not invested much in checking FF - too much headache and we like Chrome... Actually the code is open, you can dig into ;)

https://github.com/Z-Wave-Me/home-autom ... /htdocs/js
The postRender.js is minimized (beacuse this is the only way this crazy part accepts JS code - in one single string only) and the code is in the other file with compile script. Your pull requests are welcome! If not, we have this in the huge TODO, but with many more prior tasks
xurg
Posts: 52
Joined: 17 Aug 2020 22:38

Re: Email when sensor still ON after some time

Post by xurg »

I used to like Chrome too, but, well, Google.

Anyway - even with storing the timeout ID in self, I still get email notifications so the cancellation does not seem to always work. I had done my testing with short events, but in the real world the on/off events are apart by a couple of minutes, usually. What is the lifetime of the object? Do I need to store the id anywhere else?
Raspberry Pi 3 Model B Rev 1.2
Raspbian GNU/Linux 10 (buster)
RaZberry ZW0500 1024/2 SDK: 6.82.01 API: 05.39
Z-Way version v4.1.0
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Email when sensor still ON after some time

Post by PoltoS »

Sorry, have not noticed - you use setTimeut and clearTimeout there are no place to store your variable - this is why it does not work.

Please check again my example - there is a reason I used setTimer/stopTimer - those are wrappers on top of setInterval/clearInterval that references the timer by name and saves it in the script context.
xurg
Posts: 52
Joined: 17 Aug 2020 22:38

Re: Email when sensor still ON after some time

Post by xurg »

No worries. During my initial experiments I may just have been fooled by JS's === operator which I didn't know. That's why I tried messing with setTimeout and self. As I said, JS noob. For completeness, this seems to work as intented now:

Code: Select all

### DummyDevice_11

var secondsToWarningMail = 15*60;

if (vdev("DummyDevice_11").value() === "on") {
    setTimer("myTimer", fn, secondsToWarningMail);
    vdev("ZWayVDev_zway_2-0-37").on();
}
else {
    stopTimer("myTimer");
    vdev("ZWayVDev_zway_2-0-37").off();
}

function fn() {
        vdev("NotificationSend_9").on();
}
It would be nice to have a bit more documentation on EasyScripting. I understand it's hard to keep up development and documentation, but ZWayManual.pdf really needs a few new sections.
Raspberry Pi 3 Model B Rev 1.2
Raspbian GNU/Linux 10 (buster)
RaZberry ZW0500 1024/2 SDK: 6.82.01 API: 05.39
Z-Way version v4.1.0
Post Reply