Ensure device is on for 7 seconds before turning off

Hi,

I have installed a ‘smart’ floodlight, that I am switching with a Sonoff Mini. The floodlight automatically goes back into its setup mode if its switched on & off for less than 7 seconds, its very annoying.

I want to know if there is a way to create an entity that I could use to control the Sonoff to stop this from happening, ie delay turning on or off if it was used less than 7 secs ago. I would like this as an entity as the outside lights get controlled in several ways, and if I have a new entity I can use it everywhere, automations, node-red, lovelave etc

Any suggestions on where to start?

Thanks in advance
Paul

How will users interact with the floodlight? Exclusively via Home Assistant’s UI?

If there’s no physical means of a user controlling the floodlight and it will controllable exclusively via Home Assistant’s UI then this allows for some measure of control to prevent it from being turned off before 7 seconds.

If when you say its controllable “in several ways” and one of those ways is via a physical switch, then I don’t see a way of using Home Assistant to prevent the floodlight from being turned off before 7 seconds.

Could build a switch template for it and set the ‘off’ as a script with an embedded template delay that ensures it waits 7 seconds. (now() - last_changed) calculation or similar.

Agree that if it’s a physical switch w/ direct control of the light and bypasses HA, not going to help w/ that use-case.

1 Like

Yes, always through home assistant. The switch is a smart switch, that connects through home assistant to trigger the Sonoff mini, there’s no direct wiring.

Markus,

I’ll have a read around this and see if I can understand it, thanks.
Paul

Template Switch:

- platform: template
  switches:
    floodlight_switchtemplate:
      friendly_name: "Floodlight"
      value_template: "{{ is_state('switch.sonoff_mini', 'on') }}"
      turn_on:
        service: switch.turn_on
        data:
          entity_id: switch.sonoff_mini
      turn_off:
        service: script.floodlight_turn_off
      icon_template: "mdi:lightbulb"

Script:

floodlight_turn_off:
  alias: Floodlight Off with Dynamic Delay
  sequence:
  - delay: >-
      {% set elapsed = now().timestamp() - as_timestamp(states.switch.sonoff_mini.last_changed,now()|float(default=0)) %}
      {% if elapsed > 7 %}00:00:00
      {% else %}00:0{{ (elapsed | int) + 1 }}:00
      {% endif %}
  - service: switch.turn_off
      data:
        entity_id: switch.sonoff_mini

The above is pretty untested, but hopefully at least gets you on the right path.

Just FYI: you can put all that in the turn_off option, no need to use a script (also your service data was indented too far).

- platform: template
  switches:
    floodlight_switchtemplate:
      friendly_name: "Floodlight"
      value_template: "{{ is_state('switch.sonoff_mini', 'on') }}"
      turn_on:
        service: switch.turn_on
        data:
          entity_id: switch.sonoff_mini
      turn_off:
        - delay: >-
            {% set elapsed = now().timestamp() - as_timestamp(states.switch.sonoff_mini.last_changed,now()|float(default=0)) %}
            {% if elapsed > 7 %}00:00:00
            {% else %}00:0{{ (elapsed | int) + 1 }}:00
            {% endif %}
        - service: switch.turn_off
          data:
            entity_id: switch.sonoff_mini
      icon_template: "mdi:lightbulb"
1 Like

Just for my understanding, does this delay for seconds or minutes?

{% else %}00:0{{ (elapsed | int) + 1 }}:00

In my understanding it would have to be {% else %}00:00:0{{ (elapsed | int) + 1 }} ?

switch:
  - platform: template
    switches:
      floodlight:
        friendly_name: Floodlight
        value_template: "{{ is_state('switch.sonoff', 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.sonoff
        turn_off:
          - wait_template: "{{ now() - states.switch.sonoff.last_changed > timedelta(seconds=7) }}"
            timeout: '00:00:10'
          - service: switch.turn_off
            target:
              entity_id: switch.sonoff
1 Like

Thanks all for the input, it’s all much appreciated. I’ll look into this over the next couple of days. I’ll take a deeper look at the different approaches.

Yes, seconds are at the end. So my code had this in the wrong spot. Good catch. Thank you!

Huh. TIL you can put multiple steps in the action pieces of template entities. Since it just said action here I always assumed it had to be one. Neat!

I’m considering making a PR to improve that documentation.

EDIT: it is already documented https://www.home-assistant.io/integrations/switch.template/#multiple-actions-for-turn_on-or-turn_off

Though I created a PR to update the options descriptions.

I haven’t used a wait template in the past, but doesn’t it just wait until it’s been 7 seconds in your example?

Trying to understand why your time out is set to 10 seconds

Forgive me if I’m misunderstanding, but seems that in your example, the timeout should be set to all zeros versus waiting an additional 10 seconds after the switch has been turned off or at least seven seconds.