In the past I used a switch to turn the lights on and off, and the bulbs are smart ZigBee bulbs. Of course they didn’t like that the mesh repeater (another bulb) got turned off and so they often lost reception.
Now I have Shelly 1pm in the switches. A few days this worked flawlessly as before. The shellys just did output what the input said.
So I wanted to change to detached mode. This allows more configuration. My idea is:
- If the switch wasn’t changed in the last second, toggle the state. If it is on in any way, turn it off. If it was off, do a reasonable light.
- If the switch already changed shortly before, change to next possible state. Bright, red, dark, whatever.
So I did a helper for the states. The states are named like the scenes.
helper input_select.flur_eg_zustand:
scene.flur_eg_aus
scene.flur_eg_mittel
scene.flur_eg_hell
Then I did an automation that actives the scene if the helper changes.
alias: Flur EG aktivieren
description: ""
triggers:
- trigger: state
entity_id:
- input_select.flur_eg_zustand
conditions: []
actions:
- action: scene.turn_on
target:
entity_id: "{{ states('input_select.flur_eg_zustand') }}"
mode: single
This works wonderful.
Now I wanted to implement the logic I explained above.
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() > state_attr('automation.flur_eg_schalter','last_triggered')
+ timedelta(seconds=1) }}
enabled: true
- condition: not
conditions:
- condition: state
entity_id: input_select.flur_eg_zustand
state: scene.flur_eg_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
That value template which checks if the switch was toggled in the last second works fine when tested in the developer tools. It also works fine when tested by the menu in the automation editor. But switching the light switch and looking at the traces, that template always returns false!
Does anyone have an idea how to repair this?
Please note that the “next” logic works fine if the light is in state “aus” (off) already. It doesn’t matter if the switch was toggled more or less than a second ago, as the first entry after “aus” is the one the I define as “reasonable light” so that “next entry” and “first entry after aus” is the same.
Edit: OK, I got it. Of course the “last_triggered” state of the switch is “now” if it gets toggled. I need the one before or maybe just use the last state change of zustand.
The template is now fine I think:
{{ now() > states.input_select.flur_eg_zustand.last_changed + timedelta(seconds=1) }}