I think you have an error in the command above. You are missing }} at the end of the json string.
After I fixed the json error I tried that method and the result is exactly as it is using the dev-tools state page. It sets the state to the opposite state but then it switches the state back to the correct true state after a few seconds.
then I tried the python script and appears to do exactly the same thing.
At least it does for entities that have a real world feedback mechanism.
It probably (maybe…) works for a stateless device but it doesn’t work for devices that have a state feedback.
Yeah, it’s possible that I copy pasted wrong, I had some variables in that string that I removed, I am using it for my 40 lights, don’t want to have 40 shell commands
Yes, it’s only for changing states, so if you actually press a button or or if you have state feedback later, it will override off course…
But in your first reply, you asked for a service to just change a state for a switch, without actually doing anything like on/off, that’s what this code do?
Or do your switches have a value_template configured?
Because I have configured my lights and switches with an assumed state, you see that in the curl…
Hi,
I have a problem with my SwitchBot controlling my air conditioner. If somebody manually turns on the air conditioner I get out of sync since HA does not know it is operational. I would like to manually change the state of the SwitchBot entry and this script seemed to do it.
My issue is when I sense that the air conditioner is not operational and change the state of the switch in HA, it changes this state back on almost immediately.
Here is my automation:-
- alias: Set airconditioner operational off
initial_state: true
trigger:
- entity_id: sensor.possible_airconditioner_operation
from: 'True'
platform: state
to: 'False'
for:
minutes: 45
condition:
- condition: state
entity_id: switch.air_conditioner
state: 'on'
action:
- data:
message: AUTOMATED CORRECTION Seems like the air conditioner is not operational HA thinks it is!
service: notify.ios_tincan
- service: python_script.set_statev2
data:
entity_id: switch.air_conditioner
state: 'off'
I was miffed that the basic switch didn’t work like mqtt version which has separate mqtt command/set and state. It seems most basic “action/input” entities should support this as HA might be may be just a UI for an otherwise independent process controlled in various ways and by other UI’s. Since my code listens (via websocket api) for changes to this switch setting the switch was causing havoc looping. This script fixed all that and works great calling from via websocket api. I now have a new method for my api for changing just state just like with mqtt version. Thx!!
Well, after more investigation this script didn’t work. Changing the state (no matter in this script or not) triggers an event on the event bus which is picked up in my socket listener for that entity and causes a “double fire”. One wonders if you can disable the event bus temporarily during a state update? I saw some other posts where automations were turning off “homeassistant” then doing the switch state change then turning it back on. Only changing state of switch, no action I guess I’ll try that next.
Gosh, I have exactly the same problem with my airco - what happens when someone triggers it manually. I’m trying to do this with an input boolean and automation in between
I think you need 3 things
Have your switchbot.airconditionner be used only for toggle. Not on/off
Create an input_boolean.airconditonner with on / off
Create another input_boolean.resetairconditonner
Then use automations 3 (!) to:
trigger: input_boolean.airconditonner change from off to on
condition “input_boolean.resetairconditonner” = off
action toggle switchbot.airconditionner
Then identical as above for " change from on to off" on the trigger
Finally automation to reset:
trigger “input_boolean.resetairconditonner” = on
no conditions
action: input_boolean.turn_off for airconditonner
delay 10 seconds
action: input_boolean.turn_off for resetairconditonner
I should have written that in code, but I am useless at writing it - it would take me half a day…
The hold action calls the python_script.set_statev2 with the state of “on”
This works perfectly to update the state to on without sending that IR for when the physical light is on but home assistant it showing it as off.
What I’d like now it to send service_data conditionally based on the current state, something like :
For anybody “newbie as I am” - here’s a write up on how to do this from scratch
In configuration.yaml add only this line
python_script:
Create a folder under “config” called “python_scripts”
It should end up like this: /config/python_scripts
Copy the script of Oligarch (on the right of the gray box, there is a copy symbol. Use that to make sure you all have the good identation, etc. You want to copy the box with “SCRIPT” in the top…
Open notepad, paste that text, and save it as “state_change.py” (and on a windows system, put the quotes around the filename otherwise you’ll end up with a .txt at the end and you don’t want that)
Restart Hassio / Home Assistant
Now you should be good to go to use it. It has created a new service you can call in automations, etc. The best place to test it out is
Developer tools > Services (tab at the top)
In there, the simple two lines of config seen below does the magic of changing the state of my switch called “switch.warmeroffice” to “off” without triggering the automation linked to that.