Increment Counter to Fire automation

Hey everyone.
Hopefully a simple question.

I’ve created a counter to capture “on” state sets for a particular device.

What I want is to basically stop an automation that is firing at the first on state if I turn the switch on off on within 5 seconds.

Basically I have an automation that sees a switch turn on (and it’s halloween/christmas whatever) and it sets lights to run a script to transition colours. However I want this to stop and turn on to a “normal” (cool white) light if I flick the switch twice.

I created an automation to increment my counter and the counter maxes out at 2 but I can’t figure out how to determine if it was incremented within 5 seconds.

I suppose I can alternatively add another automation to reset the counter after 5 seconds. Is that the only way or is there something “cleaner” I can use instead.

Thanks

When you say “flick” do you mean turn on the switch, then off very briefly, then back on?

Yes that’s what I mean :slight_smile:

OK, a long time ago, I implemented that behavior with a timer. It was used to disable auto-off for motion-controlled lighting (I use something different now). If you perform an on-off-on sequence with the switch (within 3 seconds) it served as a signal to an automation that you did not want the lights to be turned off automatically.

  • When you turn on the switch, it starts a short timer (say 3 to 5 seconds max).
  • If you do nothing else with the switch, the timer simply expires (and nothing happens).
  • However, if you then quickly turn off the switch and then back on, the short timer is still active and that’s what tells an automation that you just signaled it to do something special.
- alias: 'Switch off'
  trigger:
    platform: state
    entity_id: switch.example
    from: 'on'
    to: 'off'
  action:
    service: timer.start
    entity_id: timer.example

- alias: 'Switch on'
  trigger:
    platform: state
    entity_id: switch.example
    from: 'off'
    to: 'on'
  condition: "{{ is_state('timer.example', 'active') }}"
  action:
    ... detected the signal, do something special here..

EDIT

Updated, consolidated version:

- alias: 'Switch control'
  trigger:
  - platform: state
    entity_id: switch.example
    from: 'on'
    to: 'off'
  - platform: state
    entity_id: switch.example
    from: 'off'
    to: 'on'
  action:
  - choose:
    - conditions: "{{ trigger.to_state.state == 'off' }}"
      sequence:
      - service: timer.start
        target:
          entity_id: timer.example
    - conditions: 
      - "{{ trigger.to_state.state == 'on' }}"
      - "{{ is_state('timer.example', 'active') }}"
      sequence:
      ... detected the signal, do something special here.
    

The following automation is set for my Office. If i turn the lights on, the scene “Hell” is activated after 5 seconds if nothing happens. But when i switch off within 5 seconds and then on after 3 seconds the Scene “Nachtlicht” is activated.

- alias: Set Nightlight on 3-Times State Change
  trigger:
    platform: state
    entity_id: light.buro
    to: "on"
  action:
    - wait_template: "{{ is_state('light.buro', 'off') }}"
      timeout: "00:00:05"
    - choose:
        - conditions: "{{ wait.completed }}"
          sequence:
            - wait_for_trigger:
              - platform: state
                entity_id: light.buro
                to: "on"
              timeout: "00:00:03"
              continue_on_timeout: false
            - service: hue.hue_activate_scene
              data:
                group_name: "Büro"
                scene_name: "Nachtlicht"
      default:
        - service: hue.hue_activate_scene
          data:
            group_name: "Büro"
            scene_name: "Hell"

Thanks for the pointers. Looks like I can accomplish what I’m doing with these.

Much appreciated!

For future reference, you can only mark one post in the thread with the Solution tag.

I received a notification that you marked my post with the Solution tag. However, it isn’t because you either changed your mind, and removed the tag, or attempted to tag two posts (you can’t). Whichever post you tag last becomes the Solution post. Something to bear in mind when tagging posts in the future.

Good luck with your project and let us know how it works out.

Hey there.

So I tried the automation but I realise now that my solution (the quick light switch flick) won’t work.
The problem is that the smart light is in series with a dumb light on a non-smart switch. So the light doesn’t ever register an off state and the unavailable is only after a timeout which is too long for a quick override.

I guess I’m going to have to go back and just set colouring according to a schedule condition (using the choose logic).

Thanks for the assistance.

This is what I’ve gone with so far. Will be testing as I go.

alias: SEASONAL_Bathroom Light
description: >-
  Set the light based on Conditions between 18:00 and 06:00, Halloween - Red,
  Christmas - Multicolour, Neither - Cool white
trigger:
  - platform: state
    entity_id: light.ty_bulb
    to: 'on'
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.halloween
            state: 'on'
          - condition: time
            after: '18:00:00'
            before: '06:00:00'
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.ty_bulb
            data:
              brightness: 255
              color_name: red
      - conditions:
          - condition: template
            value_template: '{{ now().month == 1 or now().month ==12 }}'
        sequence:
          - service: script.halloween_colour_cycle
    default:
      - service: light.turn_on
        target:
          entity_id: light.ty_bulb
        data:
          transition: 3
          kelvin: 6000
          brightness: 90
          white: 90
mode: single

In that case, remove the Solution tag because it doesn’t resolve your situation and re-opens the topic to attract new ideas.

1 Like