Automation weather help needed

I have setup a weather component with I would like to use the forecast next day temperature for automation rule. E.g. if next day temperature >20C, then switch on ventilation fan.
I am stuck at which attribute I should use. The component reports attribute:
forecast: [object Object],[object Object],[object Object],[object Object],[object Object]
which is 5 list for the temperature of the next 5 days. the first ‘object’ I presume is date and the ‘Object’ is probably temperature and/or conditions.
Can anyone help setting up a template to extract the first ‘Object’?


The forecast attirbute returns
[{‘datetime’: datetime.datetime(2019, 7, 23, 12, 0, tzinfo=<DstTzInfo ‘Europe/Oslo’ CEST+2:00:00 DST>), ‘temperature’: 20.7, ‘condition’: ‘sunny’, ‘pressure’: 1016.9, ‘humidity’: 77.9, ‘wind_speed’: 6.5, ‘wind_bearing’: 128.8}, {‘datetime’: datetime.datetime(2019, 7, 24, 12, 0, tzinfo=<DstTzInfo ‘Europe/Oslo’ CEST+2:00:00 DST>), ‘temperature’: 23.9, ‘condition’: ‘partlycloudy’, ‘pressure’: 1019.9, ‘humidity’: 77.3, ‘wind_speed’: 5.0, ‘wind_bearing’: 160.0}, {‘datetime’: datetime.datetime(2019, 7, 25, 12, 0, tzinfo=<DstTzInfo ‘Europe/Oslo’ CEST+2:00:00 DST>), ‘temperature’: 26.4, ‘condition’: ‘partlycloudy’, ‘pressure’: 1021.1, ‘humidity’: 59.3, ‘wind_speed’: 11.9, ‘wind_bearing’: 174.5}, {‘datetime’: datetime.datetime(2019, 7, 26, 12, 0, tzinfo=<DstTzInfo ‘Europe/Oslo’ CEST+2:00:00 DST>), ‘temperature’: 28.9, ‘condition’: ‘partlycloudy’, ‘pressure’: 1020.0, ‘humidity’: 52.5, ‘wind_speed’: 7.9, ‘wind_bearing’: 182.8}, {‘datetime’: datetime.datetime(2019, 7, 27, 12, 0, tzinfo=<DstTzInfo ‘Europe/Oslo’ CEST+2:00:00 DST>), ‘temperature’: 28.9, ‘condition’: ‘partlycloudy’, ‘pressure’: 1016.4, ‘humidity’: 49.4, ‘wind_speed’: 10.8, ‘wind_bearing’: 136.3}]

How can I use regex to strip the first temperature or condition? Thanks

I have tried {{ state_attr(‘weather.home’, ‘forecast’) | regex_match(‘temperature’, ignorecase=TRUE) }} in developer tool. But it returns Unknown error rendering template

is the attribute a string or dictionary?

If it is a string…

{% set s = state_attr('x.x','forecast') %}
{{ s | regex_findall_index('(\'temperature\': |\"temperature\": )(\d+\.\d+)',index=0) | last }}

Then change the index to grab whatever day you want. 0 = first temp, 1 = second, etc.

If it’s a dictionary…

{% set d = state_attr('x.x','forecast') %}
{{ d[0]['temperature'] }}

then change the index (the zero) to whatever day you want. 0 = first temp, 1 = second, etc.

1 Like

FWIW, I tried pasting the forecast attribute’s value into template editor (after correcting all the single-quotes) but it’s considered to be malformed. It complains when it encounters the first < character. That’s understandable given the appearance of the first key-value pair:

'datetime': datetime.datetime(2019, 7, 23, 12, 0, tzinfo=<DstTzInfo 'Europe/Oslo' CEST+2:00:00 DST>)

The key is fine but I don’t see how it can ingest that value.

1 Like
{% set d = state_attr('x.x','forecast') %}
{{ d[0]['temperature'] }}

It’s dict. And this works! Can’t believe it was that simple. Thanks all

Are you sure that what you pasted above, allegedly representing the forecast attribute’s value, is correct?

Because that example fails to be interpreted as a valid dictionary when pasted into the Template Editor (even after the quotes are corrected).

May be something to do with how the component was setup and HA recognise them properly?

Yeah pasting that won’t work because he copied it from the attributes page which is outputting the __repr__ of the datetime object.

1 Like

Thanks! I didn’t know it worked like that.

I was puzzled by the discrepancy. It was mentioned that the attribute appears as [object Object],[object Object], etc which means it interpreted it as a data structure. Yet when the attribute’s value is pasted into Template Editor, it’s a string. Now I understand why that happened.

One more question: According to
A filter | is used to set priority of operations in the template. But when I put

{% set d = state_attr(‘x.x’,‘forecast’) %} | {{ d[0][‘temperature’] }}

It actually print the filter seperator with the temperature as well? Why is that?

Yah, it’s def weird. It appears that we overrode the __repr__ for datetime to add the word datetime(x, x, x) but we didn’t override timezone and other items inside the datetime. I’m not sure why we even did that when the normal repr is good enough. It just returns the datetime string. I guess that might confuse people though (as to what type it is).

That’s not how this works. If you want it in one line…

{{ state_attr('x.x','forecast')[0]['temperature'] }}

Filters can only be placed inside the jinja. I.E. between {{ }} and {% %}. Also there’s only a specific set of filters that are do-able. For that you need to reference the jinja documentation. You can’t just start making up random filters or adding 2 lines of code together.

1 Like