The state of the sensor could change several times within 50 seconds but I am interested in continuing the automation only if (in those 50 seconds) at least 20 are in the ‘off’ state
How can I change the code?
Use the history_stats sensor as condition with
a. no start
b. now() as end
c. duration: "00:00:50"
c. type: time
d. entity_id: binary_sensor.lumi_lumi_motion_ac02_iaszone_2
e. state: off
and compare that value with the 20 seconds you want to continue, or not.
If you think that the 20 seconds might be not coninuous, then you can reuse the history_stats sensor again with type: count to know the number of time it was changing from on to off in the same last 50 seconds
You can always check in the Developer Tools that the sensor is created (states tab) and its current value.
In the same place, you can also check the value_template in the Template tab
Thanks so much for all the info, I’ll try it tonight. My doubt remains that, as it is written now, it is enough that it is ‘off’ for 20 seconds in the 50. How should I modify this to see that it is 20 continuous seconds?
The duration and the end are here to give you the total time in the last 50 seconds at any time.
I’m thinking out loud that it will increase all the time, even if it is by fractions until it is 20 seconds.
I think someone else can help as I’m not using presence detection sensor.
Isn’t it a configuration somewhere to tell that it is becoming clear after a given time? Therefore, it will not turn on/off all the time.
If you’re lucky enough, @123, @tom_l or @Didgeridrew will have a look and find my solution very stupid as there might be a much better way to do it
The automation has 2 choices: 1) it does not detect movement (solved) 2) the door goes from being closed to open and I need this to verify that in the 50 seconds there is no more movement for 20 consecutive seconds (it means that you have left the room ). If so, it turns off the light, if not, it exits the timeout and the light stays on.
Isn’t it easier to just base your automation on the 50 seconds without move detection?
If door is open, you wait for motion clear for a maximum of 50 seconds. Then, if it times out, you can stop the automation, otherwise turn off the light.
A motion sensor is not supposed to become clear instantly.
yes I could, but you could be detected after opening the port and then the automation (wait template or wait trigger) fails. It only worked if I wasn’t detected at the moment of opening
I think that you should trigger the automation on the door opened. It is the logical condition.
Then, your PIR will trigger motion (normally, if I understand your need)
Then, you can do a wait_template based on the last_updated, which will ensure 20 consecutive seconds. If this wait_template timeout after 50 seconds, you know that motion never stopped for at least 20 seconds in the last 50.
Therefore, you can keep the light on and/or do some conditions based on the door sensor (if door is closed, probably someone is still inside).
With that solution, no need for the history_stats sensor.
Sorry but it’s not clear to me, now I’ll explain better.
I enter the bathroom, close the door and the light stays on all the time. When I open the door to go out, the presence sensor could detect me. If it doesn’t reveal the condition I wrote at the beginning, it’s fine but if it detects me it doesn’t work anymore because the state changes. I would like to make the light go off if after 20 consecutive seconds no one is detected for a maximum of 50 second timeout. This is because I could close the bathroom door when I leave but no longer be inside and the light still has to go out.
How does it know when you enter or exit? To me and you it makes sense. To a motion sensor, it just see’s motion. If you attempt to track something like this, It’ll get out of sync. Like if someone enters the room closes the door, leaves the room and someone else enters right after, closes the door., and sits still on the pot. You now have a person sitting in the dark after 50 seconds.
Totally agree with you, that’s the reason why I put the Occupancy Blueprint, that is using the mechanism of the wasp in the box.
The blueprint seems perfectly adapted to this use case :
“The lights in my bathroom should stay on as long as I am in the room. No matter if I move or not.”
“As long as the door is closed and movement was detected once, the light can stay on. The only way for you to leave the room, is to open the door.”