Duration-based State Triggers and rapid "OFF" --> "ON" --> "OFF" changes question

I understand that. And it is a good solution. What I mean is how are you going to ignore the brief changes using a template?

1 Like

I’ve confirmed my solution works – going from OFF to OFF doesn’t seem to be reflected in the history nor in updates to anything else. It’s as if it didn’t happen. It’s really just taking advantage of an optimization in HA (which in other cases can be pretty bad, e.g. when you are trying to compute derivative of a sensor w.r.t. time and the value of that sensor has not changed).

You still have not answered my question.

How are you ignoring the brief states in the template sensor?

What method are you using?

sorry - family time
triggers
template sensors support the triggers platform
the difference between doing that directly in an automation vs passing it through a template sensor first is that the template sensor automatically deduplicates repeat values, so the double OFF triggered isn’t an issue (since it goes from OFF to OFF, see our examples above)

No it does not. That’s the problem. It goes on briefly between the the off states.

At 11 sec in your diagram above.

You will have exactly the same issue as using the automation.

Would it maybe help if you explained in what real life situation you’re going to use said automation? This reminds me of something I set up recently where a button press turns a light on 10%. If I press it again withing 2sec, it goes to 20%, within 2sec again 30% and so on. If the button is not pressed for >2sec, then the button acts like an “off” switch.

As I mentioned earlier, using the delay_on and delay_off options in a template binary sensor could be a way around this.

template:
  - binary_sensor:
      - name: Debounced
        state: >
          {% if has_value('binary_sensor.some_data') %}
            {{ states('binary_sensor.some_data')|bool }}"
          {% else %}
            {{ this.state }}
          {% endif %}
        delay_on: 10
        delay_off: 10

That binary sensor won’t change for states shorter than 10 seconds. It will also ignore the unavailable and unknown states (due to the has_value() test).

binary_sensor.some_data

0 off
11 on
12 off
13 on

Produces:

binary_sensor.debounced

0 off
11 off
12 off
13 off
23 on

Likewise for the opposite states of the source sensor.

2 Likes

nice! that should work. is the same concept but more elegant.

As you can see in the diagram it doesn’t trigger because of the duration clause (for: etc)
Maybe I’m being stupid but I’ve tested and it works fine.

I never see an on state at all in that case, and importantly the second triggered OFF isn’t actually reflected as an update to the template sensor by HA because the value is the same as the previous one.

I’ll test your delay option as well once I’m home. Maybe I’m just missing something fundamental

Apologies @tom_l I haven’t had a chance to test the delay solution since the one I did with the template trigger platform sensor works for me and solved my problem. However, based on your confirmation above I will mark it as the solution. Thanks for your help and time investment to help solve this!

1 Like