Template if/else doesn't work anymore

Hi,

why doesn’t the else part of the statement below work anymore? It used to show 0 but now it shows the latest value it had when “sensor.varmeprioritering_alltid_tillganglig” had the value 20

 - sensor:
      - name: "Effekt varmvatten"
        unit_of_measurement: W
        device_class: power
        state: >
          {% if is_state('sensor.varmeprioritering_alltid_tillganglig', '20') %}
            {{ states('sensor.instantaneous_used_power_32167')|float }}
          {% else %}
            0,0            
          {% endif %}

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

Hi,

sorry about that. Have now formated the post.

Probably needs to be:

          {% else %}
            0
          {% endif %}

Hi,

that didn’t work :frowning:

Now it doesn’t even understand that it is a number so it is not used in calcuations correctly and the old problem still persist where it keeps the old value.

0 is a number.

0,0 is a string. That’s your problem. You cant have a non numeric state value if you have a unit of measurement.

You should also have a default defined for your float filter:

{{ states('sensor.instantaneous_used_power_32167')|float(0) }}
1 Like

Managed to solve it. The thing that was missing was the default value when the statement was true

  - sensor:
      - name: "Effekt varmvatten"
        unit_of_measurement: W
        device_class: power
        state: >
          {% if is_state('sensor.varmeprioritering_alltid_tillganglig', '20') %}
            {{ states('sensor.instantaneous_used_power_32167')|float(default=0) }}
          {% else %}
            0            
          {% endif %}

Thank you for the assistance!

The error message about a missing default value should have been in your logs.

You can just use |float(0) as the default value is the only parameter float takes.

This is also important for your future templates. You might use a decimal comma in your locale, but templates always use a decimal point.