Problems getting attribute out with a template

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 am trying to extract the attribute current_temperature, using template platform like this:

  - sensor:
      - name: "Hot Water Temperature"
        unit_of_measurement: "°C"
        state: "{{state_attr('sensor.water_heater.dhw_controller','current_temperature')}}"

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.


water_heater is a separate domain from sensor. You seem to have incorrectly assumed it should be prefixed with sensor..

  - sensor:
      - name: "Hot Water Temperature"
        unit_of_measurement: "°C"
        state: "{{ state_attr('water_heater.dhw_controller','current_temperature') }}"

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.

Thanks Troon, sorted now.

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?


I think this is the way to do it but I don’t have an attribute to test it on:

{{ state_attr('water_heater.dhw_controller', 'state_status').state }}

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:

{{ states.water_heater.dhw_controller.attributes.state_status.state }}

If you can’t get that last one to work, don’t even start trying to get the first one to work.

Bracket notation is always better.

Assuming state_status is a top-level attribute (as it appears to be in the screenshot), then you want:

state: "{{ state_attr('water_heater.dhw_controller','state_status')['state'] }}"

If it is nested under status: (as per your first attempt), then:

state: "{{ state_attr('water_heater.dhw_controller','status')['state_status']['state'] }}"

As Rick says, try it out in Developer Tools / Template. Start with:

{{ states['water_heater.dhw_controller']['attributes'] }}

then add bits on like:

{{ states['water_heater.dhw_controller']['attributes']['status'] }}

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.

Further reading:

Thank you both for the explanations. Working now.

1 Like