Automation to Turn Device A off after N minutes if Device B has changed?!

Help :grin:

I’m fairly new to Home Assistant, but slowly moving over from a hodgepodge of Smartthings, Hubitat and diy ESP based wifi devices.

So I’ve got a switch that turns a bathroom light on/off, no problem, this just happens. Problem is, kids and wife doesn’t seem to understand turning the light off when not occupying the bathroom is a good idea. Ok, quick and easy solution, time the light to go off after 10 minutes. It works, but it’s not ideal when you’re in there for longer periods of time.

Installed a contact sensor on the door, figuring i could still keep the timed aspect but add in an override where the automation wouldn’t fire unless the door had closed and opened again. Well, I’m not getting it to work.

Tried a bunch of ways, but I can only seemingly get it to turn off either by time or sensor. Even tried setting a bool DoorHasOpened but I’m not getting it to trigger properly.

Any help would be appreciated.

Hi,
It’s a common challenge for sure. I have never perfected a solution either but i can offer a couple of pointers that will likely help.

  • The door sensor is not so good for triggering an switch ‘off’ condition. A door opening/closing in quick succession would happen when someone enters or leaves.
  • You really need some kind of presence detection in the bathroom. To detect when someone is still in the room. Something like a motion sensor.
    In the case of using a motion sensor you can then base the time to turn off the switch on when motion is no longer detected for a period of time.
    Cheers
1 Like

To do exactly what the title of your post describes, you just need to have a trigger of the state of DeviceB and an action sequence of a delay followed by a service call to turn off Device A.

For example - this turns off the lights 10 minutes after the door opens/closes and if the door changes state while the 10-minute timer is running, it resets the timer to 10 minutes.

alias: Turn off light 10min after door change
mode: restart
trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_door
condition: []
action:
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      entity_id: light.bathroom

However, I think you may find this implementation annoying for the reason you mention - the light turning off when you don’t want it to. For example, it’ll turn off if you take longer than a 10 minute shower.

I find the door-sensor-only automation (light on when door opens, off when door closes) works really well for closets, and it has the side-benefit of encouraging folks to close doors when they’re done, too. I’m not a fan of it for rooms, though.

For bathrooms, I use a combination of a motion sensor and a door sesnsor. The motion sensor always turns on the lights when it detects motion. After motion is no longer detected for X minutes, the lights turn off, but only if the door is open. If the door is closed when the “no motion” is detected, then the automation will wait for the door to open before turning off the lights. I’ve lived in a home with multiple other people with that type of automation for the better part of a year now, and my verdict is that it works well.

2 Likes

Thanks, yes, this is pretty much how I have it at the moment, when the door changes state the timer is reset, and that works fine apart from when I want to escape the world (kids) and lock myself in for 15 minutes :joy:

The idea was that in addition to the switch use the contact sensor to, pause if you will, the automation if the door hasn’t closed and then opened again during the N minutes. As in, if the door closes the automation waits for it to open again before turning the light off. This is what I’m really struggling with I guess.

Basically I want it to turn off the light after N minutes UNLESS door closes, then wait for door to open and turn off instantly.

This should cover instances where someone turns the light on to get a towel or whatever and don’t close the door as well as normal use and those other cases where it just takes longer.

Yes, I get it, should use a motion sensor, I’ve got no spares at the moment and the one I did have in there died from humidity, just trying to think outside the box :grin:

If you do ever go the motion sensor route, I have had good experience with the Philips Hue motion sensors - I have several mounted high in bathrooms (where humidity gets high), and they work great. The batteries last multiple years, and I haven’t had one fail in over 3 years. Not a super-long time, but promising so far.

Regarding the automation if all you have is a light switch and a door sensor, though…

Having the light switch in addition to the door sensor does give you additional options. I suggest you think through how you want the lights to behave. Draw out a state machine diagram if that helps - I use those in planning my more complicated automations.

One strategy I’ve found for simplifying automations (both writing and debugging) is to separate out detection logic and action logic. For example, in the bathroom, I have an input_boolean defined to indicate whether the bathroom is occupied or not. I have a separate automation that sets/clears this input_boolean. I then have another automation that triggers on the input_boolean and decides what to do when the bathroom is occupied or vacant. This makes it much easier to debug, and you get the side-benefit of recorded data about when the bathroom was occupied via the history of the input_boolean.

For reference, here’s roughly what I use for my bathroom occupancy detection logic in an automation blueprint. Since you’re not using a motion sensor, you could probably use the light switch in its place for the “turn on” logic.

trigger:
  - platform: state
    id: 'Motion Detected'
    entity_id: !input motion_entity
    to: 'on'
  - platform: state
    id: 'Motion Cleared'
    entity_id: !input motion_entity
    to: 'off'
    for: 
      seconds: !input no_motion_timeout 

  - platform: template
    id: 'Door Opened'
    value_template: >
      {{ door_entity != none and is_state(door_entity, 'on') }}
  
condition: []

action:
  - alias: Determine if motion was detected
    choose:
      - alias: Choice 0 - Motion detected
        conditions: 
          - condition: trigger
            id: 'Motion Detected'
        sequence:
          - service: input_boolean.turn_on
            target: !input target_boolean

      - alias: Choice 1 - Motion cleared and door open/unspecified
        conditions:
          - condition: trigger
            id: 'Motion Cleared'
          - condition: state
            entity_id: !input motion_entity
            state: 'off'
            for: 
              seconds: !input no_motion_timeout 
          - alias: Door sensor is unspecified or OPEN
            condition: or
            conditions:
              - '{{ door_entity_1 == none }}'
              - '{{ is_state(door_entity, "on") }}'
        sequence:
          - service: input_boolean.turn_off
            target: !input target_boolean

      - alias: Choice 2 - Door opened
        conditions:
          - condition: trigger
            id: 'Door Opened'
          - alias: Door sensor is unspecified or OPEN
            condition: or
            conditions:
              - '{{ door_entity == none }}'
              - '{{ is_state(door_entity, "on") }}'
        sequence:
          - alias: Wait a bit to see if someone entered the room
            delay: 00:00:03
          - choose:
              - alias: Make sure there is still no motion
                conditions:
                  - condition: state
                    entity_id: !input motion_entity
                    state: 'off'
                sequence:
                  - service: input_boolean.turn_off
                    target: !input target_boolean

    default: []

From there, I have a separate automation that decides what to do when the bathroom occupancy changes. For yours, it should be pretty simple - trigger on the input_boolean going to off and call the light.turn_off service.

2 Likes

Wow I’m so new to this, do you think this is possible to recreate whit scripts