Lights: Timed on with a single press, or locked on with a double press [SOLVED]

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

  1. 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!
  2. Do the delay… irrespective of if it is a double press or not
  3. Now conditionally trigger IF the trigger was the single press turn on… or actually, exit itf it was the double press!!
  4. 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./

1 Like

The simplest solution, I think, would be to use something like an input boolean. When the MQTT DOUBLE message is received, turn on a boolean. Then in your automation, check if the boolean is on, and if so, don’t run the timeout. You would also need to automate disabling the boolean when the light is turned off.

If you really want to keep it in one automation, then perhaps you can turn off the automation from within the automation? Something like this:

  action:
  - choose:
      conditions:
        - condition: trigger
          id: 1
      sequence:
        - service: automation.turn_off
          entity_id: automation.16249657842342345
        - service: light.turn_on
          target:
            entity_id: light.pantry
        - service: automation.turn_on
          entity_id: automation.16249657842342345
  - 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

This way the automation won’t trigger itself. You could also tidy it up by putting the delay & turn off within default actions, so that HA doesn’t needlessly run a timer when you want the light to stay on anyway. More details about Choose here: Script Syntax - Home Assistant

*Don’t rely on my indentation, my spacebar is dying so it’s probably all over the place.

Thanks! You got me on the right track with turning the automation off and on!

Here is where it has ended up:

- id: "16249657842342345"
  alias: Pantry Light - Turn off after 5 Minutes
  description: ""
  trigger:
    - platform: state
      entity_id: light.pantry
      to: "on"
      id: "trig_on_single"
    - platform: mqtt
      topic: stat/tasmota_EFFC79/BUTTON1T
      value_template: "{{ value_json.TRIG }}"
      payload: DOUBLE
  action:
    - service: automation.turn_off
      target:
        entity_id: "{{ this.entity_id }}"
      data:
        stop_actions: false
    - service: light.turn_on
      target:
        entity_id: light.pantry
    - delay:
        milliseconds: 100
    - service: automation.turn_on
      target:
        entity_id: "{{ this.entity_id }}"
    - condition: trigger
      id: "trig_on_single"
    - delay:
        minutes: 5
    - service: light.turn_off
      target:
        entity_id: light.pantry
      data: {}
  mode: restart

I did have to put a 100ms delay between switching on the light and re enabling the automation or it would still trigger the automation again.

Thanks @EKC !

G./

1 Like

I have turned it into an Blueprint:

G./

1 Like

What are your Tasmota settings for the Button on the switch? In the Tasmota documentation, they talk about modifiying SetOption1, SetOption11 and ButtonTopic.

Thanks

I did not set anything specific. I used the template for my devices (DETA 1,2,3 gang switches) from blakadder.com and multipress is the default behavior.

Here are the settings reported by the devices:

CMD: SetOption1
MQT: stat/tasmota_8AC822/RESULT = {"SetOption1":"OFF"}
CMD: SetOption11
MQT: stat/tasmota_8AC822/RESULT = {"SetOption11":"OFF"}
CMD: ButtonTopic
MQT: stat/tasmota_8AC822/RESULT = {"ButtonTopic":"0"}```

I did a quick check and my Treatlife switch is indeed generating MQTT stat messages on multipresses by default.

Now to figure out how to use Blueprints!

I will likely need to modify the code to flip the behavior because my wife insists that all smart switches act like dumb switches on a single press and no family member needs to be retrained!

The only thing is that if you want to have multi press behavior, there is a slight delay for a single press action, as tasmota needs to wait for a period of time to see if there is a second press… that delay is only 1/2 a second or so, but can be noticeable and at times annoying… especially with capacitance type switches.

If you don’t need multi press, just turn it off and the switches work straight away.