How to count visitors?

I’m trying to setup an automation which will allow me to track how many visitors are in the building. For that purpose I’ve installed two proximity switches. Whenever switch A is triggered, dummy automation A is called, and dummy automation B for switch B. This is done in order to calculate the time difference which in the end determines if the visitor is entering or leaving the building. If the time difference is between 0 and 1, a visitor is entering, if between 0 and -1, a visitor is leaving. Anything else is not meant to increment or decrement the counter.
I’m able to retrieve the last_triggered data and calculate the time difference under Developer Tools > Template but I fail to implement it within automation.

Any help appreciated, and thanks in advance.

I guess you can simply use the Counter.

+1 or -1 each time you do a calculation.

My bad, didn’t mention it. I’m already using the counter. The problem is, each time a switch changes its state for off to on, a time difference calculation should be done between A and B in order to determine if the counter is to be incremented or decremented.

I guess I need to template that part but there is no such option under action.

If you can template it, then you can also use it in an automation.
Its been quite some time that i have done something like that, as i mainly use nodered now to do such kind of automations. But let me try to help you anyway.

First thing that comes to mind, is you could create a template sensor, that when the calculation is +1 turns ON and OFF if its -1. Or better yet, 2 sensors that turn on for only a short period of time as to also account for multiple people entering.

Then you could use that as a trigger.
Or you just go directly with the template trigger:

You could just have a single automation that triggers on both motion sensor.
Then, add a choose action, with a template condition that will compare the last_updated of both sensor.
Base on the result, call counter.increment or counter.decrement service on your counter.

Mind that if people are getting in or out in “packs”, your system won’t work, ofc.
If they are somehow forced to go in queue, you still have to be sure that your motion sensors are going “off” pretty instantly. Some of them take minutes to go back to “off”.
Also, the “field of view” will have to be pretty narrow, I guess.

I had a similar situation, I needed to count people inside my lab to trigger some automations depending on the number of occupants. Here is my approach that has been working rock solid since then.

https://community.home-assistant.io/t/help-creating-a-people-counter-passing-through-a-door/312897

Thanks for pointing me in the right direction. I was so obsessed to solve it with last_triggered I completely missed what you meant. I’ve got it working with 2 seperate automations.

Below I posting the code in case needed.

- id: "10002"
  alias: Gate Time Difference Exit
  trigger:
    - platform: state
      entity_id: switch.gate_a1
      to: "on"
  condition:
    - condition: numeric_state
      entity_id: counter.gate_counter
      above: "0"
  action:
    - condition: template
      value_template:
        '{% if 0 < as_timestamp(states.switch.gate_a1.last_changed) - as_timestamp(states.switch.gate_a2.last_changed) < 1 %}
        true {% endif %}'
    - service: counter.decrement
      target:
        entity_id: counter.gate_counter
  mode: single

- id: "10003"
  alias: Gate Time Difference Enter
  trigger:
    - platform: state
      entity_id: switch.gate_a2
      to: "on"
  action:
    - condition: template
      value_template:
        '{% if -1 < as_timestamp(states.switch.gate_a1.last_changed) - as_timestamp(states.switch.gate_a2.last_changed) < 0 %}
        true {% endif %}'
    - service: counter.increment
      target:
        entity_id: counter.gate_counter
  mode: single