Best way to a binary sensor

Right now I wrote this code but I realized that it is not good for my needs:

wait_template: >-
  {{ (now() -
  states.binary_sensor.lumi_lumi_motion_ac02_iaszone_2.last_changed).total_seconds()
  > 20 }}
continue_on_timeout: false
timeout: "00:00:50"
enabled: true

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?

Don’t know your use case but what I’d do is

  • Wait 50 seconds anyway
  • 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

Thank you for suggestion but I’m a beginner. I tried to write the function like you told me in the configuration.yaml:

sensor:
  - platform: history_stats
    name: Clear_motion_detection
    entity_id: sensor.lumi_lumi_motion_ac02_iaszone_2
    state: "off"
    type: time
    end: "{{ now() }}"
    duration: "00:00:50"

this is right?
also, how do I check this in automation?

Thanks!

This is the sensor you need, and you’ll have to check for its value in the automation

- if:
  - condition: template
    value_template: |
      {{ states('sensor.clear_motion_detection') > "00:00:20" }}
  then:
    - ...

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

You might have to convert both side of the > into a number of seconds like so (not tested either)

{{ states('sensor.sensor.clear_motion_detection') | int > 20 }}

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?

You can do a wait_template condition based on the history_stats sensor, when it is reaching 20 seconds.

- alias: "Wait until 20 consecutive seconds"
  wait_template: "{{ states('sensor.clear_motion_detection') | int > 20) }}"
  timeout: "00:00:50"
  continue_on_timeout: false

again, not tested

EDIT: This replace the delay: “00:00:50” of my first answer as it will try to get 20 continuous seconds within 50 seconds.

Should I remove duration: "00:00:50" from the sensor?

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

I’m thinking of the last_update

  - condition: template
    value_template: >-
      {{ ( now()|as_timestamp() -
      states[sensor.clear_motion_detection].last_updated | default(0,true) | as_timestamp() |default(0,true)
      ) > 20 }}

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.

There is blueprint for that specific case.

or, even better

Based on last_update

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’m thinking of the last_update

trigger:
  - platform: template
    value_template: >-
      {{ ( now()|as_timestamp() -
      states[sensor.clear_motion_detection].last_updated | default(0,true) | as_timestamp() |default(0,true)
      ) > 20 }}

So this will be the trigger and in the conditions I will put only if the door is open?

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.”

1 Like

Yah, that’s a nice blueprint. If I had motion sensors, I’d use that.

Now the question is how can I associate this blueprint with the 3 minute auto shut down automation that I already have?