Timing on templates

Hello, I wanted to know how I can configure a binary sensor (let’s call it 1) based on two binary sensors (let’s call it 2 and 3): I want the binary sensor 1 to go “On” when the binary sensor 2 goes from “On” to " Off “, and at the same time that binary sensor 3 has not been put” On "within a 5 minute interval.
I have come to get this code, but I can not set the delay so that it works.

binary_sensor:
  - platform: template
    sensors:
      sensordef:    
        friendly_name: "sensor1"
        delay_on:
          minutes: 5
        device_class: safety
        value_template: >-
          {{ is_state('binary_sensor.0x00158d0003969245_occupancy', 'on')
             and is_state('binary_sensor.0x00158d0003968c82_contact', 'off')}}



Thanks in advance

        value_template: >-
          {{ is_state('binary_sensor.0x00158d0003969245_occupancy', 'on') and is_state('binary_sensor.0x00158d0003968c82_contact', 'off')
  and ( now()|as_timestamp - as_timestamp(states.binary_sensor.0x00158d0003968c82_contact.last_changed) ) > 300 }}

Hi Tom, thanks for replying. The code you have given me is not valid. From what I see the syntax must be something like what you passed me. When validating it gives me the following error:

                          Invalid config for [binary_sensor.template]: invalid template (TemplateSyntaxError: expected token ',', got 'x00158d00052bce8b_occupancy') for dictionary value @ data['sensors']['sensordef']['value_template']. Got "{{ is_state('binary_sensor.0x00158d00057b3ebf_occupancy', 'on') and is_state('binary_sensor.0x00158d00052bce8b_occupancy', 'off')\n   and ( now()|as_timestamp - as_timestamp(states.binary_sensor.0x00158d00052bce8b_occupancy.last_changed) ) > 300 }}". (See ?, line ?).```

That’s because your object id starts with a number (which I forgot about). See: https://www.home-assistant.io/docs/configuration/templating/#entity_id-that-begins-with-a-number

Try this:

        value_template: >-
          {{ is_state('binary_sensor.0x00158d0003969245_occupancy', 'on') and is_state('binary_sensor.0x00158d0003968c82_contact', 'off')
  and ( now()|as_timestamp - as_timestamp(states.binary_sensor['0x00158d0003968c82_contact'].last_changed) ) > 300 }}

Hello, I want to know how to make a condition be checked when an automation is triggered. So if my binary sensor 1 goes “off”, I notify telegram on the condition that sensor 2 does not change in the next 30 seconds. I have tried with the for and the telegram notification notifies me when the sensor goes from “on” to “off” state. What I want is that sensor 1 goes “off” wait 30 seconds to verify that sensor two has not changed in 30 seconds.

condition: device
device_id: edf65a03951a415ab491000338b33f91
domain: binary_sensor
entity_id: binary_sensor.movimiento0105_occupancy
for:
  hours: 0
  minutes: 1
  seconds: 50
type: is_no_motion