Ho to extract an attribute value from an entity?

Hello!
could you please help me to extract the temperature from my weather forecast entity?
I have researched before and added the following to my configuration.yaml:

# Temperature from entity
  - platform: template
    sensors:
      norwegian_temperature:
        friendly_name: "weather.dom"
        unit_of_measurement: '°C'
        value_template: "{{ state_attr('weather.dom', 'temperature') }}"

<>

In this example the “weather.dom” is the name of the entity, and “temperature” is the attribute.

Could you please let me know how to fix this so it works?
I’ll be extremely grateful.

{{ states_attr('weather.dom', 'tempersture') }}

3 Likes

Thank you for the answer! I have changed this last line in configuration.yaml, restarted HASS, and nothing has changed :frowning:

Please use the </> button to format your code and paste in the section of the file again.

Your sensor from your code above will be called sensor.norwegian_temperature and will work if you use @Hellis81’s code (with the temperature typo fixed).

If, instead of norwegian_temperature, you use weather_hki, your sensor will be sensor.weather_hki which is as close as you can get. You can’t create a sensor under the weather domain.

1 Like

Thanks Troon! All is formatted now. I was expecting to see a new entity following the whole operation but I can not see anything new :frowning:

# Temperature from entity
  - platform: template
    sensors:
      norwegian_temperature:
        friendly_name: "weather.dom"
        unit_of_measurement: '°C'
        value_template: "{{ state_attr('weather.dom', 'temperature') }}"

Is this code under the sensor: header in configuration.yaml? Do you have any other sensors set up and working?

There should only be one sensor: header in the file. See the docs.

So you should have:

sensor:
  # Temperature from entity
  - platform: template
    sensors:
      norwegian_temperature:
        friendly_name: "weather.dom"
        unit_of_measurement: '°C'
        value_template: "{{ state_attr('weather.dom', 'temperature') }}"

Reload, and you should see sensor.norwegian_temperature.

6 Likes

Thank you very, very much!!! You nailed it! :+1: I didn’t have this header in the file (it’s my first sensor).
All the best!

1 Like

Hey there,

This might come off as a stupid question, or one that is common programming knowledge, but I want to be able to extract a subset value from a nested array.
I’m working with a weather forecast entity that includes a 5 day weather forecast and would like to retrieve specific attribute values from it, rather than applying a pre-made weather card.

Here’s the code that I’m working with:

- sensor:
    - unique_id: weather
      state: template
      attributes:
        weather: >
          {% set temp = state_attr('weather.forecast_home', 'forecast')[0] %}
          {{ temp }}

This gives me all attributes and values from the entity:

'forecast') %}

This gives me all attributes and values from tomorrow’s forecast only:

'forecast')[0] %}

How would I retrieve today’s forecast as well as specific subset values from an array?

Here’s what gets displayed:

condition-weather

I got it:

'forecast')[0]['condition'] %}

Thanks.

1 Like