Template help needed

Hi all,

I’ve been trying to solve this but could not succeed.

I want to calculate state only of the templated sensor only if the states of all of the 3 sensors are 20 or more, otherwise the templated sensor must be 0 (zero)

See current code below

      - name: actueel_zonnepanelen_opbrengst
        unique_id: actueelzonnepanelenopbrengst
        unit_of_measurement: 'W'
        state: '{{ states("sensor.dng0a4702r_pv2_watt") |float(0) |round(0)
             + states("sensor.cmg4bd702g_pv1_watt") |float(0) |round(0) 
             + states("sensor.dng0a4702r_pv1_watt") |float(0) |round(0) }}'
        device_class: power

Any help is more than welcome.

Which one do you want?

Report the sum of the three sensors:

  1. Only if the sum is greater than 20. Otherwise report 0.

  2. Only if each sensor’s value is greater than 20. Otherwise report 0.

Hi @Taras,

Both will do, but if I had to choose, option 2

Thanks,
Rien

{% set slist = [states("sensor.dng0a4702r_pv2_watt")|float(0),
                states("sensor.cmg4bd702g_pv1_watt")|float(0),
                states("sensor.dng0a4702r_pv1_watt")|float(0)] %}
{{ slist|sum|round(0) if slist|min >= 20 else 0 }}

Here are both versions.

  1. Only if the sum is greater than 20. Otherwise report 0.
        state: >
          {% set total = expand('sensor.dng0a4702r_pv2_watt', 'sensor.cmg4bd702g_pv1_watt', 'sensor.dng0a4702r_pv1_watt')
            | map(attribute='state') | map('float', 0) | sum | round(0) %}
          {{ iif(total >= 20, total, 0) }}
  1. Only if each sensor’s value is greater than 20. Otherwise report 0.
        state: >
          {% set total = expand('sensor.dng0a4702r_pv2_watt', 'sensor.cmg4bd702g_pv1_watt', 'sensor.dng0a4702r_pv1_watt')
            | map(attribute='state') | map('float', 0) | list %}
          {{ iif(total | min >= 20, total | sum | round(0), 0) }}