Turning on a light between a state change of another device in a set time window

Hi all

I want to be able to turn a light on when I open a contact but with a rule that another device has been off for 1 sec to 5 min anything within that time frame will turn a light back on. So if the contact opens at 3 sec after the light has changed state on to off the other light will come on. If the contact opens and it’s been 3 min after the light has changed state from on to off the other light comes on.

But if the light has not been on nothing happens as there has been no state change and if the state change has happened for longer than 5 min again nothing happens.

So basically when the contact opens it runs a check to see if that light has been on in the last 5 min if it has and light is off turn another light on within a 5 min window.

Is this possible. So meaning anything within that time frame when the contact sensor opens and the light has went off to start the 1 sec to 5 min window to be able to activate another light

Using the gui of home assistant to try and create but not sure how
Thanks.

Yes, it’s possible to do that using the Automation editor. You can use a Not condition to construct the “not more than 5 minutes” part… The Not condition can be found under “Building Blocks” in the UI editor.

conditions:
  - alias: Has the light been off at least 1 second
    condition: state
    entity_id: light.example
    state: 'off'
    for: "00:00:01"
  - alias: Don't Pass if it's been off 5 minutes or more 
    condition: not
    conditions:
      - condition: state
        entity_id: light.example
        state: 'off'
        for: "00:05:00"

This could also be done with a Template condition.

Create a New Automation:


alias: Turn On Light When Contact Opens (Within 5-Min Window)
trigger:
  - platform: state
    entity_id: binary_sensor.your_contact_sensor
    to: 'on'  # Adjust if your sensor uses other states like 'open'
condition:
  - condition: template
    value_template: >
      {{
        (now() - states.light.your_light_name.last_changed).total_seconds() > 1 and
        (now() - states.light.your_light_name.last_changed).total_seconds() <= 300 and
        states.light.your_light_name.state == 'off'
      }}
action:
  - service: light.turn_on
    target:
      entity_id: light.another_light_name

Adjust the 1 second and 5 minutes timing logic as needed to suit your requirements.