Sensor with value_template does not show up

Hello everyone, I want to rewrite the first code so that it works even if one of the sensors has the status unknown. Unfortunately, the sensor does not appear in the corrected version. Do I have an error in the code?"

First Code:

template:
    - sensor:

        - name: v2
          state_class: total_increasing
          icon: mdi:transmission-tower
          unit_of_measurement: W
          device_class: power
          state: "{{states('sensor.tasmota_power_power_curr') | float() +states('sensor.balkon_ch2_power') | float() }}"

And new Version:

template:
  - sensor:
      
      - name: v2
        state_class: total_increasing
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        value_template: >
          {% set sensor_a = states('sensor.tasmota_power_power_curr') | float %}
          {% set sensor_b = states('sensor.balkon_ch2_power') %}
          {% if sensor_b == 'unknown' %}
            {{ sensor_a }}
          {% else %}
            {{ sensor_a + sensor_b | float }}
          {% endif %}

Thank you so far !

Replace value_template: with state:

1 Like

yes im new :smiley:

thank u

1 Like

You could just use:

state: >
  {{ states('sensor.tasmota_power_power_curr')|float(0)
   + states('sensor.balkon_ch2_power')|float(0) }}

The default 0 in the call to float is returned if the state cannot be converted to a number, like in the case of unknown.

1 Like