Hi all,
I have been struggling with this for a few days so I cast myself to the mercy of the community.
I am trying to use a template to extract an entity attribute, but nothing I try seems to work. I have tried both the old and new methods, and syntax from every article I can find. Here’s the entity:
I’ve tried different combinations of syntax, but always get “Unknown” as the state for the new entity. The value of current_temperature has changed lots of times whilst I have been trying this.
Any assistance is, as always, gratefully received.
To save time editing the sensor repeatedly in a trial-and-error fashion, you can test out your templates under Developer Tools / Template. Paste in the template ( the bit in {{ ... }}) and it’ll show you the output.
Sorry, I spoke too soon; I also need to get another attribute out of this, but it is nested and I can’t figure out the syntax (again). In the above pic, I need the state_status.state attribute, and have tried:
- sensor:
- name: "Hot Water State"
state: "{{state_attr('water_heater.dhw_controller','status.state_status.state')}}"
- sensor:
- name: "Hot Water State"
state: "{{state_attr('water_heater.dhw_controller','state_status.state')}}"
And a couple of other variations, but none have worked. Is a dotted notation not correct for nested attributes?
Try it out in the template editor in developer tools.
And to make sure you have the right nested attributes you can use this. This isn’t the preferred way to access the attributes but it is what I would use in developer tools to make sure the attribute names and nesting is all correct, so then you only have to worry about is the proper formatting of the state_attr function:
Note that {{ states['x']['attributes']['y'] }}
is equivalent to {{ state_attr('x','y') }}
except that state_attr won’t throw an error if the attribute doesn’t exist.