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.

1 Like

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.

1 Like

Thanks both for the input.

Tried Didgeridrew Method and worked great, reason for this Believe it or not it’s because of a Phillips hue motion sensor as it’s cool down for reading light levels is 5 min.

And thanks to marounalkattar as well

I have a sensor setup so that when u walk past it waits 1.3sec to reset the illumination light readings as when you trigger the motion sensor it re reads the light levels after 1.3 sec and gets the right lux levels, then Continues on and checks the door contact Sensor To see if it is closed if it is the lights come on and stay on for 1 min then turn off, but if the door contact is open when the light is triggered to come on with the motion sensor is passed it just leaves the light on until u close the door then walk past the motion sensor again it re triggers the 1 min light routine and turns off again.

When I enter the house without triggering the motion sensor as I am entering the house and not walking past the sensor the door contact routine will then check the iIllumination of the motion sensor see that it’s dark then turns the lights on, then shut the door lights stay on until I walk past the motion sensor and runs the 1 min routine and then turns the lights off.

All great until,

if someone walked past the motion sensor in the hallway to walk upstairs and runs the 1 min light routine which turns the lights on and then lights go off after 1 min the Phillips hue keeps a hold of the higher lux level even when it goes dark for 5 min as it has a cooldown of 5 min before it rereads the light Levels.

When i open the door in that 5 min timeframe of the cooldown the lights would not come on as the hue motion sensor still has a high lux reading, So by doing the 5 min light check routine to see if the lights have been on in the last 5 min it ignores the motion sensor light readings and runs the 2nd Automation, the door contact would still then turn the lights on and after the 5 min it would revert back to the motion sensor Illumination light levels as by that time they would be reset.

I hope you understand what I was trying to do but it now works in both conditions.

Only if the Phillips hue could read the light levels all the time but I suppose the battery level would not last long.