Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait Until - Ability to act like HA automations for statement #206

Closed
niemyjski opened this issue Feb 14, 2020 · 2 comments
Closed

Wait Until - Ability to act like HA automations for statement #206

niemyjski opened this issue Feb 14, 2020 · 2 comments

Comments

@niemyjski
Copy link

I noticed something really hard to do in node red was mimic the behavior of for time limit in HA automations.

For example, I'd like to say wait until the entity state has been on for 4 hours (timeout), but I want it calculated based on the last changed property. This means when node red reboots, I can do a wait for and resume or immediately execute based on if the condition has passed (switch has already been on for 4 hours so don't even wait).

I think we would need to add some date comparison operators and maybe add a conditions section to the wait until node.

@zachowj
Copy link
Owner

zachowj commented Feb 15, 2020

The wait-until and trigger node can handle pretty much everything that the for condition does HA. I do see how adding a JSONata expression to the timeout field would allow users to easily create dynamic timers from within the wait-until node.

Here's a simple solution to your use case done with a function node but can be easily done using one of the many date manipulation nodes and a math node.

image

const lastChanged = Date.now() - new Date(msg.payload.last_changed).getTime();
const timeLeft = Math.min(1, lastChanged - 1.44e+7);
msg.payload = {
    timeout: Math.abs(timeLeft) / 1000,
    timeoutUnits: "seconds"
}
return msg;
[{"id":"b81b4cd4.523e","type":"inject","z":"311deaea.efb306","name":"4.5 hours ago","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":294,"y":352,"wires":[["3146637f.2b22fc"]]},{"id":"cd95dc87.9e9a6","type":"function","z":"311deaea.efb306","name":"calc remaining timeout","func":"const lastChanged = Date.now() - new Date(msg.payload.last_changed).getTime();\nconst timeLeft = Math.min(1, lastChanged - 1.44e+7);\nmsg.payload = {\n    timeout: Math.abs(timeLeft) / 1000,\n    timeoutUnits: \"seconds\"\n}\nreturn msg;","outputs":1,"noerr":0,"x":676,"y":352,"wires":[["b337f580.16d9f8"]]},{"id":"43fdcc6d.a2cfc4","type":"inject","z":"311deaea.efb306","name":"3.5 hours ago","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":294,"y":384,"wires":[["3146637f.2b22fc"]]},{"id":"3146637f.2b22fc","type":"function","z":"311deaea.efb306","name":"setup demo","func":"const duration = msg.payload ? 1.62e+7 : 1.26e+7;\nconst date = new Date(Date.now() - duration);\n\nmsg.payload = {\n    \"entity_id\":\"sun.sun\",\n    \"state\":\"below_horizon\",\n    \"last_changed\": date.toISOString(),\n};\n\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":352,"wires":[["cd95dc87.9e9a6"]]},{"id":"3d94989f.208918","type":"debug","z":"311deaea.efb306","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1014,"y":352,"wires":[]},{"id":"b337f580.16d9f8","type":"ha-wait-until","z":"311deaea.efb306","name":"","outputs":2,"entityId":"sun.sun","property":"placeholder_property","comparator":"is","value":"","valueType":"str","timeout":"4","timeoutUnits":"hours","entityLocation":"","entityLocationType":"none","checkCurrentState":false,"blockInputOverrides":false,"x":860,"y":352,"wires":[[],["3d94989f.208918"]]}]

@niemyjski
Copy link
Author

niemyjski commented Feb 25, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants