Templating question in sensors regarding updating

I have this template for a sensor:

  - name: "Battery Storage 24kWh energy"
    unique_id: "battery_storage_24kwh_energy"
    unit_of_measurement: 'kWh'
    state_class: measurement
    device_class: energy_storage
    state: >
      {% set maxCapkwh = 24 %}
      {% set wert = (states('sensor.ewz_power_meter_pv_power_return') | float(0)) %}
      {% if wert > 0 %}
        {% set tempKwBa = wert / 3600 * 10 / 1000 %}
        {% set tempKwBa = (states('sensor.battery_storage_24kwh_energy') | float(0)) + tempKwBa %}
        {% if tempKwBa > maxCapkwh %}
          {% set tempKwBa = maxCapkwh %}
        {% endif %}
        {{ tempKwBa }}
      {% endif %}

But it does just update once HA does boot.
So why does this not update once sensor.ewz_power_meter_pv_power_return chages its value?
or how do such templating update?

Andy

Put this in the template debugger and it will tell you on which state changes it listens.
If it doesn’t do what you expect, you might have a logic issue…

1 Like

Jinja if statements require an else case.

    state: >
      {% set maxCapkwh = 24 %}
      {% set wert = (states('sensor.ewz_power_meter_pv_power_return') | float(0)) %}
      {% if wert > 0 %}
        {% set tempKwBa = wert / 3600 * 10 / 1000 %}
        {% set tempKwBa = (states('sensor.battery_storage_24kwh_energy') | float(0)) + tempKwBa %}
        {% if tempKwBa > maxCapkwh %}
          {% set tempKwBa = maxCapkwh %}
        {% else %}
          what here?
        {% endif %}
        {{ tempKwBa }}
      {% else %}
        again, what here?
      {% endif %}
1 Like

thx a lot guys!!! This helped me tremendous!