I trying to create an automation for when i switch 3 times (on/off/on) in less than 3 sec it trigger an action
sound easy to make but i can’t figure it out.
is it ?
Switch : Shelly1 mqtt
there may be another way but what I would use is a counter and a timer so that on the first flip of the switch to on it starts the 3 second timer and it also increments the counter. Then on every subsequent flip on it increments the counter. And then use the counter value to going to 3 to trigger the automation only if the 3 second timer is not finished.
Here’s an example where if I turn the kitchen light on/off/on, all within 3 seconds, it will toggle the state of the sink light.
I’ve tested it and it works. Just remember that the sequence assumes the light is already off before you turn it on/off/on. So if the light is already on the on/off/on sequence will not be detected.
This solution requires:
- timer
- input_boolean
- automations
- scripts
However, you only need to modify 2 or 3 lines in order to customize it for your needs.
Step 1
Define a 3-second timer and an input_boolean.
timer:
toggler:
duration: '00:00:03'
input_boolean:
toggler:
name: toggler
Step 2
Define 3 automations.
- The first one is for the kitchen light to detect when it changes from
off
toon
. This is the only automation requiring customization. Replacelight.kitchen
with your light. - The other two are for detecting when the timer is started and stopped (and require no customization).
#automation:
- alias: 'toggler'
trigger:
- platform: state
entity_id: light.kitchen
from: 'off'
to: 'on'
action:
- service_template: script.toggler_is_{{states('input_boolean.toggler')}}
- alias: 'toggler timer started'
trigger:
- platform: event
event_type: timer.started
event_data:
entity_id: timer.toggler
action:
- service: input_boolean.turn_on
entity_id: input_boolean.toggler
- alias: 'toggler timer finished'
trigger:
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.toggler
action:
- service: input_boolean.turn_off
entity_id: input_boolean.toggler
Step 3
Define two scripts.
- The first script ends the timer and toggles the state of the sink light. This is the one you must customize. Replace
light.toggle
andlight.sink
with whatever service and entity you require. - The second script starts the timer (and requires no customization).
#script:
toggler_is_on:
sequence:
- service: timer.finish
entity_id: timer.toggler
- service: light.toggle
entity_id: light.sink
toggler_is_off:
sequence:
- service: timer.start
entity_id: timer.toggler
Best accomplished by resetting the counter when the timer expires.
good point. i forgot to add in the part about resetting the counter after 3 seconds.
Use this service:
counter.reset
There’s no need to maintain a running count.
- The first off/on sets a flag (the input_boolean) and starts a 3-second timer.
- The second off/on (if executed within 3 seconds) checks the flag’s state. If the flag is enabled, the task is performed.
No counting required.
Yeah, that’s probably true.
I thought they wanted to be forced to cycle to ‘on’ three times in three seconds. For that they would need a counter.
Reading it again I see it’s just three cycles of the switch in which both on & off count as one cycle. No need for a counter for that.
Great topic.
I would just need a little help (I am not a coder), what I need is just that: if my switch is on and I press switch off in the next 3 seconds, then toggle my lamp. I suppose it is quite obvious but I do not know how to do it.
Thanks for any help
Post what you have and we can help you sort it out.
I have exactly the code you wrote, and it works but I have to:
switch on room lamp - switch off room lamp - switch on room lamp, and then it works and toggles another lamp.
What I would like is:
switch on room lamp - switch off room lamp (within 3 seconds) and then it works and toggles another lamp.
thank you
OK. You’ll be pleased to hear that your requirement can be achieved with a lot less code. Keep the existing 3-second timer and remove everything else (the input_boolean, two scripts, and three automations). You’ll only need two (new) automations.
- When the primary light is turned on the first automation starts a 3-second timer.
- When the primary light is turned off the second automation checks if the timer is active. If it is then the secondary light is toggled.
- alias: 'toggler light on'
trigger:
platform: state
entity_id: light.primary
from: 'off'
to: 'on'
action:
service: timer.start
entity_id: timer.toggler
- alias: 'toggler light off'
trigger:
platform: state
entity_id: light.primary
from: 'on'
to: 'off'
condition:
condition: template
value_template: "{{ states('timer.toggler') == 'active' }}"
action:
service: light.toggle
entity_id: light.secondary
WAO!
thank you so much! I will try tomorrow since it’s late now.
Really, I appreciate your help and your know how.
Thanks again!