Just to add, I’m often using ZigBee bulbs behind a switch which is controlled by Shelly in detached mode. This is the reason why the standard methods usually aren’t appropriate in my case. One is supposed to do things using that switch. Not only turn light on and off, but also change modes.
For these I’m searching how to remember that state. But as mentioned not by managing by entity, but in a general way where you don’t have to do things and create more helpers if you add a light.
Currently if you turn on one of my lights by switching the switch, it will go to default mode which is the first state in the input_select list for that light. For every mode there is a scene. If you press it multiple times in less than a second, you can choose which state it should have. The wish is to restore the previous state if the switch was last toggled more than one second ago.
I’d like to generalize my logic for changing the switch state to be unique for all lights, like I did with activation. It makes no sense to have multiple copies of the same script or automation dealing with the light - and if you change one of them, you have to copy it to all of these, or you’ll search bugs forever.
As sample, here my automations for Flur EG:
alias: Flur EG Schalter
description: ""
triggers:
- trigger: state
entity_id:
- binary_sensor.lichtschalter_flur_eg_input_0_input
from: "on"
to: "off"
- trigger: state
entity_id:
- binary_sensor.lichtschalter_flur_eg_input_0_input
from: "off"
to: "on"
conditions: []
actions:
- if:
- condition: template
value_template: >-
{{ now() > states.input_select.flur_eg_zustand.last_changed +
timedelta(seconds=1) }}
enabled: true
- condition: not
conditions:
- condition: state
entity_id: input_select.flur_eg_zustand
state: aus
enabled: true
then:
- action: input_select.select_first
data: {}
target:
entity_id: input_select.flur_eg_zustand
else:
- action: input_select.select_next
data:
cycle: true
target:
entity_id: input_select.flur_eg_zustand
mode: single
alias: Flur EG aktivieren
description: ""
triggers:
- trigger: state
entity_id:
- input_select.flur_eg_zustand
- trigger: state
entity_id:
- light.eg_flur_light_2
from: unavailable
conditions: []
actions:
- action: script.licht_zustand_aktivieren
metadata: {}
data:
schalter: switch.lichtschalter_flur_eg_switch_0
lampe: light.eg_flur_light_2
liste: flur_eg
mode: restart
and the script “zustand aktivieren”
alias: Licht Zustand aktivieren
description: ""
sequence:
- if:
- condition: template
value_template: "{{ is_state(lampe, 'unavailable') }}"
- condition: template
value_template: "{{ is_state(schalter, 'on') }}"
then:
- sequence:
- action: switch.turn_off
metadata: {}
data_template: {}
target:
entity_id: "{{ schalter }}"
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 500
- action: switch.turn_on
metadata: {}
data_template: {}
target:
entity_id: "{{ schalter }}"
- delay:
hours: 0
minutes: 0
seconds: 0
milliseconds: 250
enabled: true
- if:
- condition: template
value_template: "{{ is_state(schalter, 'off') }}"
then:
- action: switch.turn_on
metadata: {}
data_template: {}
target:
entity_id: "{{ schalter }}"
- action: scene.turn_on
target:
entity_id: >-
{{ 'scene.' ~ liste ~ '_' ~ states('input_select.' ~ liste ~ '_zustand')
}}
fields:
schalter:
required: true
description: Steuert Strom für ZigBee-Licht
lampe:
required: true
liste:
required: true