Action (or in-action) based on device state within past x minutes

Looking for a little help in how to construct this flow.

I have a flow that locks the front door after sunset. I’d like to make it a little smarter by adding in a check that will see if the door has been open in the last 5 minutes and halt the flow. I’m lazy and don’t like having to unlock the door again if I run out to check mail :wink:

I’m thinking that I may need to use the get history node for this but I can’t figure hour how to set it up to check history for five minutes in the past. Or maybe I’m totally in the wrong track.

Anyone done something similar?

Just another thing I have on the TODO list. Here’s example on how to pass in a startdate to the get history node then stop the if any of the states are a set value.

[{"id":"d0c4dae1.5bc328","type":"function","z":"6edf92e.d52b66c","name":"","func":"let fiveMinsAgo = new Date(new Date() - 300000).toISOString();\nmsg.startdate = fiveMinsAgo; \nnode.status({text: fiveMinsAgo});\nreturn msg;","outputs":1,"noerr":0,"x":274,"y":400,"wires":[["ad61580c.52e068"]]},{"id":"edd691bf.6c31b","type":"debug","z":"6edf92e.d52b66c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":790,"y":400,"wires":[]},{"id":"46b7bcf8.051a74","type":"inject","z":"6edf92e.d52b66c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":124,"y":400,"wires":[["d0c4dae1.5bc328"]]},{"id":"ad61580c.52e068","type":"api-get-history","z":"6edf92e.d52b66c","name":"","server":"351c96ee.9dc3aa","startdate":"","enddate":"","entityid":"light.night_light","entityidtype":"is","x":470,"y":400,"wires":[["62e13d13.0c7714"]]},{"id":"62e13d13.0c7714","type":"function","z":"6edf92e.d52b66c","name":"","func":"if(msg.payload[0] && msg.payload[0].filter(item => item.state == \"on\").length) {\n    return null;\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":642,"y":400,"wires":[["edd691bf.6c31b"]]}]
4 Likes

Hey circling back on this. Thanks for the example.

Something I noticed about the “get history” function is that regardless of whether I specify and entity_id or not it always returns an array containing the history of ALL my entities for the last five minutes.

Doesn’t that defeat the purpose of specifying an entity_id that were interested in in the first place? Or am I doing something wrong?

And if that’s the normal behavior for it to return an array then how to I match it to the entity_id i’m interested in with the second last function? I can’t get it to work as is. I would expect that the “get history” should return just the state of the entity I’m interested in. The second last function would pass a message on or not depending on the state of the entity I’ve asked to check.

But for me it always passes on the msg, which is in this case is the history of all the entities it returned from the “get history” call.

Edit: I just realized that does seem to be working as expected. I’m still just curious why the “get history” call is returning and array for all of my entities when I specify one. If I change the criteria for entity_id to “includes” instead of “is” it actually only returns the state of the entity I’m interested in.

Thanks for the help!

Do you run hass.io? There’s an issue with the hass.io proxy and the get-history node. https://github.com/home-assistant/hassio/issues/800

Here’s a solution I provided that might work in the short term. https://github.com/zachowj/node-red-contrib-home-assistant-websocket/issues/33#issuecomment-436778039

1 Like

thought I’d bring this thread back to life with a follow up question…I too am looking to suppress an automation if an entity has been triggered within the last 30 seconds. I am however having issues with the Get History node response within Node Red…

I intend on using the last_changed datetime attribute included in the Get History response to determine when the entity was last triggered. The last item’s last_changed value in the Get History array that is returned appears to correspond to the relative date range configured in the Get History Node request and not when the Entity’s state was last changed.

vs image
*The Get History last_changed datetime appears to be UTC while my machine and the Home Assistant frontend are representing UTC +2 for my timezone.

Below is the Node Red implementation, please let me know if I have implemented anything incorrectly or if there is another way to implement such logic within node red. thanks

[{"id":"5850c808.b65c88","type":"server-state-changed","z":"6b578c04.3d58e4","name":"Driveway Beam Movement","server":"18cd850f.0ce76b","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.drivewaybeam","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":510,"y":280,"wires":[["56965379.05f0ac"],[]]},{"id":"56965379.05f0ac","type":"api-get-history","z":"6b578c04.3d58e4","name":"","server":"18cd850f.0ce76b","startdate":"now - 30","enddate":"now ","entityid":"binary_sensor.drivewaybeam","entityidtype":"is","useRelativeTime":true,"relativeTime":"1 min","flatten":true,"output_type":"array","output_location_type":"msg","output_location":"payload","x":710,"y":320,"wires":[["794742ef.8a768c","22710964.3eefc6"]]},{"id":"794742ef.8a768c","type":"function","z":"6b578c04.3d58e4","name":"Check Time Difference","func":"var now = new Date();\nvar history_array = msg.payload;\n//var history_array = msg.payload.reverse();\n//msg.payload.LastTimeTriggered= history_array[0].last_updated;\n//flow.set('LastTimeTriggered',history_array[0].last_updated);\n//flow.set('LastSinceLastTriggered',(now-Date.parse(history_array[0].last_updated)));\nmsg.payload = now-Date.parse(history_array[history_array.length-1].last_updated)\nreturn msg ;\n","outputs":1,"noerr":0,"x":880,"y":320,"wires":[["b63fab34.271af8","fc174bf7.341dc8"]]},{"id":"b63fab34.271af8","type":"switch","z":"6b578c04.3d58e4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"3000","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1070,"y":360,"wires":[[]]},{"id":"25fe5bd5.e7acd4","type":"inject","z":"6b578c04.3d58e4","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":540,"y":360,"wires":[["56965379.05f0ac"]]},{"id":"22710964.3eefc6","type":"debug","z":"6b578c04.3d58e4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":830,"y":380,"wires":[]},{"id":"fc174bf7.341dc8","type":"debug","z":"6b578c04.3d58e4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":420,"wires":[]},{"id":"18cd850f.0ce76b","type":"server","z":"","name":"Home Assistant"}] ```