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
Scripts are placed in a python_scripts folder under the configuration directory, in my case it is /home/homeassistant/.homeassistant/python_scripts. You then need to either restart Home Assistant or click RELOAD SCRIPTS under Configuration, General, Configuration Reloading.
FYI, here is an updated script that has been generalized to be able to set any attribute.
#==================================================================================================
# python_scripts/set_state.py
#==================================================================================================
#--------------------------------------------------------------------------------------------------
# Set the state or other attributes for the entity specified in the Automation Action
#--------------------------------------------------------------------------------------------------
inputEntity = data.get('entity_id')
if inputEntity is None:
logger.warning("===== entity_id is required if you want to set something.")
else:
inputStateObject = hass.states.get(inputEntity)
inputState = inputStateObject.state
inputAttributesObject = inputStateObject.attributes.copy()
for item in data:
newAttribute = data.get(item)
logger.debug("===== item = {0}; value = {1}".format(item,newAttribute))
if item == 'entity_id':
continue # already handled
elif item == 'state':
inputState = newAttribute
else:
inputAttributesObject[item] = newAttribute
hass.states.set(inputEntity, inputState, inputAttributesObject)
Iām looking forward to testing this out as I sometimes find that a couple of my motion sensors will occasionally get stuck in a āmotionā state. I think itās actually that HA hasnāt seen a state change back to āno motionā for whatever reasonā¦ But this would give me a way of forcing the state back within HA I guessā¦?
Thanks for a nifty script! I was thinking to write an appdaemon for the exact same thing but python_script is much better option. It can be used from HA automation .
@rodpayne +1 for a PR. I agree that it should be part of HA. Had I seen this, it would have saved me a great deal of pain recently working out how to use the REST API to set entity attributes.