Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 105976

Template binary sensor not triggering immediately (delay/throttle?) - what am i doing wrong here?

$
0
0

So, im sort of trying to create a template binary sensor (room presence) that is a hybrid/combination of a pir and a mmw sensor (ie. turns on with pir, turns off with mmw, and takes into account if pir triggers without mmw, and mmw triggers without pir).

First of all, the goal is:

  • sensor turns on when pir turns on
  • sensor turns off 5s after when mmw and pir turns off
  • if mmw was never triggered, the sensor turns off 120s after pir turns off

Anyway, i have made the logic into a binary template sensor using states and last_changed calculations. Here is what i have:

- binary_sensor:
    - name: "Test sensor"
      device_class: "presence"
      state: >
        {% set is_pir_on = is_state('binary_sensor.sensorgroup_pir_test', 'on') %}
        {% set is_pir_off = is_state('binary_sensor.sensorgroup_pir_test', 'off') %}
        {% set is_mmw_on = is_state('binary_sensor.sensorgroup_ps_test', 'on') %}
        {% set is_mmw_off = is_state('binary_sensor.sensorgroup_ps_test', 'off') %}
        {% set pir_last_changed = as_timestamp(now()) - as_timestamp(states.binary_sensor.sensorgroup_pir_test.last_changed) %}
        {% set mmw_last_changed = as_timestamp(now()) - as_timestamp(states.binary_sensor.sensorgroup_ps_test.last_changed) %}
        
        {% if is_pir_on %}
          on
        {% elif is_pir_off and is_mmw_off and pir_last_changed > 5 and mmw_last_changed > 5 %}
          off
        {% elif is_pir_off and pir_last_changed > 120 %}
          off
        {% else %}
          on
        {% endif %}

This actually works as pretty well (if you have any other suggestions or improvements let me know).

BUT my problem is that its too slow. I mean, i think its just not updating immediately. Let me explain. The new sensor turns on IMMEDIATELY when the PIR triggers - great. but when pir and mmw both turns off, instead of turning off after 5 secs, it takes about 30s to turn off… WHY? I changed the delay to 10s but still takes about 30s to turn off.

im suspecting that its getting throttled somehow. But im not entirely sure how i can get around that. Any thoughts?

I could potentially use a trigger based template instead, but i feel like a trigger based template + complex templating conditions on the state would make it s bit messy (if trigger was set to 5s after pir turns off but if it triggers milliseconds early and the last_changed was not >5s then it may end up at a weird state).

Any help is appreciated, thanks in advance :slight_smile:

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 105976

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>