How to store weather attribute into a sensor? I'm not understanding what I am reading in the docs

Hello all,

Can you all help me with the subject? I’m not very familiar with yaml and not a programmer in general.

This is what I have but it doesn’t seem to be working anymore.

        friendly_name: "Today's High Temp"
        #unit_of_measurement: ""
        device_class: temperature
        value_template: >-
          {% set forecast = state_attr('weather.forecast_home','forecast')[0] %}
          {{ forecast.temperature }}

Below is what I am seeing in developer tools when calling the forecast home weather integration.

  - condition: sunny
    datetime: "2024-06-07T16:00:00+00:00"
    wind_bearing: 297.7
    temperature: 79
    templow: 65
    wind_speed: 9.63
    precipitation: 0
    humidity: 38

Below is what I have but I know it’s not right because I want the value to be loaded to the actual sensor, not to attributes of the sensor.

Can someone show me how to write this properly? Thank you all!!

  - trigger:
    - platform: time_pattern
      hours: /1
      - service: weather.get_forecasts
          type: daily
          entity_id: weather.forecast_home
        response_variable: x
      - name: Today's High Temp
        unique_id: todays_high_temp
        state: "{{ now().isoformat() }}"
          todays_high_temp: >-
            {{ x['weather.forecast_home'].forecast[0].temperature }}
      - name: Tonight's Low Temp
        unique_id: tonights_low_temp
        state: "{{ now().isoformat() }}"
          tonights_low_temp: >-
            {{ x['weather.forecast_home'].forecast[1].templow }}

Worked example:

That sort of looks like my code that I’ve posted several times to load the forecast into a sensor. What exactly are you looking to get? You say you don’t want this in an attribute, which means you want a SINGLE value from a SINGLE forecast entry to be the state of your sensor. What value are you looking for?

You can make the state be whatever you like, for instance if you just want the condition:

state: "{{ x['weather.forecast_home'].forecast[0].condition }}"

It is. haha.

You helped me a little yesterday and I’m still struggling to get all of my weather related stuff working.

Thank you for showing me that. I just didn’t understand if I delete the below out and replace with your idea if that was going to mess things up. I’m pretty sure that date thing is just a place holder for the state of the sensor that doesn’t really do anything.

"{{ now().isoformat() }}"

I think this will help me get the rest of it going possibly. I will know shortly.

Thank you again and I’m sorry that I’m so lost. Essentially I am trying to fix a bunch of broke things in my setup. I haven’t touched the background/ yaml in years honestly.

Thank you again @CO_4X4

Yes, the state as the date/time was just because, for me, I did want it all in attributes and that just kept me informed as to the last time it was updated. You don’t need to use attributes at all if the only thing you are after is a single value.

I completely understand. That makes perfect sense.

Thank you again for helping me over the last few days. Very thankful!

1 Like