/config/python_scripts
@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.
I like this very much.
But I get a failure if the sensor is unknown.
: File "set_state.py", line 15, in <module>
: TypeError: 'NoneType' object is not callable
Would it be possible to allow the script to create a new sensor if it does not already exist?
Could you tell us what sensor should be created?
Example: The code below tries to set a sensor âcold_water_rateâ.
If such a sensor already exists in ha, then set_state would change its state to 42.
But if the sensor does not exist, then I get an error. To me it would make sense if set_state could be updated to check for this condition, and if needed automatically create a new sensor for the caller.
Perhaps it is easy, perhaps not, I donât know.
- id: automation 3
trigger:
platform: time_pattern
minutes: '/1'
action:
service: python_script.set_state
data_template:
entity_id: sensor.cold_water_rate
state: 42
Well, I can see a reason in checking that entity does exist before changing its state.
you can use the following code:
inputEntity = data.get('entity_id')
if inputEntity is None:
logger.warning("===== entity_id is required if you want to set something.")
elif hass.states.get(inputEntity) is None:
logger.warning("===== unknown entity_id: %s", inputEntity)
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)
On the other hand, I have no idea what one would create if a sensor does not exist⊠and who would use itâŠ
I donât think would be possible because you wouldnât be providing enough data (ie: attributes) to create the sensor.
Is it possible that the status that you want to give with the set state comes from another sensor?
why not, provided that sensor does existâŠ
I could not use state: sensor.randomsenor
though, but resolved in a different way.
Sir, this works like a charm, thanks heaps! What a nice script !
This is what Iâve done to set a sensor value based on a light switch state:
sensors:
ledstrip_consuption:
value_template: >
{% if is_state('light.led_strip', 'off') %}
0
{% else %}
30
{% endif %}
Hi,
im using your script to set the state of a switch but in the interface, it changed the toggle button for a string that say âtrueâ,
any idea ?
here is the last part of the automation:
- service: python_script.set_state
data_template:
entity_id: switch.shelly1_basement
state: ON
screenshot :
have you tried
state: 'ON'
instead?
and by the way, why donât you use switch.turn_on service?
Yes I tried that. It will show âonâ instead of true.
Iâm not using turn_on because I donât want to turn them on.
The reason is that I have 5 wifi lights that I control with a wifi switch but the switch itself never cut the power to the lights so I can control them at any time. Instead Iâm using mqtt to turn off all lights using the switch. My problem is that if I open one lights. It will not put the state of the switch to on. Does it makes sense?
Iâm afraid it doesnâtâŠ
What is âwifi switchâ and âopen one lightâ?
It would be easier if you described your configuration in more details.
I have one Shelly 1 Switch turning on/off 5 yeelights. Since I want the switch the turn on/off the yeelights and not the power to lights (because objvously if there is no electricity, the lights wont be connected to wifi) iâm using automation to turn on/off the lights if the switch is pressed but not close/opening the circuit. Now, let say, i open one light from home-assistant interface, i want the shelly1 switch to report âonâ but not turn on all other lights.
Is it more clear ?
If I get it right, you use your shelly as a controller.
Iâm not sure itâs a good idea to show the controller itself in the UI - why wouldnât you show appropriate lights instead? Otherwise itâs a lot of fiddling and what for?
We are getting out of focus here,
Im using the switch to turn off the lights, thatâs why i want itâŠ
I am using this script for Insteon Keypad buttons. Insteon Keypad buttons can be linked to another Insteon device and turn that device on or off directly. HA gets a notification that the keypad button was pressed, but does not know explicitly that the other device is now âonâ. The linked device does not report its state immediately to the Insteon modem.
In this case, HA is now in the state where the button is âonâ, and the linked light (or lights) is âonâ but HA still thinks itâs âoffâ (until the next poll).
However, I donât want to simply send another âturn_onâ request, since Insteon messages are serial and slow, and I donât want to clog up the network, especially since some buttons may control 4 other lights. So this script is perfect, it allows HA to be updated to the current status without sending any messages through the Insteon component. Here is how I use it in my automation:
- alias: Kitchen Pendant Keypad Button Turned On
trigger:
platform: state
entity_id: switch.kitchen_keypad_button_2
to: 'on'
from: 'off'
condition:
condition: template
value_template: "{{ is_state('light.kitchen_pendant_lights', 'off') }}"
action:
- service: python_script.set_state
data:
entity_id: light.kitchen_pendant_lights
state: 'on'
brightness: 255