Getting values set using hass.states.set into the entity object

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:

  1. 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.
  2. 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 the sensor 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:

  1. 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.
  2. 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.

Why do you care so much? You are not actually supposed to “force” the state of a sensor.
That’s really a corner state. In 99.99% of the cases, the state is derived from the native_value.

Or are you actually trying to “fix” that corner case for the “variable” integration?

I think you should read this bit of the developement docs in how to restore sensor state on reboot.

It is because I have a service that is using hass.states.set to update a Variable Sensor and I can’t figure out how to update the Sensor a different way. This service is defined in __init__.py and not in sensor.py so I can’t sort out how to call a function in the Variable class.