Querying an Attribute- Templating Question

Hello all,

I have what I believe is a pretty cut and dry question about querying for a specific value when multiple are presented by a sensor.

I am attempting to pull information out of a forecast by using the states page along with the templating tab in the developer. Could someone show me an example of how to get 6-26-2022s temp low with a templating statement.

For instance to get the temperature, I would use:

{{states.weather.home.attributes.temperature}}
temperature: 87
humidity: 49
pressure: 30.03
wind_bearing: 210.7
wind_speed: 3
forecast:
  - condition: partlycloudy
    precipitation: 0.43
    temperature: 100
    templow: 76
    datetime: '2022-06-26T16:00:00+00:00'
    wind_bearing: 248.5
    wind_speed: 16.2
  - condition: cloudy
    precipitation: 0.06
    temperature: 90
    templow: 74
    datetime: '2022-06-27T16:00:00+00:00'
    wind_bearing: 19.2
    wind_speed: 13
  - condition: cloudy
    precipitation: 0
    temperature: 88
    templow: 66
    datetime: '2022-06-28T16:00:00+00:00'
    wind_bearing: 37.9
    wind_speed: 10.4
  - condition: sunny
    precipitation: 0
    temperature: 92
    templow: 73
    datetime: '2022-06-29T16:00:00+00:00'
    wind_bearing: 262.7
    wind_speed: 7.6
  - condition: sunny
    precipitation: 0
    temperature: 96
    templow: 72
    datetime: '2022-06-30T16:00:00+00:00'
    wind_bearing: 179
    wind_speed: 9.4
attribution: >-
  Weather forecast from met.no, delivered by the Norwegian Meteorological
  Institute.
friendly_name: Home

The entity is weather.home in this instance if it matters

But how do I get the 6-26-22 templow with the same concept (it’s a level “deeper” and I don’t really know how to get it. Thank you all so much for your guidance!!-- And really, I guess what I mean since this date will change as days go on, how can I get the first “templow” in the list each time. I don’t really want to query an exact date after thinking about it, I would prefer it query the first “templow” in the list.

I’m a little confused because you say you want the first templow, but you use 6-27-22 as an example, which is actually the second one.

In any case, try:

{{ states.weather.home.attributes.forecast[0].templow }}
1 Like

You could do that but you would be ignoring this warning:

It would be much better to do this:

{{ state_attr('weather.home', 'temperature') }}

{{ state_attr('weather.home', 'forecast')[1].templow }}

EDIT: beaten by Phil while mucking around looking for that warning.

I was going to mention about using state_attr, but if the entity or attribute doesn’t exist, the statement still wouldn’t work, because then it would be trying to index a value of None (which is the value returned by state_attr in that case.)

EDIT: If you wanted to handle the error case, you could do something like this:

{% set fc = state_attr('weather.home', 'forecast') %}
{{ fc[0].templow if fc else 'unknown' }}
1 Like

@pnbruckner Thank you for catching that mistake. I have updated my original post to reflect your catch on my mistake in wording.

And what you have showed me is exactly what I was after. Thank you for this, this was the first instance I had ran into wanting to pull out a piece of data that was a level deeper than the “main” attribute level.

Also, I’m sorry also for my terminology, I don’t even know the right way to talk about this stuff yet. As you can tell I’m sure, I’m very new to home assistant and feel like I am drinking water through a fire hose.

Thank you again @pnbruckner

Ahh, thank you for bringing my attention to this, I will make sure to do this from now on (and change some of my past messes within my home assistant setup)

haha. No problem. Thank you all for all of the help you have given me. Maybe one day I will be proficient at least at the level I am comfortable with. For now though, my wants are greater than my abilities with home assistant. Thank you all again!!

2 Likes