Template evaluation: comparing two timers for vehicle direction

I’m starting to build an automation using 3 motion sensors to determine which direction a passing vehicle is travelling.

I need to compare the value of two timers to determine which one triggered first.

I’ve been paying around with the following, but have just about exhausted my current templating capabilities.

Any pointers to the right direction will be most welcome.

- alias: test timer compare
  trigger:
    platform: template 
    value_template: >
      "{% if (state('timer.motion_driveway_1_pir_timer')|int) > (state('timer.motion_driveway_1_pir_timer')|int)%}true{% endif %}
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.driveway_motion_3_detected

May sound like a silly question, but if you have motion sensors, why do you need timers?
you could instead use triggers based on motion sensor state and see which was was triggered first based on either the last_updated attribute or a trigger template to see which sensor triggered the automation (and have a condition that the automation doesn’t run if it was last triggered less than [say] 5 sec ago)…
so something like:

- alias: test timer compare
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.motion_driveway_1
        - binary_sensor.motion_driveway_2
  condition:
    - condition: template
      value_template: '{{ (as_timestamp(now()) - as_timestamp(state_attr("automation.test_timer_compare", "last_triggered") | default(0)) | int > 5)}}'
  action:
    - service: notify.email
      data_template:
        message: >
          motion detected from
          {% if is_state(trigger.entity_id,"binary_sensor.motion_driveway_1") %} binary_sensor.motion_driveway_1 to binary_sensor.motion_driveway_2 {% else %} binary_sensor.motion_driveway_2 to binary_sensor.motion_driveway_1 {% endif %}
1 Like

Yes. That certainly another way to do it. I’ll give that a go.

Thank you for taking the time to reply.

It’s been a while but I’m back to this now.

Here’s where I’m at:

- alias: test timer compare
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.switch_1
      from: 'off'
      to: 'on'
    - platform: state
      entity_id: binary_sensor.switch_2
      from: 'off'
      to: 'on'
  condition:
    - condition: template
      value_template: '{{ (as_timestamp(now()) - as_timestamp(state_attr("automation.test_timer_compare", "last_triggered") | default(0)) | int > 5)}}'
  action:
    - service: notify.lounge_tv
      data_template:
        message: >
          driveway vehicle {% if trigger.entity_id == 'binary_sensor.switch_1' %} approaching {% else %} departing {% endif %}

It currently notifies as soon as either switch is triggered.

Any idea why that would be?

What I need it to do is report the direction of travel. If switch_1 triggers first and then switch_2 within the next 5 seconds it notifies ‘approaching’. If switch_2 triggers first and then switch_1 triggers within the next 5 seconds it notifies ‘departing’.

I had to use from 'off' to 'on' as I’m using mechanical switches to test this, rather than motion sensors and the changeover isn’t always clean.

Try and play with your value template:

value_template: '{{ (as_timestamp(now()) - as_timestamp(state_attr("automation.test_timer_compare", "last_triggered") | default(0)) | int > 5)}}'

You might be missing parenthesis, maybe something like this
value_template: '{{ (as_timestamp(now()) - (as_timestamp(state_attr("automation.test_timer_compare", "last_triggered") | default(0)) | int) > 5)}}'

Sorry came be more specific, I’m not home now

Did you ever get this working? I’m looking for a direction automation too.

In theory, yes, but I’ve yet to find sensors that respond quickly enough in my situation.

A hard-wired beam sensor will probably deliver the response times needed but the installation complexity of that outweighs the benefits.

I’m now using DVA on my Synology cameras to detect vehicle movement, though that doesn’t give me direction.