Automation using counters

Hello,
I need some help to make the following an automation using a motion sensor. I would like to have this logic:

variable: sensorCount = 0
Motion detected: sensorCount++
If sensorCount > 1 { do something }
DoorOpened:  sensorCount = 0

any help how I can implement this in HA?

Create a counter then in your automation:

Trigger:

  • motion detected
    Action:
  • increase counter
  • condition counter > 1
  • action you want to do if condition is met

Try to do it yourself and in case you get stuck, please post your code and we may be able to help you.

Thanks…will try it out and revert either way :slight_smile:

1 Like

it seems the following worked fine … will be testing it further during the weekend

#Counters
counter:
  stairssensorcount:
    name: Stairs Sensor Count
    initial: 0
    step: 1



- id: '1576869741194'
  alias: Motion in Stairs Counter
  description: Motion in Stairs Counter
  trigger:
  - entity_id: binary_sensor.commonstairs_movement
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - entity_id: counter.stairssensorcount
    service: counter.increment
  - delay: '60'
  - entity_id: counter.stairssensorcount
    service: counter.reset

- id: '1575225703256'
  alias: Motion in Stairs
  description: Motion in Stairs
  trigger:
  - entity_id: binary_sensor.commonstairs_movement
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - above: '1'
    condition: numeric_state
    entity_id: counter.stairssensorcount
  action:
  - data:
      message: Motion Detected in Stairs - test
    service: notify.mobile_app_sm_g930f

Why do you need this? If the motion sensor is a binary sensor, couldn’t you just check the state change from off to on?

you are right but I do not want to trigger an action on first movement.
I reduced the time it stays on to 3 secs, and I want to trigger an action once a person is detected more than once in a short amount of time. This is installed in stairs, where people tend to pass (1 or 2 sec) but if people stop i would like to trigger an action

Most sensors trigger every time a person passes by (within a time frame, for example 3 minutes) and that will reset the ‘off’ timer. So if the timer is 10 minutes the action will fire 10 minutes after the last person has triggered the sensor.

But that will make it trigger only once every 10min, even if multiple people will pass.

hey again, just did some tests but the increment is not going past 1.
also I am not understanding why the counter is being reseted before 60sec … any idea of what I might be doing wrong @Burningstone ?

I don’t know exactly how automations work technically under the hood. Perhaps the first Automation can only be triggered again after it has finished? So it isn’t triggered again if motion is detected within the delay?

You can always try to build complex joint automations but it sometimes is easier to have multiple.

In my view you need to monitor 3 events.

  1. Increase a counter every time motion is detected
  2. Reset the counter every 60 seconds (if desired only when counter greater than zero)
  3. Notify when counter above 1.

Nice and simple.

2 Likes

hi @metbril … i think you are correct. I just did a small test where I removed the “delay + reset” part of the automation, and the counter was incrementing each time the motion sensor was being triggered (see below). I think the solution is how you described it … to make an independent automation to reset the counter. I think I would change point 2. as reset the counter after 60 seconds the motion is detected. Hopefully this works.

added this automation, will test and revert

- id: '1577118540943'
  alias: Motion in Stairs Reset
  description: Motion in Stairs Reset
  trigger:
  - entity_id: binary_sensor.commonstairs_movement
    platform: state
    to: 'on'
  condition:
  - above: '1'
    condition: numeric_state
    entity_id: counter.stairssensorcount
  action:
  - entity_id: counter.stairssensorcount
    service: counter.reset

EDIT:
the above what tested and what is happening is that after the second detection (i.e. after condition > 1 reached), counter is being reseted to zero :confused:

The counter is being reset, because of the delay in your action. This is, some would say illogical, behaviour of HA. When you trigger an automation with a delay, the automation is “running” during the delay time. If the automation gets triggered again, while the automation is still “running” (waiting for the delay to finish and executing the other actions), it skips the delay part and therefore in your automation, resets the counter.

The code you used resets the counter every time when motion is detected and the counter is > 1.

To work around this I would suggest creating a timer and use a second automation to start this timer every time when motion has been detected. When the timer is started, while it is running, it will be reset automatically.

your explanation makes sense … i.e the delay is being skipped altogether when it is triggered again.
Will test the timer object and revert :slight_smile:

something like this?

timer:
  stairssensortimer:
    duration: '00:01:00'


- id: '1577118540943'
  alias: Motion in Stairs Reset
  description: Motion in Stairs Reset
  trigger:
  - entity_id: binary_sensor.commonstairs_movement
    platform: state
    to: 'on'
  action:
  - entity_id: timer.stairssensortimer
    service: timer.start
  - entity_id: counter.stairssensorcount
    service: counter.reset

EDIT: did not work … once triggered counteres was resetted immediately

thanks for helping out!
is this the correct approach?

- id: '1576869741194'
  alias: Motion in Stairs Counter
  description: Motion in Stairs Counter
  trigger:
  - entity_id: binary_sensor.commonstairs_movement
    platform: state
    to: 'on'
  condition: []
  action:
  - entity_id: counter.stairssensorcount
    service: counter.increment
  - entity_id: timer.stairssensortimer
    service: timer.start

- id: '1577118540943'
  alias: Motion in Stairs Reset
  description: Motion in Stairs Reset
  trigger:
  - event_data:
      entity_id: timer.test
    event_type: timer.finished
    platform: event
  condition: []
  action:
  - entity_id: counter.stairssensorcount
    service: counter.reset

No, not like this. I would do something like this:

Create a counter and a timer:

counter:
  stairs_motion_count:
    name: Stairs Motion Count
    initial: 0
    step: 1

timer:
  stairs_motion_timer:
    duration: '00:01:00'

One automation to reset the counter when the timer finishes:

- id: '1576869751187'
  alias: 'Reset counter when timer expires.'
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.stairs_motion_timer
  action:
    service: counter.reset
    entity_id: counter.stairs_motion_count

And then one automation that increments the counter, starts/restarts the timer and executes another action in case the count is > 1

- id: '1576869741194'
  alias: 'Increase Counter and start/restart timer on motion'
  description: Motion in Stairs Counter
  trigger:
    platform: state
    entity_id: binary_sensor.commonstairs_movement
    from: 'off'
    to: 'on'
  action:
  - entity_id: counter.stairs_motion_count
    service: counter.increment
  - entity_id: timer.stairs_motion_timer
    service: timer.start
  - conditon: numeric_state
    entity_id: counter.stairs_motion_count
    above: 1
  - action you want to do if count > 1
     

This can be simplified If you can change the time the motion sensor uses to change from “on” to “off” when there is no motion.

will try it out! thanks!

in case motion is triggered more than once, will the timer restart?

Yes, the second automation includes the action to start the timer. When an already running timer is started again, it just resets the timer.

cool … will try it out