Help creating template to get min and max values from an attribute of several sensors

Hi All,

I have a number of statistics sensors called 1_week_lounge_temperature, 1_week_kitchen_temperature, etc.

I want to create two template sensors, one to contain the highest value from the max_value attribute of each of the statistics sensors and the other to contain the lowest value from the min_value attribute.

Problem is, I have pretty much no idea where to start :slight_smile:

I could probably create a load of template sensors to where the state is the equal to the attributes and then use Min/Max sensors but there must be a more elegant way.

Anyone got any thoughts?

thanks in advance

I ended up doing it like this - can’t help wondering if there’s not a better way. I don’t like having to manually list all of the sensors to check when there is a distinct pattern to the naming.

  - platform: template
    sensors:
      max_temp:
        value_template: >-
          {% set sensors = [ 
            state_attr('sensor.1_week_lounge_temperature', 'max_value')|float,
            state_attr('sensor.1_week_kitchen_temperature', 'max_value')|float,
            state_attr('sensor.1_week_bedroom_temperature', 'max_value')|float,
            state_attr('sensor.1_week_study_temperature', 'max_value')|float,
            ]
          %}
  
          {{ sensors|max }}
2 Likes

If you don’t list all the sensor entities somewhere (within the template like you’ve done and/or by listing them in the entity_id option) then Home Assistant won’t know which entities to monitor (for state-changes) and the Template Sensor won’t get updated.

So although it may be tempting to iterate through the entities using a loop that constructs each entity’s name (because they follow a naming pattern), that technique will prevent Home Assistant from discovering the entities it must monitor.

1 Like

Ah, ok. I’ve not done much templating before so hadn’t realised that. I’ll leave it as it is.