Home Assistant Python Script to force set an entity state
This script adds a service that lets you update the state and/or attributes of any entity, similar to the developer tools
Requires python_script:
to be enabled in you configuration
Requirements:
- HACS version 0.9 or higher
- Enabled
python_scripts:
in your Home Assistant configuration - Enabled
python_scripts
as option, in the HACS configuration. (If you missed this, kindly read below!)
You are only able to use this custom repo with HACS, if Python Scripts is enabled in Home Assistant. Also, "
python_scripts" should have been enabled during the HACS setup through "Integrations" in Home Assistant.
If you missed that part, you need to remove HACS, and add it again, in order to find the "Python Script" category! This time, remember to set a checkmark in "Enable python_scripts discovery & tracking":
Find more information about the HACS configuration here and here. If you used the YAML-way to configure HACS, it's a bit easier. Just edit the YAML configuration for HACS. That parts is also mentioned https://hacs.xyz/docs/categories/python_scripts#enable-if-you-used-yaml-to-configure-hacs.
Copy the Python script in to your /config/python_scripts
directory.
key | required | type | description |
---|---|---|---|
entity_id: |
True | string | The full entity id to update |
state: |
False | any | The state value |
state_id: |
False | any | The state of a other entity |
allow_create: |
False | Boolean | Allow the entity to be created if it does not exists (Defaults to false so mis-typed entities do not accidentally get created.) |
...: |
False | any | any additional name becomes or replaces and attribute on the entity |
Each call requires at least an entity_id and a state or attributes (otherwise it wont do anything)
example:
service: python_script.set_state
data:
entity_id: sensor.random_sensor
state: "off"
service: python_script.set_state
data:
entity_id: sensor.random_sensor
state_id: sensor.random_other_sensor
(Thanks to @rodpayne for the initial script: (https://community.home-assistant.io/t/how-to-manually-set-state-value-of-sensor/43975)