Help please - Trying to extract Forecast data into sensors

Hi,
I’m pretty new to HA, and I’d like to extract data from the Forecast and put it in sensors.
So, I managed to make it work in the Developer Tools with this:

{% set today = now().day -%}
{% for i in state_attr('weather.maison', 'forecast') -%}
{% set jour = as_timestamp(i.datetime) | timestamp_custom('%d') | int -%}
{{ jour }}
{% if jour == today %}
Condition_0d: {{ i.condition }}
Temperature_0d: {{ i.temperature }} °C
Temperature_basse_0d: {{ i.templow }} °C
Precipitation_0d: {{ i.precipitation }} mm
Precipitation_prob_0d: {{ i.precipitation_probability }} %
Vitesse_vent_0d: {{ i.wind_speed }} km/h
{% elif jour == today+1 %}
Condition_1d: {{ i.condition }}
Temperature_1d: {{ i.temperature }} °C
Temperature_basse_1d: {{ i.templow }} °C
Precipitation_1d: {{ i.precipitation }} mm
Precipitation_prob_1d: {{ i.precipitation_probability }} %
Vitesse_vent_1d: {{ i.wind_speed }} km/h
{% elif jour == today+2 %}
Condition_2d: {{ i.condition }}
Temperature_2d: {{ i.temperature }} °C
Temperature_basse_2d: {{ i.templow }} °C
Precipitation_2d: {{ i.precipitation }} mm
Precipitation_prob_2d: {{ i.precipitation_probability }} %
Vitesse_vent_2d: {{ i.wind_speed }} km/h
{% elif jour == today+3 %}
Condition_3d: {{ i.condition }}
Temperature_3d: {{ i.temperature }} °C
Temperature_basse_3d: {{ i.templow }} °C
Precipitation_3d: {{ i.precipitation }} mm
Precipitation_prob_3d: {{ i.precipitation_probability }} %
Vitesse_vent_3d: {{ i.wind_speed }} km/h
{% elif jour == today+4 %}
Condition_4d: {{ i.condition }}
Temperature_4d: {{ i.temperature }} °C
Temperature_basse_4d: {{ i.templow }} °C
Precipitation_4d: {{ i.precipitation }} mm
Precipitation_prob_4d: {{ i.precipitation_probability }} %
Vitesse_vent_4d: {{ i.wind_speed }} km/h
{% endif %}
{% endfor %}

So first, how can I get from that to defining sensors ?
And second, as forecast changes from one day to another, will these sensors update in time with the forecast ?

I tried to get it in the configuration.yaml, but it failed.

Thanks a lot in advance

I think you will want to create a template sensor, examples here in the documentation. To you second question, ways to update a template sensor as documented here as well:

The biggest problem you are going to have with the way you are trying to set it up is that states are always limited to 255 characters.

I’m not sure why you need to create additional sensors if you already have that data available in another sensor.

what are you trying to accomplish?

I’m a bit unsatisfied with the forecast cards (no matter which one): I’m in Canada… a place where snow comes is quite large supply ;). My goal is mainly to extract the precipitation amounts (and all other available data, while I’m at it) from the Forecast data. I know it’s there, because of the Developer Tools (I see all available data there).

The main problem I’m having is to extract all that from the Forecast array . The way I think is the “easiest”, is a For loop in the config.yaml, with a couple of if’s to separate the data per day and type.

From that, the 255 characters limit doesn’t seem to be a problem, because the extracted data will be subdivided in many sensors: (that’s not code, but a mere representation of my goal)
Temperature_0d = “Temperature today” (that’d be a number)
Precipitation_0d = “Precipitation today” (that’d be a number too)
Temperature_1d = “Temperature tomorrow”… and so on.

I’ll post my attemted modifications of my config. yaml in a couple of minutes, as a distinct post… maybe that’ll give you a better idea of what I began (and possibly see the error I made)

Thanks in advance,

If I understand what you want to do correctly…

You can’t use one long template as you did in the dev tools template editor and create many sensors from it.

You will have to create one sensor for each bit of data that you want to extract using it’s own portion of that template:

sensor:
  - platform: template
    sensors:
      temperature_0d:
        name: Temperature 0 Day
        value_template: >
          < the template to extract the temperature_0d here >
      precipitation_0d:
        name:  Precipitation 0 Day
        value_template: >
          < the template to extract the precipitation_0d here >
    etc...

OH, and as an FYI unless you click the reply button in my post or tag me using the ‘@’ symbol and my username I don’t get notified of your reply.

Yes, that’s exactly what I’d like to do.
I tried with a For loop, so here’s what it looks like right now, but the Config Checker at the top right of the screen says there’s a problem… plus all the text from the top of the loop is in green… (so it recognize it as text…??)

sensors: >-
      set today = now().day
      {% for i in state_attr( 'weather.maison', 'forecast') %}
          set jour = as_timestamp( i.datetime ) | timestamp_custom( '%d' ) | int
        
            {% if jour == today %}
                condition_0d:
                value_template: "{{ i.condition }}"
                
                temperature_0d:
                unit_of_measurement: '°C'
                value_template:  "{{ i.temperature }}"
                
                temperature_basse_0d: 
                unit_of_measurement: '°C'
                value_template:  "{{ i.templow }}"
                
                precipitation_0d: 
                unit_of_measurement: 'mm'
                value_template:  "{{ i.precipitation }}"
                
                precipitation_prob_0d: 
                unit_of_measurement: '%'
                value_template:  "{{ i.precipitation_probability }}"
                
                vitesse_vent_0d: 
                unit_of_measurement: 'km/h'
                value_template:  "{{ i.wind_speed }}"

            {%- endif -%}

        {% endfor %}

Also, I don’t think I understand the “>-” at the beginning and “-%” correctly… if you can help me, I’d appreciate it a lot.

Thanks again,

Yes, because you are trying to do exactly what I said you can’t do.

You can’t create multiple sensors from a single template.

you have to create each sensor from it’s own internal template as in my example above.

so lets assume that your weather.maison entity has a few attributes with one of them being “forecast”. And the “forecast” attribute has sub-keys one of which is “temperature”. and another is “precipitation”.

you would need to set each sensor to extract one piece of info from that weather entity like this:

sensor:
  - platform: template
    sensors:
      temperature_0d:
        name: Temperature 0 Day
        value_template: >
          {{ state_attr('weather.maison', 'forecast').temperature }}
      precipitation_0d:
        name:  Precipitation 0 Day
        value_template: >
          {{ state_attr('weather.maison', 'forecast').precipitation }}
    etc...

As you see there is only one sensor per template.

the variables you create (i & today) are only local to the specific template section. they don’t travel outside of that local scope.

for example, let’s say I create two templates in the sensor config:

sensor:
  - platform: template
    sensors:
      temperature_0d:
        name: Temperature 0 Day
        value_template: >
          {% set i = 'temperature' %}
          {{ state_attr('weather.maison', 'forecast').i }}
      precipitation_0d:
        name:  Precipitation 0 Day
        value_template: >
          {% set i= 'precipitation' %}
          {{ state_attr('weather.maison', 'forecast').i }}

Those templates are completely different since you have defined i to be two different values in each template. the i in the first template won’t carry down to the second template.

But just be aware that it appears to work differently in the template editor. In that case the variables assigned are still “local” but since the template editor thinks that the template is all one long local template then i can be used many times in the same local template.

The “>” denotes a multi-line template. It lets the jinja interpreter know that the template spans multiple lines so it ignores newlines, etc.

If you only ever use a template in a single line trhen you don’t use that notation and instead use the quote method.

these are equivalent:

value_template: >
  {{ state_attr('weather.maison', 'forecast').temperature }}

value_template: "{{ state_attr('weather.maison', 'forecast').temperature }}"

the “%” (or more precisely {%…%} ) in the template is used to denote a “non-return value” template section. it’s used when you need to do some operation on whatever is inside the brackets.

As in your template the {% if…%} is an operation/test.

the {{…}} is the actual returned value of the template.

I hope that’s what you were asking.

Thanks, that helped a lot.

After only a couple of tries, based on your (very helpful) comments, I found the way of looking in the array. I just had to add the position in the array, right before the property that I want to extract. For example, see the 0, as the temperature is the first in the array:
value_template: {{ state_attr('weather.maison', 'forecast').0.temperature }}

Here’s what it’s going to look like (in case anyone else is interested in something similar):

sensor:
  - platform: template
    sensors:
      temperature_0d:
        friendly_name: "Temperature today"
        value_template: {{ state_attr('weather.maison', 'forecast').0.temperature }}

      precipitation_0d:
        friendly_name:  "Precipitation today"
        value_template: {{ state_attr('weather.maison', 'forecast').0.precipitation }}

After that, I’ll just need to add a sensor for every data that I want to extract from the forecast, and voilà.

Thanks a lot, couldn’t have done it without you.

4 Likes

The only thing I see is that you will likely get errors since you didn’t wrap your templates in quotes.

Thanks for the feedback, but it’s working now (don’t know about the quotes)… maybe I’m just lucky…
Thanks again