Home presence time limit restriction

Hi all, I´m switching to Red Node and I have the following flow for presence, is really simple. When my phone connect/disconnect to the wifi, a boolean is toggled (at home?), is the boolean is on, the flow check the sun sensor and turn the lights on, set the volume of my google home and send a push over notification with the T° of my room and my city (santiago, Chile).

Anyway, this is working, the problem I have is that the wifi doesnt reach my room perfectly, so sometimes my phone got disconnected from wifi and then it reconnect 10 seconds after, triggering the lights in the middle of the night.

What I want to do is a filter that check the last time the boolean was toggle and establish a time limit, like if at home boolean last change time was less than 2 minutes ago, stop the flow In this way I want to avoid HA to think I left my home and then come back in 1-2 minutes.

I´m using a rasbperry pi 3b+, HA 0.76.1,

Hope this request make sense.

Thanks for the help

not sure about node-red but this is how I’ve coded this in HA: I added a condition that if the automation ran in the last xxx sec do not run the automation again:

    - condition: template
      value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.AUTOMATION_NAME_GOES_HERE.attributes.last_triggered | default(0)) | int > 120)}}'

Above example is for 120 sec (2 min)

thanks, I switch to yaml automation and use your solution, its working great!
I think that node red is a great app but I need to study before migrate 100%

1 Like

Take a look at this

[{"id":"560d1b0b.9538e4","type":"cron","z":"b4f13d0.1052fc","name":"Every hour[22:00 to 6:00]","crontab":"01 22-23,0-6 * * *","x":150,"y":540,"wires":[["e65d3982.fc5e98"]]},{"id":"e65d3982.fc5e98","type":"api-current-state","z":"b4f13d0.1052fc","name":"checkLoungeSensor","server":"d5ce5cec.92bc8","halt_if":"on","override_topic":true,"override_payload":true,"entity_id":"binary_sensor.0x00158d0001f9b9af_occupancy","x":400,"y":540,"wires":[["d6aea734.352bd8"]]},{"id":"4c016abc.77d9d4","type":"inject","z":"b4f13d0.1052fc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":600,"wires":[["e65d3982.fc5e98"]]},{"id":"99db09e1.ecabe8","type":"comment","z":"b4f13d0.1052fc","name":"Turn off lounge devices [WIP]","info":"","x":140,"y":500,"wires":[]},{"id":"d6aea734.352bd8","type":"function","z":"b4f13d0.1052fc","name":"addSeconds","func":"dateSinceLastChanged = Date.parse(msg.data.last_changed);\ncurrentDate = (new Date).getTime();\nsecondsSinceLastChange = Math.round((currentDate - dateSinceLastChanged) / 1000);\nmsg['secondsSinceLastChange'] = secondsSinceLastChange;\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":540,"wires":[["a6e2fa4c.a97ef8"]]},{"id":"a6e2fa4c.a97ef8","type":"switch","z":"b4f13d0.1052fc","name":"passOnlyAfter45minutes","property":"secondsSinceLastChange","propertyType":"msg","rules":[{"t":"gte","v":"2700","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":810,"y":540,"wires":[["d5900ef1.b1ea6"]]},{"id":"d5900ef1.b1ea6","type":"api-current-state","z":"b4f13d0.1052fc","name":"checkKodiLounge","server":"d5ce5cec.92bc8","halt_if":"playing","override_topic":true,"override_payload":true,"entity_id":"media_player.kodi_lounge","x":1050,"y":540,"wires":[["8a5e3223.2d92a"]]},{"id":"8a5e3223.2d92a","type":"change","z":"b4f13d0.1052fc","name":"delEntity_Id","rules":[{"t":"delete","p":"data.entity_id","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1230,"y":540,"wires":[["e622dd7.8143b2"]]},{"id":"e622dd7.8143b2","type":"api-current-state","z":"b4f13d0.1052fc","name":"checkLoungeTV","server":"d5ce5cec.92bc8","halt_if":"on","override_topic":true,"override_payload":true,"entity_id":"media_player.lounge_tv","x":1400,"y":540,"wires":[["d27438b8.6071d8"]]},{"id":"d27438b8.6071d8","type":"api-call-service","z":"b4f13d0.1052fc","name":"turnOffLoungeDevices","server":"d5ce5cec.92bc8","service_domain":"script","service":"turn_on","data":"{\"entity_id\":\"script.turn_lounge_off\"}","mergecontext":"","x":1620,"y":540,"wires":[[]]},{"id":"d5ce5cec.92bc8","type":"server","z":"","name":"Home Assistant","url":"https://localhost","pass":"password"}]

This checks every hour for lounge activity to turn off some lights and the TV, it will parse the msg.data.last_changed field, which usually comes in Iso8601, transforms into seconds. Then there is a switch that will only let if there has been no motion for 45 minutes at least. The timestamp-date transformation is a function, i don’t know if there is a specific node that can do that time change to seconds.

For your case using state changed the last_changed property will be in:

msg.data.old_state.last_changed

I had the same issue with an unstable signal (especially when phone battery went low). I fixed it with the following sequence

  1. Get the “At home trigger”
  2. Wait 90 seconds (Delay 90s) … or whatever timeout works best for you
  3. re-validate your “At home” status … if it is still "home’ proceed with your automation