Template, 'unknown', default value, and restarts

I’ve read dozens of threads and I still don’t know how to solve this.

How can I make a template sensor not return a default value (like 0.0) when a referenced entity isn’t available? This usually happens when restarting home-assistant.

The default behavior for templates should be to be ‘unavailable’ until all referenced entities are available. Now templates (or is filter more accurate?) require default values, which are counted as actual states, and of course are always wrong. I don’t want wrong. I want accurate or nothing.

Templates simply output what you code them to output. If you use a sensor that is unavailable, your template should check for that. If you don’t know how to do that, you’re going to have to post your template so we can show you.

Thanks for helping :green_heart:
Here’s one:

- sensor:
    - name: "boiler_power"
      unit_of_measurement: 'W'
      unique_id: "sensor.boiler_power"
      device_class: power
      state: "{{ states('sensor.boiler_l1_power') | float(0.0) * 3 }}"
      state_class: measurement
      state: >
        {% set power = states('sensor.boiler_l1_power') | float(none) %}
        {% if power is not none %}
          {{ power * 3 }}
        {% else %}
          None
        {% endif %}

Your other options is to make an availability template

- sensor:
    - name: "boiler_power"
      unit_of_measurement: 'W'
      unique_id: "sensor.boiler_power"
      device_class: power
      state: "{{ states('sensor.boiler_l1_power') | float(0.0) * 3 }}"
      availability: "{{ states('sensor.boiler_l1_power') | float(none) is not none }}"
      state_class: measurement

or

- sensor:
    - name: "boiler_power"
      unit_of_measurement: 'W'
      unique_id: "sensor.boiler_power"
      device_class: power
      state: "{{ states('sensor.boiler_l1_power') | float(0.0) * 3 }}"
      availability: "{{ states('sensor.boiler_l1_power') | is_number }}"
      state_class: measurement
6 Likes

Thank you very much! I’m going with this:

availability: "{{ states('sensor.boiler_l1_power') | is_number }}"

I was having an issue with availability vs availability_template (just linking if others get stuck)

https://community.home-assistant.io/t/need-help-availability-is-an-invalid-option-for-sensor-template/333671/2?u=kelos-01

2 Likes