Don't push template change

Hi everyone,

I’m writing a template_sensor which should fetch another sensor and do some basic calculations.

Basically I’m using:

  - platform: template
    sensors:
      all_3_phases_power:
        friendly_name: "All Phases Power"
        value_template: >-
          {% set phases = (states('sensor.phase_1_power') | float + states('sensor.phase_2_power') | float + states('sensor.phase_3_power') | float) | round(2) %}
          {% set phase_1_updated = (now().timestamp() - states.sensor.phase_1_power.last_changed.timestamp()) < 1 %}
          {% set phase_2_updated = (now().timestamp() - states.sensor.phase_2_power.last_changed.timestamp()) < 1 %}
          {% set phase_3_updated = (now().timestamp() - states.sensor.phase_3_power.last_changed.timestamp()) < 1 %}
          {% set all_phases_updated = phase_1_updated and phase_2_updated and phase_3_updated %}
          
          {% if phases != 'unavailable' and phases > 0.0 %}
          {% if all_phases_updated %}
          {{ phases | float | round(2) }}
          {% endif %}
          {% else %}
          {{ 'unavailable' }}
          {% endif %}
        unit_of_measurement: "W"
        icon_template: mdi:power

The problem is if all_phases_updated is False, the sensor gets an empty value, and I just want it to stay at the last value (Not pushing the value).

The reason I need to push if all_phases_updated is True is because it updates 3 times in rapid sucession to every update in the 3 phases (every 5s), so I watch first if all 3 phases have updated in the last 1 sec and only then push the value to the template.

So, the question is: How do I avoid a empty value in the template when the value isn’t “ready” (aka. How do I not push a value to the template/ignore this iteration)?

Thanks in advance

Instead of {{ 'unavailable' }}, use this to keep the last value of the sensor:

{{ states('sensor.all_3_phases_power') }}

Also indenting your code makes it easier to read and see that you were missing an else statement as well.

          {% if phases != 'unavailable' and phases > 0.0 %}
            {% if all_phases_updated %}
              {{ phases | float | round(2) }}
            {% else %}
              {{ states('sensor.all_3_phases_power') }} 
            {% endif %}
          {% else%}
            unavailable
          {% endif %}

Yes! It worked! Didn’t know I could reference the sensor to itself.

Final template:

{% set phases = (states('sensor.phase_1_power') | float + states('sensor.phase_2_power') | float + states('sensor.phase_3_power') | float) | round(2) %}
{% set phase_1_updated = (now().timestamp() - states.sensor.phase_1_power.last_changed.timestamp()) < 2 %}
{% set phase_2_updated = (now().timestamp() - states.sensor.phase_2_power.last_changed.timestamp()) < 2 %}
{% set phase_3_updated = (now().timestamp() - states.sensor.phase_3_power.last_changed.timestamp()) < 2 %}
{% set all_phases_updated = phase_1_updated and phase_2_updated and phase_3_updated %}

{% if phases != 'unavailable' and phases > 0.0 %}
  {% if all_phases_updated %}
    {{ phases | float | round(2) }}
  {% else %}
    {{ states('sensor.all_3_phases_power') }}
  {% endif %}
{% else %}
  {{ 'unavailable' }}
{% endif %}

I’ve also changes <1 to <2, since all 3 didn’t update so fast sometimes, causing the sensor to go unknown forever.

Thanks so much!

You don’t need double braces or quotes around unavailable.

1 Like

I was thinking unavailable should be treated especially… So it’s just a normal String and Home Assistant will interpret that?

Didn’t know that. I’ll change it then :slight_smile:

Nope. States are just strings.

1 Like