Change the result of an entity's state attribute

Nico Writing by Nico
  3 min of reading

This page has been visited ... times

Change the result of an entity's state attribute

In the previous article I showed you how to create an average of states on entities, by making a call on an entity state states() which directly reports the state of this entity. Today is different, I’m going to show you how to work with entity state attributes.

Prerequisites

Home Assistant installed

Box Home Assistant Green on sale at domadoo for less than €100

Box HomeAssistant Green

By Nabu Casa

Box Home Assistant Green on sale at domadoo for less than €100

find all functions of an entity

Nothing could be simpler, go to:

Development tools > states tab, then you search using the filters

finding information about state and state attribute on entity

Info: you can also work on the state attributes of entities directly in the scenarios (automations) of Home Assistant and yes for a short time they have been listed according to the previously selected entity, I find it rewarding.

management of entity attributes directly in a home assistant automation

In short, during the installation at August of thermostatic valves from different brands, I needed to standardize the results of the commands in order to be able to work more easily in the automations.

Template and entity

To create a template, nothing could be simpler, go to home assistant

Parameters/inputs/create-an-input/template/Model-a-sensor

Code explanation

below the code, you do not have to put the tag {{ result }} which is there mainly to read the result of the command .

What I did, I retrieve the state attribute running state of the entity climate.thermostat_salon and I put a condition if the result is heat, the transform into result on otherwise off

the running_state function of a thermostatic module generally responds to Idle or Heat, but in my case I had a thermostatic valve Aqara SRTS -01 which did not have this function so I had to adapt a code and in home-assistant it is largely doable.

Time for code explanations

We save the result of the running_state state attribute

{%- set result = state_attr('climate.thermostat_salon', 'running_state') %}

If the default result is heat

{%- if result == 'heat' %}

We transform it into On {%- set result = 'on' %}

Otherwise Off {%- else %} {%- set result = 'off' %}

{%- set result = state_attr('climate.thermostat_salon', 'running_state') %}
{%- if result == 'heat' %}
{%- set result = 'on' %}
{{ result }}
{%- else %}
{%- set result = 'off' %}
{{ result }}
{%- endif %}

Conclusion

Thanks to this mini-tutorial you will be able to take your first steps in home-assistant templates and transform the results of an entity’s state attributes.

Nico

Nico

Founding member of the Haade site, a home automation enthusiast with all the tricks that go well.

Comments