Reading out values from a list

Hello everyone,

I’ll first try to explain what I want to achieve and then talk about the way I try to achieve it.

I want my dashboard to change, depending on weather forecast values.
For example: an icon is supposed to show the rain animation and get the rain icon, when rain is coming.

Only when rain is coming, I want the hourly forecast to show up on my dashboard.

However, if it is cloudy, then I do not want to see the hourly forecast but still want the cloud animation and icon.

If it is cloudy and rainy, then rain should get the priority.

This leads me to my first problem: I do not know how to prioritize if-statements.

I have tried to create a template sensor that lists the weather conditions of the next hours.

{% for i in range (8) -%}
{{(state_attr('sensor.new_weather_sensor_hourly',
          'forecast')[i]['condition'])}}
  {% if state_attr("sensor.new_weather_sensor_hourly",
          "forecast")[i]["condition"] == 'rainy' %}   
   test
  {% endif%}
{% endfor -%} 

My second problem: That does not really help me though as I don’t know how to access the values from the for loop and check for “rain”, “snow” and so on. Also, ‘test’ is printed every time ‘rainy’ comes up in the list.

With namespace I only found information about numbers.

When creating a list, I do not know what to filter for as the [condition] part is not a state so this format won’t work:

state: "{{ **states** | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | list | count }}"
{% set my_list = [ (state_attr('sensor.new_weather_sensor_hourly',
          'forecast', ) [0] ['condition']),  
(state_attr('sensor.new_weather_sensor_hourly',
          'forecast', ) [1] ['condition']),
(state_attr('sensor.new_weather_sensor_hourly',
          'forecast', ) [2] ['condition']),
(state_attr('sensor.new_weather_sensor_hourly',
          'forecast', ) [3] ['condition']),
(state_attr('sensor.new_weather_sensor_hourly',
          'forecast', ) [4] ['condition']),
(state_attr('sensor.new_weather_sensor_hourly',
          'forecast', ) [5] ['condition']),
(state_attr('sensor.new_weather_sensor_hourly',
          'forecast', ) [6] ['condition']),
(state_attr('sensor.new_weather_sensor_hourly',
          'forecast', ) [7] ['condition']),          
(state_attr('sensor.new_weather_sensor_hourly',
          'forecast', ) [8] ['condition']) ] -%}

Does anyone know of a good way to get prioritized information from my forecast sensor in another sensor that I can then base decisions on for my dashboard? This has cost me hours so far and I have no clue what direction to go now…

I appreciate the help :slightly_smiling_face:

Hello,
I do not know if it helps to, but one idea what I use.

I created helper input_number here I have number of hours for example 24 = 1Day to future.

I created template with sensors that are updated when ha start and change forecast.
The sensor keeps count of hours with rain where precipitation is greater then 1.5.

if the sensor = 0 means no rain comming and <> 0 will be rain. Similar you can use for snow.

template:
  - trigger:
      - platform: state
        entity_id: weather.pirateweather
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    action:
      - action: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.pirateweather
        response_variable: result
    sensor:
      - name: >
          {{ 'Rain in ' }} {{ states('input_number.observer_weather_hours') | int }} {{ 'hours' }}
        unique_id: weather_rain_inXXhours
        state: >
          {% set CheckHours = (states('input_number.observer_weather_hours') | int) %}
          {{ result['weather.pirateweather'].forecast[:CheckHours] | selectattr('condition', 'eq', 'rainy') | selectattr('precipitation', 'gt', 1.5) | list | count }}
      

After that you can use it this way

{%  if states('sensor.weather_rain_inXXhours') == 0 %}

or you can create another sensor that will be implement your logic an you can simple use the logic sensor in dashboard

I hope that it helps somehow.

Thank you for your reply.
I tried copying your sensor but cannot get it to work. I already have a sensor where I save my weather.get_forecasts response. But neither with that sensor, nor with the original weather sensor, I get a result.

  - trigger: 
     - platform: state
       entity_id: weather.stutensee_h
     - platform: homeassistant
       event: start
     - platform: event
       event_type: event_template_reloaded
    action:
     - action: weather.get_forecasts
       data: 
         type: hourly
       target:
         entity_id: weather.stutensee_h
       response_variable: result
    sensor:
     - name: >
         {{ 'Rain in '}} {{states('input_number.input_number24') | int }}
       unique_id:  weather_rain_inXXhours
       state: >
          {% set CheckHours = (states('input_number.input_number24') | int) %}
          {{ result['weather.stutensee_h'].forecast[:CheckHours]  | selectattr('condition', 'eq', 'rainy') | selectattr('precipitation', 'gt', 1.5) | list | count }}

The state of my sensor is always 0. I changed the sensor to respond to “cloudy” but even then I had no results. Did you have a similar problem?

My input number starts a min value 0 and max value 23. It is increasing 1 step at a time. According to the HA documentation, that should be enough that it works in the code for the sensor I think?

It’s not clear what you actually want your template to return… but you may not need a loop.

You prioritize if/then by the order you put them in reading top to bottom:

{% set forecasts = state_attr('sensor.new_weather_sensor_hourly', 'forecast') %}
{% set conditions_8hr = (forecasts|map(attribute='condition')|list)[:9] %}

{% if conditions_8hr is search('snow') %}
snow
{% elif conditions_8hr is search('rain|pour') %}
rain
{% elif conditions_8hr is search('cloud') %}
cloudy
{% else %}
clear
{% endif %}

This looks like it might be what I am going for and seems easy to adapt for other things I’d like to do. I had not heard about “map” and “search” before.

I will try it out and get back to you. Thanks for taking the time to reply!

Hello,
I see that you have solution for you. I’ll try explain why you probably see results 0.

I used:

| selectattr('precipitation', 'gt', 1.5)

for filter precipitation it means that rain have to be more than 1.5mm in hour. If you remove mentioned part it change results.

The sensor seems to work the way you suggested. Thank you very much for that.
Could you help me out with math functions as well?

If I list the precipitation_probability attribute, I’d like to only activate my dashboard card if the probability is above say 15%.
According to this page I’d need either gt(a,b,/) or simply “probability > 15”.

{% if (probability > 15) %}

problem:

TypeError: ‘>’ not supported between instances of ‘list’ and ‘int’

or

{% if gt(probability,15,/) %}

problem:

TemplateSyntaxError: unexpected ‘/’

The rest of the incomplete template editor for completeness:

{% set forecasts = state_attr('sensor.new_weather_sensor_hourly', 'forecast') %}
{% set probability = (forecasts|map(attribute='precipitation_probability')|list)[:9] %}

{% if gt(probability,15,/) %}
snow

{% else %}
clear
{% endif %}

edit: I am assuming I probably have to declare somewhere, that “probability” is an int or a float?

AFAIK, that is not valid syntax.

Your variable probability is a list which contains 9 values. Which of those values do you want to compare to 15?

The smallest one? {% if probability|min >= 15 %}

The largest one? {% if probability|max >= 15 %}

The average of the 9 values? {% if probability|average >= 15 %}

Something else?

Once you have selected a method, you could use a the gt test (which is what I think you were trying to do) as follows:

{% if probability|median is gt(15) %}

If you need that at least one forecast have probability greater than 15 you can modify sensor value. I used precipitation so you can substitute precipitation by precipitation_probability

Modify sensor this way

{{ result['weather.stutensee_h'].forecast[:CheckHours]  | selectattr('condition', 'eq', 'rainy') | selectattr('precipitation_probability', 'gt', 15) | list | count }}

You should see count of rainy forecast where probability is greater then 15%.