If State Within x minutes in the past

Is there a simple way to check whether an entity was on a state WITHIN a period of history (from now). For example if motion sensor was triggered at all within the past 5 minutes.

The current state node has a FOR setting, but not WITHIN.

Anyone know if a way to do this?

Use the get history node.
It will return an array with timestamps of state changes that you need to loop through in a function node.

[{"id":"995d6eea163efb93","type":"api-get-history","z":"587f20ab.e7c01","name":"","server":"4bbca37b.1700ec","version":0,"startdate":"","enddate":"","entityid":"device_tracker.moto_g_8","entityidtype":"is","useRelativeTime":true,"relativeTime":"5 minutes","flatten":false,"output_type":"array","output_location_type":"msg","output_location":"payload","x":2620,"y":840,"wires":[["2754570e80c6b10c"]]},{"id":"2754570e80c6b10c","type":"function","z":"587f20ab.e7c01","name":"","func":"var zones = msg.payload[0].map( el => el.state );\nmsg.unique = zones.filter(onlyUnique);\n\nif (msg.unique.includes(\"on\") == true){\n    return msg;   \n}\n\nfunction onlyUnique(value, index, self) {\n  return self.indexOf(value) === index;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2780,"y":840,"wires":[["6374369fba9d0bd3"]]},{"id":"0b51bba47b42aae6","type":"api-current-state","z":"587f20ab.e7c01","name":"","server":"","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":2440,"y":840,"wires":[["995d6eea163efb93"]]},{"id":"145bf8ee6fe19c79","type":"inject","z":"587f20ab.e7c01","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2240,"y":820,"wires":[["0b51bba47b42aae6"]]},{"id":"6374369fba9d0bd3","type":"debug","z":"587f20ab.e7c01","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2950,"y":840,"wires":[]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

I just realized the variable is called zones in the function node. That is because that is what I used it for.
But you can rename it to states to make it more clear

var states= msg.payload[0].map( el => el.state );
msg.unique = states.filter(onlyUnique);

if (msg.unique.includes("on") == true){
    return msg;   
}

function onlyUnique(value, index, self) {
  return self.indexOf(value) === index;
}

Or use a Boolean helper in HA.
Make NR set it to true when the PIR is triggered and set a delay to set it to false when the time period is up and then set the setting in the delay mode to reset the counter on new messages.

That won’t work.
The boolean will keep flowering back and forth because each message is delayed

No, the delay node should have an option to reset the counter only.
I have not used it myself yet, because ai missed that option, but I saw a solution with it.
In my flows I have until that revelation just used a merge node where you can also set the timer.

The delay node should hold the last message until the time has run out.
The merge delay does the same, but add all received messages to an array too.

Delay node can delay each message or rate limit.

Ok, then use the merge node. :smiley:

Hmm thank you for your suggestions. When I get a chance I’ll try them out and see what works best for my use case any my abilities.

I’m afraid I’m not sure how to use your nodes.

Where do I put my entity? In the current_state node? Do I leave If State blank? How can I get a simple true/false output, because I I did get a message object out from the function, but I don’t know how to translate it to a simple true/false case to either trigger another node or not trigger it.

If this is too much to, don’t worry about it, I won’t be bothered about it, and I’ll try a different method.

Okay let me ask another question.

I have a door sensor that I use to trigger a notification. I want this notification to only happen if it is opened from the outside. If we open it from the inside, then the alarm should not run, because it’s not useful and can be annoying.

I have a motion sensor, and I was thinking if it triggered a few seconds before the door opened, then the notification does not go off.

If the door is opened without the motion sensor having been triggered previously it would notify. I’m also afraid that the motion sensor might trigger and the door would open and it would not alarm, so I’m not sure this is the best way to do it…

Of course, I could also just create an ALARM condition the “proper” way and enable the alarm mode at night for example, but there are times when we do want to know when someone enters the house, even if it’s not during when you typically turn on the alarm (at night when going to sleep).

Maybe someone has a better idea on how I can accomplish this automation.

Have the Motion sensor change of state feed into a trigger node and a gate node - the initial motion will send an On to the trigger node which will pass it through to the gate node and close the gate - have a timeout on the trigger node of say 30 seconds so it will rearm (and open the gate) if no motion for 30 seconds.

Have your door open action feed into the gate node - if the gate is open (i.e. no motion detected from inside) it will allow the door open message through and sounds the alarm etc. If the gate is closed no message will pass through (i.e. motion on the inside of the door) and no alarms will be sounded

Craig

You can use a history node with a switch

[{"id":"79e47c8f918e7911","type":"inject","z":"f80b6c338afd5483","name":"door sensor","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":2660,"wires":[["5bfbe652ebf70ed6"]]},{"id":"5bfbe652ebf70ed6","type":"api-get-history","z":"f80b6c338afd5483","name":"","server":"6b1110b5.183a4","version":0,"startdate":"","enddate":"","entityid":"binary_sensor.motion","entityidtype":"is","useRelativeTime":true,"relativeTime":"5min","flatten":true,"output_type":"split","output_location_type":"msg","output_location":"payload","x":370,"y":2660,"wires":[["42380187e6d0017d"]]},{"id":"42380187e6d0017d","type":"switch","z":"f80b6c338afd5483","name":"","property":"payload.state","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":570,"y":2660,"wires":[["6977a8432bfe3bcf"],["20bb073f12b0f1f5"]]},{"id":"6977a8432bfe3bcf","type":"debug","z":"f80b6c338afd5483","name":"motion on do something","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":2620,"wires":[]},{"id":"20bb073f12b0f1f5","type":"debug","z":"f80b6c338afd5483","name":"motion off do something","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":2680,"wires":[]},{"id":"6b1110b5.183a4","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

Sorry that only works if you are looking for the on message to do something. Using a trigger node for the door can be disabled by the motion detector for 5 min.

[{"id":"9b8896881b9fb686","type":"server-state-changed","z":"f80b6c338afd5483","name":"","server":"6b1110b5.183a4","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binsry_sensor.motion","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,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"date"}],"x":220,"y":3340,"wires":[["c6ba48847c065006"],[]]},{"id":"245baa783e2ea025","type":"trigger-state","z":"f80b6c338afd5483","name":"","server":"6b1110b5.183a4","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.door","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"inputs":1,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":true,"x":830,"y":3340,"wires":[[],[]]},{"id":"c6ba48847c065006","type":"trigger","z":"f80b6c338afd5483","name":"","op1":"DISABLE","op2":"ENABLE","op1type":"str","op2type":"str","duration":"5","extend":true,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":550,"y":3340,"wires":[["245baa783e2ea025"]]},{"id":"6b1110b5.183a4","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]
1 Like

For this particular example you can easily achieve this:

[{"id":"29333e5f.2d3672","type":"api-current-state","z":"878e74c2.7f39c8","name":"PIR off?","server":"9405c3fe.d0a6c","version":3,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.your_pir","state_type":"str","blockInputOverrides":true,"outputProperties":[{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":380,"y":360,"wires":[["a8e1be20.a58ed"],[]]},{"id":"190846c.dc458b9","type":"inject","z":"878e74c2.7f39c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":360,"wires":[["29333e5f.2d3672"]]},{"id":"a8e1be20.a58ed","type":"switch","z":"878e74c2.7f39c8","name":"for less that 5 min?","property":"data.timeSinceChangedMs","propertyType":"msg","rules":[{"t":"lte","v":"300000","vt":"num"}],"checkall":"false","repair":false,"outputs":1,"x":550,"y":360,"wires":[["22115603.ea2d3a"]]},{"id":"22115603.ea2d3a","type":"debug","z":"878e74c2.7f39c8","name":"Do","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":360,"wires":[]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

Thank you all for your awesome suggestions. I’ll play around with these options. If I don’t forget, I’ll return here to note the success =)

Thank you all for your help.

I decided to go with Mikefila’s suggestion, because it was the easiest for me to work with.

One of the things that happened, was that I wanted to reduce the trigger duration (to 1 minute), but also re-enable the trigger, in case we are loafing around at the entrance prior to exiting. In Mikefila’s example, once the duration expired, the logic stopped working.

Therefore, I added a inject node to check the motion status every 30 seconds to re-enable the trigger and keep the notification trigger alert disabled.

See below:

[{"id":"53ea1bf3b0f37d0d","type":"trigger-state","z":"533132cd80eab3a0","name":"front door opened","server":"cfa33987.87d198","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.front_door_sensor_contact","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"inputs":1,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":true,"x":790,"y":760,"wires":[["2c0dc9b678598a9b"],[]]},{"id":"2c0dc9b678598a9b","type":"api-current-state","z":"533132cd80eab3a0","name":"lux below 100","server":"cfa33987.87d198","version":3,"outputs":2,"halt_if":"100","halt_if_type":"num","halt_if_compare":"lt","entity_id":"sensor.motion_sensor_0004_illuminance_lux","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1020,"y":760,"wires":[["87f8bcec4c1dbca8"],[]]},{"id":"ca620ec528b0c522","type":"trigger","z":"533132cd80eab3a0","name":"","op1":"disable","op2":"enable","op1type":"str","op2type":"str","duration":"1","extend":true,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":570,"y":760,"wires":[["53ea1bf3b0f37d0d"]]},{"id":"a9bb5028c95f47ab","type":"inject","z":"533132cd80eab3a0","name":"inject 30 sec","props":[{"p":"payload"}],"repeat":"30","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":820,"wires":[["e72d08ee37fab3cc"]]},{"id":"7786138b12ddda1b","type":"trigger-state","z":"533132cd80eab3a0","name":"motion sensed","server":"cfa33987.87d198","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.motion_sensor_0004_occupancy","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"inputs":1,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":true,"x":340,"y":760,"wires":[["ca620ec528b0c522"],[]]},{"id":"e72d08ee37fab3cc","type":"api-current-state","z":"533132cd80eab3a0","name":"motion sensed","server":"cfa33987.87d198","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.motion_sensor_0004_occupancy","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"date"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":340,"y":820,"wires":[["ca620ec528b0c522"],[]]},{"id":"cfa33987.87d198","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

Thank you all again very much.

P.S. Where does everyone comment their code? I have used the comment nodes, but I wonder if there’s a better place everyone does it.

Sounds like the time it takes for the motion detector to reset is > 1 minute. If there is any settings for it look for latch time. I use cameras for motion and they reset every 10 seconds or so. Every minute I have several triggers.

Comment nodes are what you would use to describe what the flow is doing.

1 Like