ianadd
(ian)
April 28, 2020, 11:28am
1
In Developer Template I put:
{{ state_attr('climate.wts','status') }}
In response I get:
{'setpoints': {'this_sp_from': '2020-04-29T00:00:00+10:00', 'this_sp_temp': 24.0, 'next_sp_from': '2020-04-29T07:00:00+10:00', 'next_sp_temp': 20.0}, 'zone_id': '2113469', 'active_faults': [], 'setpoint_status': {'target_heat_temperature': 15.0, 'setpoint_mode': 'FollowSchedule'}, 'temperature_status': {'temperature': 17.5, 'is_available': True}}
How do I extract each of these sub-items elegantly?
petro
(Petro)
April 28, 2020, 11:31am
2
state_attr('climate.wts','status')['setpoints']['temperature_status']['temperature']
accessing a dictionary stuff = {'item':'value'}
is stuff['item']
returns 'value'
.
So you have to walk through your setup and grab each item from each dictionary. i.e. nested dictionary…
stuff = {'junk':{'crap':'value'}}
stuff['junk']['crap']
returns value
tom_l
April 28, 2020, 11:53am
3
ianadd:
In response I get:
{'setpoints': {'this_sp_from': '2020-04-29T00:00:00+10:00', 'this_sp_temp': 24.0, 'next_sp_from': '2020-04-29T07:00:00+10:00', 'next_sp_temp': 20.0}, 'zone_id': '2113469', 'active_faults': [], 'setpoint_status': {'target_heat_temperature': 15.0, 'setpoint_mode': 'FollowSchedule'}, 'temperature_status': {'temperature': 17.5, 'is_available': True}}
I get an error when trying to parse that JSON though.
petro
(Petro)
April 28, 2020, 11:56am
4
it’s not json, it’s a dictionary from jinja. You can tell by the use of '
instead of "
. JSON requires double quotes for it’s dictionaries. When converted into python/jinja land, those surrounding characters may change.
ianadd
(ian)
April 28, 2020, 12:05pm
5
I get Error rendering template: UndefinedError: ‘dict object’ has no attribute ‘temperature_status’
I see no typos. Your code matches the data but …
petro
(Petro)
April 28, 2020, 12:07pm
6
ah because i made a typo
state_attr('climate.wts','status')['temperature_status']['temperature']
1 Like
tom_l
April 28, 2020, 12:07pm
7
Not with yours, when I try to parse the originally pasted data.
ianadd
(ian)
April 28, 2020, 12:09pm
8
thanks, I just worked it out too.