Binary sensor not turning off

i need some help regarding a template sensor… can someone tell me why this sensor never turns to “off”?

- trigger:
    - platform: state
      entity_id: sensor.sonnen_batterie_verbrauch
  binary_sensor:
    - name: "Sauna angeschaltet"
      state: >
        {{ states('sensor.sonnen_batterie_verbrauch') | float > 800 }}
      delay_off: "00:00:30"

I swear to god the input sensor has been below 800w for at least 5 minutes…

Was it above 800 before that?

The template needs to go from false to true, i.e. sensor from above 800 to below 800.

Also It has to be false (above 800) for 30 seconds.

Also the state template will only update if sensor.sonnen_batterie_verbrauch or one of its attributes changes.

Yes, turning the sensor one is working, here is a screenshot of the sensor data. Each dip of the power consumption is about 4 minutes long, so should okay right?

You don’t actually need a trigger as the same sensor is in the template and will cause the template to update when it changes:

template:
  - binary_sensor:
      - name: "Sauna angeschaltet"
        state: >
          {{ states('sensor.sonnen_batterie_verbrauch') | float > 800 }}
        delay_off: "00:00:30"

Yes that did the trick… So basically I was turning it on and off at the same time…

No, the template would still decide if the binary sensor was on or off based on the sensor value.

I think it’s actually unexpected behaviour.

You were triggering on any change of sensor.sonnen_batterie_verbrauch (or its attributes) and then evaluating the template.

That was not required as the plain state based template version would update on any change of sensor.sonnen_batterie_verbrauch (but not on change of its attributes).

Your original trigger version was redundant but should have lead to the same behaviour. With the exception that your trigger would also update the template if any of the sensor’s attributes changed.

I’m a little confused as to why your trigger version did not work.

@123 , @petro , @Burningstone apologies for the ping but this is an interesting enigma you may enjoy, any idea why the original trigger based version failed to operate as expected?

Well I am glad I found something interesting;-)

But I agree, it‘s at least unexpected and not really self explenatory since automation triggers and templates in automation conditions & actions work together differently.

The first thing that comes to mind is that it doesn’t need a State Trigger because its value_template is already capable of listening to the same sensor’s state-changes.

Based on my understanding, with a State Trigger it listens for any changes to the sensor’s state. When a change is detected, it triggers and evaluates the template.

Without a trigger it also listens for any changes to the sensor’s state (because the sensor exists within the template so Home Assistant creates a listener for it) and, when a change is detected, it evaluates the template.

@yolani
Out of curiosity, which version of Home Assistant are you using? Is it recent?
(Trigger-based Template Sensor was introduced in 2021.5.

I am using the docker image with the stable tag, continuously updating through Watchtower. So currently this is equal to 2021.7.3.

BTW, the input sensor is a value I retrieve with a 2s scan interval through a REST sensor from my local house power management, so it’s quite frequently updated.