How to manually set state/value of sensor?

For what it’s worth, I have been hunting for a solution to this problem and I finally decided to make a small script:
# python_scripts/set_state.py

#--------------------------------------------------------------------------------------------------
# Set the state or other attributes for the entity specified in the Automation Action
#--------------------------------------------------------------------------------------------------

inputEntity = data.get('entity_id')
inputStateObject = hass.states.get(inputEntity)
inputState = inputStateObject.state
inputAttributesObject = inputStateObject.attributes.copy()

newState = data.get('state')
if newState is not None:
    inputState = newState
    
newIcon = data.get('icon')
if newIcon is not None:
    inputAttributesObject['icon'] = newIcon

hass.states.set(inputEntity, inputState, inputAttributesObject)

With this script in place, the action could be:

  action:
    service: python_script.set_state
    data_template:
      entity_id: Binary_sensor.sensor1
      state: ON

(This script could also set the icon.)

38 Likes