Hey All.
I have a number of dumb lights that are connected to smart switched (Flashed with Tasmota). Some of these lights I have setup to automatically turn off after ‘n’ minutes. So easy to do!
However, the FAI (Family Appreciation Index) on some of these automations is not high.
Case in point:
- Pantry light goes off after 5 minutes… history of the light being left on in the pantry.
- For 99% of the time that is fine, but like today, my home schooling son needs to do a food based ‘science experiment’ and does it in the pantry so as not to destroy the marble kitchen counter tops. Good boy!
- The experiment takes 40 minutes which involves 8x turning the light back on. Not impressed.
I was thinking, perhaps I could create a ‘lock’ of sorts that allowed the user to lock the light on so it does not switch off after the 5 minutes, but I don’t want to define special value entities or timers for this. I would like to use some multi entrant, single automation foo, which would be great so I could create a blueprint!
My light’s are defined using the platform:switch
definition like this:
- platform: switch
# Pantry
name: Pantry
entity_id: switch.deta_6911ha_effc79_1sw
The switches are multi press capable (They are MQTT connected), so thought I could try to make a double press, lock the light on… but how? What is the simplest way of doing that? Is it possible in a single automation?
My current automation is:
- id: "16249657234656565"
alias: Hall Light - Turn of after 5 Minutes
description: ""
trigger:
- platform: state
entity_id: light.pantry
to: "on"
condition: []
action:
- delay:
hours: 0
minutes: 5
seconds: 0
milliseconds: 0
- service: light.turn_off
target:
entity_id: light.pantry
data: {}
mode: restart
So switching the light
on/off also turns the associate switch
on/off.
I know it could be done using:
trigger: state
light on for 5 minutes
action:
turn off light
but that was the way it was coded back in the day… you live and you learn!
But how can I have this timer going, except when I have double clicked the switch.
When the switch button is pressed, it turns toggles the light
as there is a direct connection to the relay. However if you double tap the button it does not toggle the light, but it does sens a json MQTT message with the type of press:
{TRIG: DOUBLE}
on a separate topic for the switch: stat/tasmota_EFFC79/BUTTON1T
So I coded up the following automation, which triggers on both the light switching on AND the MQTT trigger for the event:
- id: "16249657842342345"
alias: Pantry Light - Turn off after 5 Minutes
description: ""
trigger:
- platform: state
entity_id: light.pantry
to: "on"
- platform: mqtt
topic: stat/tasmota_EFFC79/BUTTON1T
value_template: "{{ value_json.TRIG }}"
payload: DOUBLE
and that captures both triggers, then I
- Switch on the light becasue the double press would not have done that yet (only sent the MQTT message) and the single press would have already done that, but no probs sending it again in that case. On is On!
- Do the delay… irrespective of if it is a double press or not
- Now conditionally trigger IF the trigger was the single press turn on… or actually, exit itf it was the double press!!
- Switch of the light
action:
- service: light.turn_on
target:
entity_id: light.pantry
- delay:
hours: 0
minutes: 5
seconds: 0
milliseconds: 0
- condition: trigger
id: 0
- service: light.turn_off
target:
entity_id: light.pantry
data: {}
mode: restart
And use mode: restart
so that if the light was switched off manually and back on during the wait, it would start again…
Steal Underpants… something something… profit!
Except… it does not work!
If the trigger is the first light on
trigger, it works fine… light goes on, trigger is called, timer, off…$$$
But, if I double press:, trigger is caught, light goes on… and because the light went on, the automation is called again (mode:restart!), but now with the trigger being the first ‘light on’ trigger, it means the timer is called and the light turns off after 5 minutes! Aaargh!
I can’t change to mode: single
as I need the timer to restart if the light is switched off and back on during the delay… And I can’t cancel the automation on a ‘light off’ event.
So how can I get this working with a single automation?
I was thinking that I could change the first trigger to also look for an MQTT message for a single press ({TRIG: SINGLE}
) and then go from there, but then when you turn on the light through another automation, or though the HA UI, the MQTT message does not get sent, so the timeout will not get triggered…
So I am a bit at a loss of how to do it with a single automation, and without using some kind of ‘flag’ variable or timer that I can cancel when the light is turned off.
Anyone?
Cheers
Gil./