Replies: 1 comment 1 reply
-
Did you try to use the switch.turn_on() and switch.turn_off() functions? Like this:
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everybody,
've declared a zigbee relay in HAS that works perfectly with the button on the Dashboard.
I'd now like to be able to change its state (turn_on / turn_off) with Pyscript.
I have no problem retrieving the switch's current state, but I can't change it.
When I try to change it, the state changes the dashboard but the relay doesn't change state.
« state.set() » doesn't seem to be the right function .
Thank you very much for your help.
Here's my HAS and Python codes :
Automation HAS :
alias: TEST_Python
description: ""
trigger:
seconds: "10"
condition: []
action:
data:
value: 12345
text: ESSAI
mode: single
==================
Python code
conso1 = "sensor.0xa4c138eb3afb5266_power_a"
switch = "switch.0xa4c13830b9134658"
last_conso = state.get(conso1)
@service
def get_conso ( **kwargs ):
global last_conso
current_value = kwargs[ "value" ]
txt = kwargs[ "text" ]
current_conso = state.get(conso1)
current_switch = state.get(switch)
if current_switch == "on":
state.set(switch, value="off", new_attributes=None)
else:
state.set(switch, value="on", new_attributes=None)
message = f"La conso est passé de {current_conso} à {last_conso} data = {current_value} {txt} {current_switch} ."
service.call( "persistent_notification" , "create" , title= "La conso a changé" , message=message)
last_conso = current_conso
Beta Was this translation helpful? Give feedback.
All reactions