Template average

my template does not work any idea what i am doing wrong ?

 - platform: template
   sensors:
     vochtshakelaaraan:
       friendly_name: "vochtshakelaar aan"
       value_template: "{{ ([states.sensor.sn1_humidity.state, states.sensor.sn2_humidity.state, states.sensor.sn3_humidity.state, states.sensor.sn4_humidity.state, states.sensor.sn5_humidity.state] | average - states.sensor.sn8_humidity.state | float) > 10 }}"
  1. You need to be on 2021.11 for average to work.
  2. average requires numerical values. states are not numerical values, they are strings
  3. You should use expand instead of states.x.y.state, as that will not error when starting up.
  4. You should verify that you have all your states before performing the math
  5. You need to provide default values for when the sensors are unavailable and converting to a float.
       value_template: 
         {% set entities = 'sensor.sn1_humidity', 'sensor.sn2_humidity', 'sensor.sn3_humidity', 'sensor.sn4_humidity', 'sensor.sn5_humidity' %}
         {% set sensors = expand(entities) | map(attribute='state') | map('float', none) | reject('eq', none) | list %}
         {% set sensor8 = states('sensor.sn8_humidity') | float(none) %}
         {% if sensors and sensor8 is not none %}
           {{ sensors | average - sensor8 > 10 }}
         {% else %}
           False
         {% endif %}
1 Like