I am calling an API that returns weather data including a list of the wind speed forecast for each of the next 48 hours. I am interested in finding the maximum wind speed and its time and base some automations on that value.
I tried saving each of the hourly data objects, which would give me an object with the data I needed but I got a warning about the state exceeding 255 characters for each of the objects
so I created a sensor ‘owm_max_speed’ . In an ideal world for me, this would have 2 attributes, ‘speed’ and ‘time’.
In my sensors.yaml I have:
owm_max_speed:
friendly_name: "Max Wind Speed"
value_template: >-
{% set hour_one = state_attr('sensor.owm_report', 'hourly')[0] %}
{% set hour_two = state_attr('sensor.owm_report', 'hourly')[1] %}
## snip out redundant ##
{% set hour_ten = state_attr('sensor.owm_report', 'hourly')[9] %}
{% set hours = [hour_one, hour_two, hour_three, hour_four, hour_five, hour_six, hour_seven, hour_eight,hour_nine, hour_ten] %}
{% set ns = namespace(max_hour=hour_one) %}
{% for hour in hours -%}
{% if hour.wind_speed > ns.max_hour.wind_speed -%}
{% set ns.max_hour = hour %}
{%- endif %}
{%- endfor %}
{{ ns.max_hour.wind_speed }} @ {{ ns.max_hour.dt|timestamp_custom ('%-I:%M %p') }}
This gives me a string that is fine for display, but I need the speed value separately as I want to trigger events based on that speed crossing above or below a certain value.
Is it possible to create an object that has both of those values?
Or if I need to create a separate sensor, one for display and one for the automation value. And if so, do I need to create the identical list and for loop for each one?
Also if there is a better suggestion on a way to create the list I iterate through, that would be welcome. I do appreciate code improvement comments.