Tip: reading an attribute from a climate entity

I couldn’t find where this was documented or an example given, even if the template documentation put me on the track, so I thought I’d share my finding.

If you ever need to read (and store) a single attribute from an entity like a climate one, you can do as follows, here with the preset_mode:

service: input_text.set_value
data:
  value: "{{states.climate.my_radiator.attributes.preset_mode}}"
target:
  entity_id: input_text.my_radiator_preset

The other useful thing that helped me and that beginners like me don’t know is the developer tools / template that you will find in your HA instance. Nifty!

I hope this is useful. Enjoy!

2 Likes

Documented tip: Use the states() and state_attr() functions instead of directly referencing an entity’s properties.

This:

  value: "{{ state_attr('climate.my_radiator', 'preset_mode') }}"

not this:

  value: "{{ states.climate.my_radiator.attributes.preset_mode }}"

NOTE

If you wish to store a value, you can also use a Trigger-based Template Sensor. The main advantage over an Input Text is that the value displayed by the Template Sensor cannot be modified in the UI.

An entity’s state property is limited to storing a string not exceeding 255 characters. If there’s a need to store more information and not necessarily just a string, here’s a more sophisticated Trigger-based Template Sensor, able to store multiple values, exceeding 255 characters, whose type can be string, integer, float, list, dictionary, or boolean.

3 Likes

Damn, I had searched!

Thanks for the correction and the extra tips!

2 Likes

Thanks for the info, hard to find stuff about accessing attributes at times.
I used the create a Helper method with a template sensor and it worked too.

value: "{{ state_attr('climate.daikin_ac', 'fan_mode') }}"