Automation to check last time switch was turned on. How to use 'get history' output?

I never have used get history node before. I want node red to, every 24 hours, check the last time a switch was turned on. If its been more than 48hrs since it was turned on, send an alert.

I just dont know how to use the info the ‘get history’ node spits out. How would I check to see if the last timestamp was more than 48 hours old?

[{"id":"c6dd4022.7c84","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"c64c8ed1.6c8a6","type":"api-get-history","z":"c6dd4022.7c84","name":"","server":"ab3e2c53.e3491","version":0,"startdate":"","enddate":"","entityid":"light.office","entityidtype":"is","useRelativeTime":false,"relativeTime":"","flatten":true,"output_type":"array","output_location_type":"msg","output_location":"payload","x":1060,"y":261,"wires":[["4a6cfb18.98f904"]]},{"id":"7d1a60f.78ff5a","type":"inject","z":"c6dd4022.7c84","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":830,"y":261,"wires":[["c64c8ed1.6c8a6"]]},{"id":"4a6cfb18.98f904","type":"debug","z":"c6dd4022.7c84","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1279,"y":261,"wires":[]},{"id":"ab3e2c53.e3491","type":"server","name":"AvilaSmartHomeRpi","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Set the History node to 48 hours (or 2 days) relative time. Then add a switch that checks if the payload is empty. If it is empty then nothing has been recorded in the past 48 hours.

[{"id":"9eb75b0d69eb27ba","type":"inject","z":"8f7c2fc4aea58e7e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":160,"wires":[["f5b535b66f400b06"]]},{"id":"5e483fb0140c6bf0","type":"debug","z":"8f7c2fc4aea58e7e","name":"This is triggered when nothing happend in the last 48hrs","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":140,"wires":[]},{"id":"f5b535b66f400b06","type":"api-get-history","z":"8f7c2fc4aea58e7e","name":"","server":"89e3a442.daf8c8","startdate":"","enddate":"","entityid":"light.office_light","entityidtype":"is","useRelativeTime":true,"relativeTime":"2d","flatten":true,"output_type":"array","output_location_type":"msg","output_location":"payload","x":350,"y":160,"wires":[["f2d48894fbd5edf5"]]},{"id":"f2d48894fbd5edf5","type":"switch","z":"8f7c2fc4aea58e7e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"empty"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":490,"y":160,"wires":[["5e483fb0140c6bf0"],["53fac2ed7e435d62"]]},{"id":"53fac2ed7e435d62","type":"debug","z":"8f7c2fc4aea58e7e","name":"This is triggered when something happend in the last 48hrs","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":840,"y":200,"wires":[]},{"id":"89e3a442.daf8c8","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

following @declankenny 's suggestion, I would add a current state node right before the get history node with the condition that the switch is off. Otherwise, if the switch was on for more than 48 hours, it’d also alert, which I believe is something you don’t want to happen.

1 Like

Doh! I should have though of that one! Well done.

2 Likes

In fact, I tried declankenny’s example and it’s not working for me, I might doing something wrong though.

The way I’d do it.

[{"id":"819eea0e.0c9728","type":"stoptimer-varidelay","z":"428d9dba.39a924","duration":"48","durationType":"num","units":"Hour","payloadtype":"num","payloadval":"0","name":"","reporting":"last_minute_seconds","persist":true,"x":440,"y":1400,"wires":[["dcce28eb.75a738"],[],[]]},{"id":"f836e419.b76958","type":"server-state-changed","z":"428d9dba.39a924","name":"Light to off","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.office","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"off","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":220,"y":1400,"wires":[["819eea0e.0c9728"],["b80d9f8b.a3f08"]]},{"id":"dcce28eb.75a738","type":"api-call-service","z":"428d9dba.39a924","name":"Notify","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":610,"y":1400,"wires":[[]]},{"id":"b80d9f8b.a3f08","type":"change","z":"428d9dba.39a924","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":315,"y":1420,"wires":[["819eea0e.0c9728"]],"l":false},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","addon":true}]

The advantage of doing it this way vs checking every 24 hours is you’ll get notified at the exact moment the light’s been off for 48 hours. You’ll need node-red-contrib-stoptimer-varidelay. I’d use varidelay since it survives HA restarts, NR restarts and even snapshot recoveries and, since 48hours is a fairly long timespan in a HA enviroment, you’ll be sure not to break your automation with any sort of restart.

@declankenny i tried your flow but its always triggering the “nothing happened” side. Even when I just turned the switch on and off.

@obaldius tried yours as well. Works great. Thats what I love about posting questions here. I would have never thought of doing it this way (and simply).

1 Like

just a bit more simplified, it does the same though

[{"id":"d04eedcc.532ad","type":"stoptimer-varidelay","z":"88e031b0.7ec12","duration":"48","durationType":"num","units":"Hour","payloadtype":"num","payloadval":"0","name":"","reporting":"last_minute_seconds","persist":true,"x":440,"y":3540,"wires":[["5db838ce.14fcb8"],[],[]]},{"id":"eb4f92c7.a4a1d","type":"server-state-changed","z":"88e031b0.7ec12","name":"Light to off","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.office","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":220,"y":3540,"wires":[["20d9cd2d.3b9ec2"]]},{"id":"5db838ce.14fcb8","type":"api-call-service","z":"88e031b0.7ec12","name":"Notify","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":610,"y":3540,"wires":[[]]},{"id":"20d9cd2d.3b9ec2","type":"change","z":"88e031b0.7ec12","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":315,"y":3540,"wires":[["d04eedcc.532ad"]],"l":false},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","addon":true}]