Template sensor goes to "unavailable" without reason

Hi everyone

I still have a problem with my template sensors…

The problem: I have defined a template sensor that should give exactly the same result as the physical sensor:

Physical sensor:

sensor.smart_plug_p07_power

And my template sensor:

  # ---------------------------------------------------
  - name: "Elektriciteit Kring A power"
    unique_id: "sensor.elektriciteit_kring_a_power"
    icon: mdi:flash
    unit_of_measurement: "W"
    device_class: energy
    state_class: measurement
    state: >
      {% if ( states('sensor.smart_plug_p07_power') not in ['unknown', 'unavailable', 'None'] ) %}
        {{ states('sensor.smart_plug_p07_power') | float }}
      {% else %}
        {{ "0" | float }}
      {% endif %}
    availability: >
      {{ states('sensor.smart_plug_p07_power')  }} 
    attributes:
      attribute: >
        {{ now().minute }}

If I take a closer look at the values in Grafana, I can see the following:

  • my physical sensor “P07” reports correct values (1) and (2)
  • my template sensor only shows differences in values different from zero. So when my “P07” reports a power consumption of zero, it seems that my template sensor doesn’t get this information.

If I take a closer look to the native Home Assistant data:
Physical Sensor
2023-03-27_12-37-33

Template Sensor:
2023-03-27_12-37-00

So for some reason, my template sensor goes to “unavailble” despite my physical sensor is still available.

What’s the reason for this?
How this can be avoided?

Kind regards,
Bart Plessers

Change it to this:

    state: >
      {{ states('sensor.smart_plug_p07_power')|float(0) }}
    availability: >
      {{ states('sensor.smart_plug_p07_power')|is_number }} 

The availability template you have at the moment will cause the template sensor to go unavailable whenever the source sensor value is 0.

1 Like

@tom_l

works perfect!
Thanx for this solution.