Template as automation trigger

I want to trigger an automation when a binary sensor changes state but only if it is with 5 seconds of another binary sensor changing state.

I’ve been trying something like this, without success:

- alias: trigger_from_switch_2
  trigger:
    platform: state
    entity_id: binary_sensor.switch_2
  condition:
    condition: template 
    value_template: "{{  (as_timestamp(now()) - as_timestamp(state_attr('binary_sensor.switch_1', 'last_changed'))) | int  < 5 }}"
  action:
    - service: script.notifications
      data_template:
        message: 'switch 2 triggered'
        speaker: 'media_player.kitchen_speaker'
        title: 'Test switches'

I can’t get the template to render in Dev Tools so don’t yet know if it will work as an automation trigger.

Would somebody please point me in the right direction?

If your template fails in Dev Tools, it will not work in an automation.

last_changed is not an attribute, so state_attr('binary_sensor.switch_1', 'last_changed') returns None.

Try:

{{ as_timestamp(now()) - as_timestamp(states.binary_sensor.switch_1.last_changed) | int < 5 }}

Thank you so much. I appreciate it.