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 states.weather.openweathermap.forecast.Where(item => item.datetime.Date == DateTime.Today).Select(item => item.temperature).Max(),
but with templates/jinja2, I’m lost.
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
I cannot guess which sensor you use in which card but…
the weather sensor by met.no only delivers one (1) temperature and I am not sure if this is sort-of-current or max, I guess it is current as it shows 12.9
Hence you would need to create the forecast/template sensor as per above link. On top of that you can run a script to extract the max value
I am really grateful for all the answers and tips, but I am not able to create the solution.
Here is a screenshot after I tapped on the card. The sensor is called Home from Met.no and generates the forecast data (hourly or next days).
I have never written a script before and I can’t get any further with the examples from the links.
I understand the challenge but (always a but) knowing how to create Template sensors is sort of an important part when using HA. I also understand the learning curve and will provide some support but as I am travelling till Thu, it will have to wait till later this week (unless someone else steps in)
If ever you want to try before…what needs to be done
create a Template sensor to collect the forecast via service call (above link). The card you show is using a service call to generate the hourly/daily forecast data bit this is only used in the card, hence you need a separate entity to have the forecast data in its attributes
any calculations as to the ‘max’ or ‘min’ or … , for this you need another Template entity which will use the forecast entity content to extract the required value, this reuqires a small jinja script. An alternative is that you use the very same jinja script directly in a card, saves the additional entity. But I am not sure what you want to achive, only 1 max value or a list or graph ?
I have used some time today trying to figure out this my self, and have made this that will give you:
A sensor for daily forecast
A sensor for hourly forecast
A sensor for the highest temperature in the current day
A sensor for the lowest temperature in the current day
Just change weather.hjem for your weather entity and add this in your configuration.yaml
(I’m no expert, so there might be some thing that should be done different, but this works for me)