How to get max value in sensor attribute array

Hi,

I am trying to find out the max expected (forecast) temperature in the next ~5 hours.

I know I could create 5 template sensors (examples below) and use min_max to get it, but there must be a more elegant way:

{{ state_attr('weather.pirateweather','forecast')[0].temperature }}
{{ state_attr('weather.pirateweather','forecast')[1].temperature }}
{{ state_attr('weather.pirateweather','forecast')[2].temperature }}
{{ state_attr('weather.pirateweather','forecast')[3].temperature }}
{{ state_attr('weather.pirateweather','forecast')[4].temperature }}

The above return:

15.4
16.8
17.9
18.8
19.4

Can anyone advise how I can create a single template sensor to get 19.4?

{{ state_attr('weather.pirateweather','forecast')[:5] | map(attribute='temperature') | list | max }}
1 Like

wow - fast response. Thanks, @petro!

…is there an idiot’s guide to learning about things like [:5]?

it’s slicing a python list. It says “Give me all values before the 5th index”

1 Like