Counter Condition

Is there a way to Use a condition with a counter that if it is a the numeric value of 2 for 3 seconds the automation will trigger?

I have a counter based on the number of times a switch has been turned on. So I essentially want if the Counter has been at number 2 for 3 seconds it will reset to 0.

I am essentially trying to create an automation for a Double/Triple tap on a sonoff switch. So the counter increases by 1 each time the switch is tapped but if it has only been on a number of 2 (For Double Tap) for 3 seconds (yes this may not be realistic) It will trigger an automation to turn on a light but also to reset the counter to 0(Zero).

I think you can just use an automation with a state trigger, don’t specify to or from, but add

for:
  seconds: 3

Then, in your actions you can do what you want based on the current value of the counter then call reset on the counter to reset it to 0.

How would i do the action based on the counter? Would that be a condition? Here is and example of what i want.

When Counter is 1 for 1 second turn Light 1 on then reset. When Counter is 2 for 1 second turn light 2 on.

Use a template for the service data. Something like

trigger:
  platform: state
  entity_id: counter.sonoff_switch
  for:
    seconds: 1
condition:
  condition: numeric_state
  entity_id: counter.sonoff_switch
  above: 0
action:
- service: light.toggle
  data:
    entity_id: >
      {% if is_state(trigger.entity_id, '1') %}
        light.light_1
      {% elif is_state(trigger.entity_id, '2') %}
        light.light_2
      {% else %}
        light.light_3
      {% endif %}
- service: counter.reset
  entity_id: counter.sonoff_switch

That would toggle light_1 for one press, light_2 for two presses, and light_3 for any other number of presses then reset the counter. The condition just keeps it from triggering again when it’s reset to zero.

Note: I’m on my phone so the syntax or formatting might need some fixing.

1 Like

Thanks That makes sense. I will give a shot.

I have tried the below and HA dashboard says there is an invalid configuration along with the configurator checker. I know it has to do with this part of the code since if i comment it out it works fine but I dont know what as neither of them tell me what line is causing the issues. Am i missing something?

- alias: Tap Controller
  trigger:
    platform: state
    entity_id: counter.tap
    for:
      seconds: 1
  condition:
    condition: numeric_state
    entity_id: counter.tap
    above: 0
  action:
  - service: switch.turn_on
    data:
      entity_id: >
        {% if is_state(trigger.entity_id, '1') %}
          switch.desk_lamp
        {% elif is_state(trigger.entity_id, '2') %}
          switch.philsoffice_switch
        {% else %}
        {% endif %}
  - service: counter.reset
    entity_id: counter.tap

@tboyce1 are you able to see anything in my Template that wouldnt be correct? HA errors out when i have the templates however it doesnt tell me what.

Try changing data to data_template

Here is what i get now when I do a check config in HASSIO.

Testing configuration at /config
2018-02-25 13:02:32 ERROR (SyncWorker_0) [homeassistant.util.yaml] mapping values are not allowed here
in “/config/automations.yaml”, line 679, column 19
2018-02-25 13:02:32 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/automations.yaml”, line 679, column 19

Here is the config. Line 679 is the data_template line.

- alias: Tap Controller
  trigger:
   - platform: state
     entity_id: counter.tap
     for:
       seconds: 1
  condition:
    condition: numeric_state
    entity_id: counter.tap
    above: 0
  action:
  - service: switch.turn_on
     data_template:
       entity_id: >
         {% if is_state('trigger.entity_id', '1') %}
           switch.desk_lamp
         {% elif is_state('trigger.entity_id', '2') %}
           switch.philsoffice_switch
         {% else %}
         {% endif %}
   - service: counter.reset
     entity_id: counter.tap

Indentation. data_template should line up with service.

I still get a Invalid configuration on the Automation Yaml file but it is not specific to lines now. Is trigger.entity_id correct or should enitity_id be replaced with an actual ID.

Sorry for going back and forth on this I dont understand templates still. :slight_smile:

Thanks for the help @tboyce1

I am stumped. I have tried adding and removing spacing but I still get the message when HA is restarted that Automation is an invalid config along with if i do check config. The frustrating part is it doesn’t show any errors in the hassio logs and when the check config fails it doesn’t tell me anything.

Any help would be greatly appreciated as I do not understand templates. This one makes sense to me and seems as though it should work but its not. Does entity_id in the trigger.entity_id actually need to be a valid Entity id? Thats the only other thing that I could think of.

here is what i have.

- alias: Tap Controller
  trigger:
    platform: state
    entity_id: counter.tap
    for:
      seconds: 1
  condition:
    condition: numeric_state
    entity_id: counter.tap
    above: 0
  action:
    service: switch.turn_on
    data_template:
      entity_id: >
        {% if is_state('trigger.entity_id', '1') %} switch.desk_lamp
        {% elif is_state('trigger.entity_id', '2') %} switch.philsoffice_switch
        {% else %}
        {% endif %}
    service: counter.reset
      entity_id: counter.tap

I just found this in the Check config results.

2018-02-27 17:09:06 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: dependency violation - key “for” requires key “to” to exist @ data[‘trigger’][0]. Got None. (See /config/configuration.yaml, line 62). Please check the docs at https://home-assistant.io/components/automation/

Line 62 in my configuration.yaml file is

group: !include groups.yaml

Edited, Researching a little more I was able to get the Tap counter to work along with resetting with the below however the switches do not turn on. any thoughts?

- alias: Tap Controller
  trigger:
    platform: numeric_state
    entity_id: counter.tap
    above: 0
    for:
      seconds: 1
  action:
    - service: switch.turn_on
      data_template:
       entity_id: >
        {% if is_state('trigger.entity_id', '1') %} switch.desk_lamp
        {% elif is_state('trigger.entity_id', '2') %} switch.philsoffice_switch
        {% else %}
        {% endif %}
    - service: counter.reset
      entity_id: counter.tap

trigger.entity_id shouldn’t be in quotes. trigger is a variable that gets passed to your templates containing useful information about the event that triggered the automation:

The error you’re seeing is because it’s always hitting the else case. In that case, there’s not a valid entity_id since it’s left empty, so the switch.turn_on service is complaining that it didn’t get the right data.

An easy workaround would be to just create a dummy template switch just to toggle in the else case or just put switch.philsoffice_switch in the else case so that it gets used for any count > 1.

A cleaner solution if you want to limit it to only acting for 1 or 2 taps might be to try changing the order of the services and using a condition to keep it from calling switch.turn_on for count > 2:

- alias: Tap Controller
  trigger:
    platform: numeric_state
    entity_id: counter.tap
    above: 0
    for:
      seconds: 1
  action:
    - service: counter.reset
      data_template:
        entity_id: {{ trigger.entity_id }}
    - condition: numeric_state
      below: 3
      value_template: {{ trigger.to_state.state }}
    - service: switch.turn_on
      data_template:
       entity_id: >
        {% if trigger.to_state.state == 1 %} switch.desk_lamp
        {% elif trigger.to_state.state == 2 %} switch.philsoffice_switch
        {% else %}
        {% endif %}

Note that I had to use trigger.to_state.state to get the state of the counter when the automation was triggered instead of getting the current state for trigger.entity_id since with this approach the counter has already been reset before we check the state.

I was able to finally get to try this out and it works. I used the original and I think there were two issues. One was having the trigger.entity_id in quotes and the other was not having above: 0 in the trigger. I added another switch to the else so that would work. The little things in the code can throw everything off.

If i wanted to have more than 3 taps i could just keep adding the elif lines correct?

the next question would be. Would it possible to add a condition in this same automation to turn the lights on if they are currently off and vica versa or would that be easier by a dummy switch to use in this automation and have the dummy switch be the trigger in another automation that would have that condition?

Correct, if you want more taps then just add more elif statements.

If you want it to turn them on/off depending on the current state, then just use switch.toggle as the service rather than switch.turn_on.

Thanks Trevor that works. I did not know about switch Toggle. I just split my Automation in half using that.

Did you just set an automation to incrementally up the counter based on the switch being turned on?

Greetings everybody,

I’m really glad I found this topic, this is exactly what I’m looking for!

But I somehow can’t get it to fire, this is what I have in my automation file:

- alias: Tap Controller

trigger:
platform: numeric_state
entity_id: counter.tap
above: 0
for:
seconds: 1
action:
- service: counter.reset
data_template:
entity_id: {{ trigger.entity_id }}
- condition: numeric_state
below: 3
value_template: {{ trigger.to_state.state }}
- service: switch.turn_on
data_template:
entity_id: >
{% if trigger.to_state.state == 1 %} switch.leeslamp_rechts
{% elif trigger.to_state.state == 2 %} switch.leeslamp_links
{% else %}
{% endif %}

Do I need to add something in the configuration file? Or does this not work with my Sonoff basic Tasmota switches?

Thanks in advance.

are you able to get all of your automation in the preformated text? The -alias looks like its in it but the rest of it is not.

Instead here is what I have. As a note it has been a long time since I worked on this and have not installed the switch in this manner yet so I do not have the ability to test but it should be the way it was when I said i had it working above. I think you are missing the counter.tap entity.

- alias: Tap Counter
  trigger:
   - platform: state
     entity_id: switch.tap_test
     to: 'on'
  action:
    service: counter.increment
    entity_id: counter.tap
    
- alias: Tap Controller
  trigger:
    platform: numeric_state
    entity_id: counter.tap
    above: 0
    for:
      seconds: 1
  condition:
    condition: numeric_state
    entity_id: counter.tap
    above: 0
  action:
    - service: switch.toggle
      data_template:
       entity_id: >
        {% if is_state(trigger.entity_id, '1') %}
          switch.desk_lamp
        {% elif is_state(trigger.entity_id, '2') %}
          switch.philsoffice_switch
        {% else %}
          switch.counter_light
        {% endif %}
    - service: counter.reset
      entity_id: counter.tap