How to get max temperature for today from forecast?

I have OpenWeatherMap integration set up, which has attributes as:

visibility_unit: km
precipitation_unit: mm
- condition: cloudy
  precipitation_probability: 0
  datetime: '2022-12-20T18:00:00+00:00'
  wind_bearing: 16
  temperature: -0.3
  pressure: 1024
  wind_speed: 1.01
  precipitation: 0
- condition: cloudy
  precipitation_probability: 0
  datetime: '2022-12-20T21:00:00+00:00'
  wind_bearing: 123
  temperature: -1.7
  pressure: 1021
  wind_speed: 1.87
  precipitation: 0

Now I want to filter the array “forecast” on datetime = today (datetime.strftime(%f) == now().strftime(%f) would do), select temperature, and get max value.

This is not only for this specific date, but to learn how to work with arrays where you want to filter on one property, select another, and then run some function on it to return single value. I will then use this logic on different sensors than weather, too.

In C# LINQ, I’d do something like => item.datetime.Date == DateTime.Today).Select(item => item.temperature).Max(),
but with templates/jinja2, I’m lost.

1 Like

Maybe I’m wrong but it seems to me that the max temperature for today is already populated

{{ states.sensor.openweathermap_forecast_temperature.state }}

That will not help you if you need something else from the forecast section but it’ll help you with that specific “Today’s maximum temperature” issue.

I believe @Olivier1974 has a point, else it is

{{ state_attr('weather.openweathermap', 'forecast')| map(attribute='temperature') | list | max }}

forgot the date component

{{ (state_attr('weather.openweathermap', 'forecast')|selectattr('datetime', 'lt', (now().replace(hour=23,minute=59)).isoformat()))| map(attribute='temperature') | list | max }}

Thanks @Olivier1974 , that does it for the max value, wihch is good for this case. And I really appreciate @vingerha 's code, as that allows me to do a lot more stuff now, thank you both :+1:

1 Like

Perfect. Made my day!