Home Assistant Python Script to set an entity state

Link to original Notion Page

PropertyValue
Additional KeywordsGithub
URLhttps://github.com/xannor/hass_py_set_state
Sourceinternet
Created2023-02-25T03:08:00.000Z
Already Readfalse
Name Home Assistant Python Script to set an entity state
parentReferences
 

Set State

Home Assistant Python Script to force set an entity state

How it works

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

Installation

HACS

Requirements:

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.

Manual

Copy the Python script in to your /config/python_scripts directory.

Service arguments

Usage

Each call requires at least an entity_id and a state or attributes (otherwise it wont do anything)

example:

entity_id: sensor.random_sensor
state: 0
allow_create: true
 

(Thanks to @rodpayne for the initial script: (https://community.home-assistant.io/t/how-to-manually-set-state-value-of-sensor/43975)

I'm not that used to HACS, but i spotted #1 - so i guess it's better with more details about HACS here.

It would be even better to ask the HACS team to add a disclaimer on the "hacs/settings" page, about missing categories. I guess most users just started using HACS, would like that. But i don't have the time to do that, right now.

Thanks for making the scrip ready for HACS, btw :-)