Sensor with delay_off has state unknown at startup when off

I have a sensor template that has a delay_off. Therefore, if the state at startup is not on it will be unknown. Which kind of makes sense, because it hasn’t been off for 1800 s.

But I want it to be off in that case.

How can I do that?

    - name: "test 1"
      unique_id: "test_1"
      unit_of_measurement: ""
      delay_off:
        seconds: 1800
      state: >
        {% if states('sensor.temperatur_test')|float > 5 %}
          {{ "on" }}
        {% else %}
          {{ "off" }}
        {% endif %}

What version of home assistant are you running?

Also if it only has a binary state (on/off) use a binary sensor:

template:
  - binary_sensor:
      - name: "test 1"
        unique_id: "test_1"
        delay_off:
          seconds: 1800
        state: "{{ states('sensor.temperatur_test')|float(0) > 5 }}"

The state of binary sensors with delays are restored on restart as of v2022.4

I am on 2022.4.4. But I changed the template to test it. Maybe that resets it. The problem is that I have to have an off state to begin with and probably half an hour of not restarting HA. :slight_smile:

No, it won’t save the state.

I just waited 30 minutes and it went off, after a restart it is back to unknown.

I can force it to on:

        {% if (as_timestamp(now()) - as_timestamp(states('sensor.uptime'))) < 1 %}
          {{ "on" }}
        {% else %}
       ...
        {% endif %}

But how can I force it to off without the delay?

Are you still using a sensor rather than a binary sensor?

I was always using a binary sensor, I just missed the first line to copy.

Then this:

        {% if (as_timestamp(now()) - as_timestamp(states('sensor.uptime'))) < 1 %}
          {{ "on" }}
        {% else %}
       ...
        {% endif %}

Should be:

state: "{{ as_timestamp(now()) - as_timestamp(states('sensor.uptime')) < 1 }}"

Which will return true or false depending on the numeric test.

The “…” where actually used for my actual if statement. But since this didn’t work out I have replaced it with my original if statements again. They do work, but after a restart the first half hour the sensor will be unknown, which is not ideal.

:man_shrugging:

Template binary sensors with delays are restoring their state for me.

Not for me.

Did you try something like this:

  - binary_sensor:
    - name: "Test 1"
      unique_id: "test_1"
      unit_of_measurement: ""
      delay_off:
        seconds: 18
      state: >
          {% if (states('sensor.temperature_1')|float < -20) %}
            {{ "on" }}
          {% else %}
            {{ "off" }}
          {% endif %}

I don’t understand the difference. The above code will remain the state after a restart.

However my “real script” won’t:

  - binary_sensor:
    - name: "Garage mold"
      unique_id: "garage_mold"
      unit_of_measurement: ""
      icon: mdi:water-percent-alert
      delay_off:
        seconds: 1800
      state: >
          {% if ((states('sensor.temperature_outdoor')|float - state_attr('sensor.mold_indicator_garage','dewpoint')) < - 5 ) and states('sensor.temperature_garage')|float > 5 %}
            {{ "on" }}
          {% else %}
            {% if ((states('sensor.temperature_outdoor')|float - state_attr('sensor.mold_indicator_garage','dewpoint')) > - 3 )  %}
              {{ "off" }}
            {% else %}
              {{ "on" }}
            {% endif %}
          {% endif %}
          

As I keep telling you, binary templates should resolve to true or false. Not the strings “on” or “off”.

All of these work for me:

- name: "Dishwasher Running"
  icon: "mdi:dishwasher"
  state: "{{ states('sensor.dishwasher_power')|float(0) > 10 }}"
  device_class: moving
  delay_off:
    minutes: 5
  delay_on:
    minutes: 2

- name: "Excess Solar"
  icon: "mdi:solar-power"
  state: "{{ ( states('sensor.lp20_inverter_power')|float(0) - states('sensor.hot_water_power')|float(0) ) > 2500 }}"
  delay_off:
    minutes: 10
  delay_on:
    minutes: 10

- name: "Washing Machine Running"
  icon: "mdi:washing-machine"
  state: "{{ states('sensor.washing_machine_power')|float(0) > 2.5 }}"
  device_class: moving
  delay_off:
    minutes: 1
1 Like

These don’t work either

            {{ True }}
            {{ False }}

You don’t need to specify them. The numeric test “>” will return true or false.

      state: >
          {% if ( states('sensor.temperature_outdoor')|float(0) - state_attr('sensor.mold_indicator_garage','dewpoint') ) < -5 and states('sensor.temperature_garage')|float > 5 %}
            {{ true }}
          {% else %}
            {{ states('sensor.temperature_outdoor')|float(0) - state_attr('sensor.mold_indicator_garage','dewpoint') > - 3 ) }}
          {% endif %}

This saves me an if statement but I still have the same issue.

What else can I do? Other binary sensors with off delays work, the example above works, but not this one. Not even if i put all in one {{...}}.

Could it be the mold indicator that is unknown at startup?

Are there any errors related to the tempalte in your log?

I not that you are not casting the attributes as numbers. You may not have to as they can be numbers (unlike all states), but you should check.

I added |float to the mold indicator.

There is an error:

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:167 
Integration: Template (documentation, issues) 
First occurred: 17:36:59 (1 occurrences) 
Last logged: 17:36:59

TemplateError('TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'') while processing template ...

I guess this comes from the mold indicator…

Give all the sensors and attributes defaults.

I deleted the mold indicator and made my own template for dewpoint.

  - sensor:
    - name: "Dewpoint"
      unique_id: "dewpoint"
      unit_of_measurement: "°C"
      icon: mdi:water
      state: >
        {% set a = float(17.271) %}
        {% set b = float(237.7) %}
        {% set dewpoint_tmp =(a *states('sensor.temperature')|float) / (b + states('sensor.temperature')|float) + log(states('sensor.humidity)|float/100) %}
        {% set dewpoint = (b * dewpoint_tmp) / (a - dewpoint_tmp) %}
        {{ dewpoint |float |round(1) }}

Now it works as expected.

Thanks for your help on the way!