Automation for bathroom with booth motion and door sensor

Hi

I have problems with automatons for my bathroom.

I want:

Turn on lights when door sensor opens
Turn of lights if no motion is detected
If motion is detected when door is closed, don’t turn of lights until door opens again.

Is this possible?

  • alias: ‘Motion bathroom detected’
    trigger:
    platform: state
    entity_id: binary_sensor.badrum_nere_rorelsesensor_sensor
    from: ‘off’
    to: ‘on’
    action:

    • service: homeassistant.turn_on
      entity_id:
      • switch.badrum_nere_switch_2
      • switch.badrum_nere_switch
  • alias: ‘Motion bathroom not detected’
    trigger:
    platform: state
    entity_id: binary_sensor.badrum_nere_rorelsesensor_sensor
    to: ‘off’
    for:
    minutes: 1
    action:

    • service: homeassistant.turn_off
      entity_id:
      • switch.badrum_nere_switch
      • switch.badrum_nere_switch_2
  • alias: ‘Bathroom door open’
    trigger:
    platform: state
    entity_id: binary_sensor.door_open
    from: ‘off’
    to: ‘on’
    action:

    • service: homeassistant.turn_on
      entity_id:
      • switch.badrum_nere_switch

Could you format your code using the </> button in the editor please? It makes it easier to see whether there are obvious syntax/indentation issues
image

by the way, if you have motion sensors, why do you need to monitor whether the door is opened or closed?
surely if there is motion that means someone is in the room and that should be enough to keep the light on?
At least that’s the approach I’ve taken for all my motion sensors…

For example im in the shower the motion sensor cant see me.
The code I pasted is just it is now, I know that that code is not working like I want, I just pasted it to show where I am right now.

And I now that I can change to 30 minutes but I don’t want that :slight_smile:

Hey @lolouk44,

please let me ask about that approach, im reconsidering mine…

I have several scenes and modes i which i set motion sensors accordingly (watch telly in the Hometheater -> switch off motion sensor home theater, leave home -> turn on motion sensors in the house and report motion, etc etc)
still, Im missing a general rule for the motions sensors to switch on the lights, if the light is low, or the sun is set, that kind of thing.
Have you setup anything to take care of that too?
If so,would you please share?
Thx,
Marius

- alias: 'Motion badrum nere'
  trigger:
   platform: state
   entity_id: binary_sensor.badrum_nere_rorelsesensor_sensor
   from: 'off'
   to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: 
       - switch.badrum_nere_switch_2
       - switch.badrum_nere_switch


- alias: 'Ingen motion badrum nere'
  trigger:
   platform: state
   entity_id: binary_sensor.badrum_nere_rorelsesensor_sensor
   to: 'off'
   for:
    minutes: 3
  action:
    - service: homeassistant.turn_off
      entity_id: 
       - switch.badrum_nere_switch
       - switch.badrum_nere_switch_2



- alias: 'Badrumdoor'
  trigger:
   platform: state
   entity_id: binary_sensor.door_open
   from: 'off'
   to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: 
       - switch.badrum_nere_switch

At first glance I think I’d build an input_boolean to track presence in the bathroom and use this as a condition or maybe even a trigger for your automation.
This will ensure the list is not left on while nobody is in the room and the light does not go off while someone is in there…
You need to think carefully of the logic to turn that sensor on and off.
Motion turning on is probably your only trigger to set presence = on, it’s the presence = off that needs some thought…

here’s an example of how I control the lights in my living room.
That includes checking whether there’s motion, whether the TV is on, whether it’s late (sun state) whether it’s dark (photo resistor), etc…

thanks!
freely associating… would this work, or would this trigger constantly… never really sure i confess:

  - alias: 'Low light - Motion sensors on'
    id: 'Low light - Motion sensors on'
    initial_state: on
    trigger:
      - platform: template
        value_template: {{ states.sensor.mean_living_lux.state < states.input_number.low_lux.state}}
      - platform: state
        entity_id: sun.sun
        to: 'below_horizon'
    condition: []
    action:
      - service: homeassistant.turn_on
        entity_id: group.philips_motion_sensors
      - condition: state
        entity_id: input_boolean.notify_notify
        state: 'on'
      - service: notify.notify
        data_template:
          message: >
            {{as_timestamp(now()) | timestamp_custom("%X") }}: Motion detection is on , since Indoor light is {{ states.sensor.mean_living_lux.state}},
            Sun is {{states.sun.sun.state}}.

of course this would have to be disabled when any of the other scenarios would kick in, but an extra line in these automations would be easy i guess:

- service: homeassistant.turn_off
  entity_id: automation.low_light__motion_sensors_on

not sure what your group.philips_motion_sensors does, but provided that you turn motion on or off and not actually turn the motion status on or off, it should work.
Silly question though, why would you turn motion sensors on or off? Mines are on all the time, but depending on lighting condition I take actions (e.g. turn the lights on)

No, it’s only going to trigger when sensor.mean_living_lux or input_number.low_lux change state
so say your sensor.mean_living_lux is 300 and you manually adjust input_number.low_lux to 350, it will trigger the automation…

actually it should have been the group.philips_motion_sensor_switches… and yes they turn on/off the motion sensors.

i do that because in the Hue app, I have actions(lights) configured upon motion sensing. Switching the sensors off, means these lights are not switched on. Switching the sensors on, means they are triggered when conditions are met. btw, in the Hue app, one can also set light level and motions sensitivity quite nicely.

Hue native app is , given the current implementation and integration of Hue in Home-Assistant, the more reliable way of controlling the accessories and lights.

thats what i had hoped for indeed. unfortunately my lights switched on again, even after my home theater scenario switched them, and the appropriate motion sensor to off. Apparently the automation took over again, maybe because the template kicked in while the light level changed, because of the lights switching off …:wink:

think i have to build in a condition in the automation, the scenarios not being active, or as mentioned in m post above, switch off the automation in the scenarios… have to figure out which is simplest and most robust.

@lolouk44
what about this then? a service_template based on the binary_sensor checking the light level (based on the input_number)

  - service_template: >
      homeautomation.turn_{{ states('binary_sensor.home_theater_lux_input') }}
      entity_id: group.philips_motion_sensor_switches

the template works fine. But when/how to trigger this…like this?

  - alias: 'Low light - Motion sensors on'
    id: 'Low light - Motion sensors on'
#    initial_state: on
    trigger:
      - platform: state
        entity_id: binary_sensor.home_theater_lux_input
      - platform: state
        entity_id: sun.sun
        to: 'below_horizon'
    condition:
      - condition: template
        value_template: >
          {{ is_state('binary_sensor.activity_home_theater', 'off')}}
    action:
      - service_template: >
          homeautomation.turn_{{ states('binary_sensor.home_theater_lux_input') }}
        entity_id: group.philips_motion_sensor_switches
      - condition: state
        entity_id: input_boolean.notify_notify
        state: 'on'
      - service: notify.notify
        data_template:
          message: >
            {{as_timestamp(now()) | timestamp_custom("%X") }}: Motion detection is {{ states('binary_sensor.home_theater_lux_input') }} , since Indoor light is {{ states.sensor.mean_living_lux.state}},

Hope this works as follows: on each state change of the binary sensor (and on state change of the sun going below_horizon) it switches on/off, depending on the actual state of the binary_sensor.

Which is set to read an input_number, which in itself is compared to the actual light level in my hometheater. On indicating low light, high indicating enough light, all under the condition home_theater mode is not set

just to confirm you don’t want the light to turn on if you’ve adjusted the slider?
If so simply add a condition that the slider’s state has not changed in the last 120 sec for example:

    - condition: template
      value_template: '{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.home_theater_lux_input.last_updated | default(0)) | int > 120)}}'
1 Like

and this doesnt error out…

 action:
      - service_template: >
          homeautomation.turn_{{ states('binary_sensor.home_theater_lux_input') }}
        entity_id: group.philips_motion_sensor_switches

took a keen eye to spot the error…

@brallor Did you solve your question?

If not, this is how I handle your scenario regarding turning on/off the bathroom light and keeping the lights on until the door is opened.

I run an automation that if motion is detected in the bathroom it will run a script that turns on the light for three minutes. If the motion sensor detects additional movement the script repeats and the lights stay on.

At the end of each three minute segment, the script processes a wait template that checks the status of the bathroom door. If the bathroom door is not open, the script waits. The lights stay on. This handles the situation you mention of being in the shower

Once the door opens, the wait template is satisfied and the scripts proceeds. It turns off the lights. Now usually the motion detector sees movement when one leaves the bathroom so the lights will actually stay on for three more minutes. At that time it processes the script once again and since there has been no new movement and the door is open, the lights will turn out.

So basically if you walk into the bathroom, keep the door open, wash your hands and leave for example, the light stays on for approximately three minutes and then turns off.

If you walk into the bathroom and close the door, the lights stay on until you open the door and leave. At that point it waits three minutes and turns off the light.

The key is to leave the bathroom door open or at least slightly ajar so the door sensor sees the door as open.

thank you! Ive changed the automations somewhat, and now need another attribute, last_triggered, of the automation itself. Reason, whithout it it keeps triggering when lets say a cloud causes temporary light loss. Taking a few minutes would prevent that from triggering. (cant use a for: minutes construction on the binary_sensor i guess…?)

Would this work:

{{ (as_timestamp(now()) - as_timestamp(states.automation.low_lights__motion_sensors_on.last_triggered | default(0)) | int > 120)}}

as in:

  - alias: 'Low light - Motion sensors on'
    id: 'Low light - Motion sensors on'
#    initial_state: on
    trigger:
#      - platform: template
#        value_template: >
#          {{ states('sensor.mean_indoor_lux') < states('input_number.low_lux')}}
      - platform: state
        entity_id: binary_sensor.low_lux_input
      - platform: state
        entity_id: sun.sun
        to: 'below_horizon'
    condition:
      - condition: template
        value_template: >
          {{ is_state('binary_sensor.activity_home_theater', 'off')}}
    action:
      - condition: template
        value_template: >
          {{ (as_timestamp(now()) - as_timestamp(automation.low_lights__motion_sensors_on.last_triggered | default(0)) | int > 120)}}
      - service_template: >
          homeassistant.turn_{{ states('binary_sensor.low_lux_input') }}
        entity_id: group.philips_motion_sensor_switches
      - condition: state
        entity_id: input_boolean.notify_notify
        state: 'on'
      - service: notify.notify
        data_template:
          message: >
            {{as_timestamp(now()) | timestamp_custom("%X") }}: Motion detection is {{ states('binary_sensor.low_lux_input') }} , 
             since Indoor light is {{ states('sensor.mean_living_lux')}},
            Sun is {{states('sun.sun')}}

yes it would, though I’d move the condition under the condition header, it’s neater and easier to troubleshoot in the future.
Also we don’t want to hijack @brallor’s post any longer, so could you raise a new post if you still need help?

@brallor, do you still need help?

@gumbo
What happens if you go to bathroom, exit and close the door before the 3 min, would the list stay on all night?

1 Like

As I indicated the key to my scenario involves leaving the bathroom door open or ajar when not in use. If you are in the habit of closing the bathroom door when the room is unoccupied, a modification would need to be made.

I guess this is at that precise time that you start cursing your guests :slight_smile:

Hm, looks nice. But will it wait if the door was opened and then closed?