I’ll start with the questions first. I’m hoping to sort out either:
-
How to deal with value updates using hass.states.set (aka. only updating the
state machine state
) so that they are somehow stored into the entity object so they persist on HA restart? -
From method 4 below, how from __init__.py can I call a function in the Sensor class to update the value instead of calling hass.states.set?
Thank you in advance for any assistance you can provide.
The full background:
I had posted a very similar question in March 2022 with no response, but I still have not solved it so I’m asking a slightly updated question again with hopes of getting some assistance. I apologize for the long post.
I am working on Wibias/hass-variables and am having continued issues handling the differences between the native_value and state in the Entity Object as well as the state in the State Machine. I’m going to use the term value in order to distinguish between sensor state
, sensor native_value
and state machine state
.
I’m not quite sure what my best approach should be when looking to update the value of the sensor. There are 4 different ways to update the value of a sensor and the goal is that they all work and the last set value is retained even on HA restart.
These 2 methods are working ok:
- On sensor creation, the initial value is set to
sensor native_value
is set along with device_class, native_unit_of_measurement, and state_class. - Calling the variable.update_sensor service defined in sensor.py to change the value. I’m still trying to determine whether to set the new value to the
sensor native_value
or thesensor state
but I may add both options to the service and leave it up to the user update one or the other.
These 2 methods are not working as desired:
- When updating the value in the Developer Tools, States, I believe it updates it using hass.states.set and only changes the
state machine state
. - There is a legacy service variable.set_entity (as well as variable.set_variable) defined in __init__.py to update the value. This is also using hass.states.async_set and only changes the
state machine state
.
When the value is only changed in state machine state
, on HA restart, the value is not the same as what it was before it is restarted.