In my Configuration.yaml I have 9 switch entitys. They send diffrent RF signals to my Livolo switches. How can I change state of 1-7 to ‘off’ when click on ‘All OFF’? I need only to change the state without performing action
I don’t think you can. That would be like asking how to turn off a physical light switch but prevent the connected light from turning off. It’s just the nature of how switches work.
I do this too, I can control my lights without ha, so I also need to change the state based on a sensor…
You need to use the rest API command, then you can change a state of a switch/light without changing it
But manually in Dev Tools (“States”) I can change state. Maybe there are some way to do this programmatically?
Could you tell in more detail about rest API command?
No, in dev tools it’s not possible, only by python or API call, I will show my command later
yes, you can change the displayed state of the switch in dev tools but you aren’t really changing the switch state. In the state machine the switch is still in the other state. And since the switch state isn’t really changing on the next scan (or whatever the correct terminology is…) it will switch the display back to the real switch state as determined by the state machine.
Maybe the API is your answer for what you want. I’ve never tried it. I’ll be watching to see how that is being done and what the result is.
here are 2 ways todo it
- enable a shell command, then excecute the curl below… note if you want to test from SSH, drop all the \ backslashes, they need to be there in the yaml file to escape the double quotes “”, in ssh its not needed (i have bearer enabled)
link edited:
light1_on: "curl -k -X POST -H \"Authorization: Bearer YOURBEARERPASSWORDHERE\" -d '{\"state\": \"on\",\"attributes\": {\"assumed_state\": true,\"friendly_name\": \"kitchen\",\"icon\": \"mdi:icon\"' https://127.0.0.1:8123/api/states/light.kitchen"
- enable python_script: then drop the .py file in that folder, this enables your service call
#==================================================================================================
# python_scripts/set_statev2.py
#==================================================================================================
#--------------------------------------------------------------------------------------------------
# Set the state or other attributes for the entity specified in the Automation Action
#--------------------------------------------------------------------------------------------------
# service:
# python_script.set_statev2
# {"entity_id": "light.keukeneiland","state":"on","icon":"mdi:door","friendly_name":"test"}
inputEntity = data.get('entity_id')
if inputEntity is None:
logger.warning("===== entity_id is required if you want to set something.")
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)
Thanks. Python script works very well. But what is the right place to call it from? In fronted i can asign only one tap_action and this can be call-service or toogle…
type: entity-button
entity: switch.livolo_all_off
tap_action:
action: toggle
action: call-service
service: python_script.state_change
service_data:
entity_id: switch.light_1
state: 'off'
hold_action:
action: more-info
show_icon: true
show_name: true
you can use it everywhere? its a service like another service?
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…
So my code doesn’t help?
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…
So I can turn on a light even its on…
I have finally done this. Thanks again, Fabio.
…may be useful to someone
Fronted:
cards:
- cards:
- entity: switch.livolo_all
icon: 'mdi:lightbulb'
show_name: false
tap_action:
action: call-service
service: script.lights_turn_state
service_data:
state_copy: 'on'
type: entity-button
- entity: switch.livolo_all
icon: 'mdi:lightbulb-outline'
show_name: false
tap_action:
action: call-service
service: script.lights_turn_state
service_data:
state_copy: 'off'
type: entity-button
type: horizontal-stack
- entities:
- entity: switch.livolo_1
- entity: switch.livolo_2
- entity: switch.livolo_3
- entity: switch.livolo_4
- entity: switch.livolo_5
- entity: switch.livolo_6
- entity: switch.livolo_7
show_header_toggle: true
title: Lights
type: entities
type: vertical-stack
Script:
for performing all_on/all_off actions and copying states to 1-7
lights_turn_state:
alias: On/Off all lights
fields:
state_copy:
description: 'Set state for other switches'
example: 'on'
sequence:
- service: python_script.state_change
data_template:
entity_id: 'switch.livolo_1'
state: '{{ state_copy }}'
- service: python_script.state_change
data_template:
entity_id: 'switch.livolo_2'
state: '{{ state_copy }}'
- service: python_script.state_change
data_template:
entity_id: 'switch.livolo_3'
state: '{{ state_copy }}'
- service: python_script.state_change
data_template:
entity_id: 'switch.livolo_4'
state: '{{ state_copy }}'
- service: python_script.state_change
data_template:
entity_id: 'switch.livolo_5'
state: '{{ state_copy }}'
- service: python_script.state_change
data_template:
entity_id: 'switch.livolo_6'
state: '{{ state_copy }}'
- service: python_script.state_change
data_template:
entity_id: 'switch.livolo_7'
state: '{{ state_copy }}'
- service_template: switch.turn_{{ state_copy }}
entity_id: switch.livolo_all
Automation:
for copying states from 1-7 to all_on/all_off
- id: synchronize.state_switchlight
alias: Synchronize switches
trigger:
- platform: state
entity_id:
- switch.livolo_1
- switch.livolo_2
- switch.livolo_3
- switch.livolo_4
- switch.livolo_5
- switch.livolo_6
- switch.livolo_7
condition:
condition: or
conditions:
- condition: template
value_template: >
{{ is_state(trigger.entity_id, 'on') }}
- condition: and
conditions:
- condition: template
value_template: >
{{ is_state(trigger.entity_id, 'off') }}
- condition: template
value_template: >
{{ is_state('switch.livolo_1', 'off') }}
- condition: template
value_template: >
{{ is_state('switch.livolo_2', 'off') }}
- condition: template
value_template: >
{{ is_state('switch.livolo_3', 'off') }}
- condition: template
value_template: >
{{ is_state('switch.livolo_4', 'off') }}
- condition: template
value_template: >
{{ is_state('switch.livolo_5', 'off') }}
- condition: template
value_template: >
{{ is_state('switch.livolo_6', 'off') }}
- condition: template
value_template: >
{{ is_state('switch.livolo_7', 'off') }}
action:
- service: python_script.state_change
data_template:
entity_id: 'switch.livolo_all'
state: >
{{ states(trigger.entity_id) }}
No problel
Is there way to call service for some entities when using data_template? Something like:
- service: python_script.state_change
data_template:
entity_id:
- 'switch.livolo_2'
- ...
state:
- '{{ state_copy }}'
- ...
Hmm, no idea, not using it for a data template… I just use this code , I not created it myself, just found it in the community here…
I updated the python state_change script to be able to pass several entitys at once.
now:
- service: python_script.state_change
data_template:
entity_id:
- person.one
- person.two
state: 'not_home'
before:
- service: python_script.state_change
data_template:
entity_id: 'person.one'
state: 'not_home'
- service: python_script.state_change
data_template:
entity_id: 'person.two'
state: 'not_home'
SCRIPT:
# EXEMPLE 1
# - service: python_script.state_change
# data_template:
# entity_id: person.one, person.two
# state: 'not_home'
# EXEMPLE 2
# - service: python_script.state_change
# data_template:
# entity_id:
# - person.one
# - person.two
# state: 'not_home'
# EXEMPLE 3
# - service: python_script.state_change
# data_template:
# entity_id: >
# {% set entities = state_attr('group.family_persons', 'entity_id') %}
# {{ states.person | selectattr('entity_id', 'in', entities)
# | selectattr('state', 'eq', 'home')
# | map(attribute='entity_id')
# | join(', ') }}
# state: 'not_home'
if isinstance(data.get('entity_id'), str):
EntityList = data.get('entity_id').split(', ')
else:
EntityList = data.get('entity_id')
for item in EntityList:
inputEntity = item
if inputEntity is None:
logger.warning("===== entity_id is required if you want to set something.")
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
logger.info(inputEntity + ': ' 'state' + '> ' + inputState)
else:
inputAttributesObject[item] = newAttribute
hass.states.set(inputEntity, inputState, inputAttributesObject)
Cool, gonna copy paste for later
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'
As you can see here in my logs:-