I don’t get it. The sensor’s state is clearly 0 (int). But state_attr()
gives me None, as if not existing.
Please enlighten me.
I don’t get it. The sensor’s state is clearly 0 (int). But state_attr()
gives me None, as if not existing.
Please enlighten me.
state
is a property of an entity’s State Object. It is a string like 0
or on
or Sunday
.
attributes
is also a property of the State Object. It is a dictionary (i.e. contains key-value pairs) and can store additional, common information like friendly_name
and optional information, specific to the entity. For example:
{ "friendly_name": "Kitchen Temperature", "humidity": "43", "light_level": "39", "battery": "96" }
The state_attr()
function is for acquiring the value of dictionary keys in the attributes property. Using the example above, you can use the function to acquire the value of the attributes key called humidity
and it will return 43
.
Your sensor does not have an attributes key called state
so that’s why state_attr()
reports the value is None
.
Go to Developer Tools > States. Whatever you see in the rightmost column resides in the entity’s attributes property and can be accessed using state_attr()
. The center column represents the entity’s state
property. You can use the states()
function to acquire it (or by using states.sensor.whatever.state
).
Here’s an example of my thermostat. It’s current state
value is cool
. I can use this:
state_attr('climate.thermostat', 'temperature')
to get the value of the temperature
key in the attributes property. You should know that most people abbreviate it and just say ‘get the temperature attribute’.
Thank you so much @123! To read the actual state of my sensor I have to use
{{ states('sensor.dryer_washer_power') }}