My overly complicated bathroom light/fan automation. Taking suggestions

I have a small room with just a shower, toilet, and a trashcan. The sink and vanity are outside of this room. It’s a private room inside the master bedroom, so shutting the door to pee is for squares. The light and fan are on the same switch. This room gets humid after a shower and takes a while to dehumidify. Sometimes I just pop in to toss something into the trash and don’t want the light/fan on. With contact sensors, a humidity sensor, and a motion sensor at my disposal, this is the ridiculous automation I have written so that neither I nor my partner should have to hit the light switch, and the cats cannot turn the light on:

alias: ⚙️💡💧Bathroom Light/Fan - NEW/EXPERIMENTAL
description: "Turn on only if the door is closed or the toilet lid is opened. Turn off when unoccupied, unless it's too humid compared to the adjacent room."
trigger:
  - platform: state
    entity_id:
      - binary_sensor.bathroom_door
    to: "off"
    id: occupied
  - platform: state
    entity_id:
      - binary_sensor.toilet_lid
    to: "on"
    id: occupied
  - platform: state
    entity_id:
      - binary_sensor.4_in_1_motion_sensor_motion_detection
    to: "off"
    id: unoccupied
    enabled: true
  - platform: template
    id: humidity_clear
    value_template: >-
      {{ states('sensor.4_in_1_motion_sensor_humidity') | float <=
      states('sensor.mr_blue_sky_humidity') | float + 2}}
  - platform: state
    entity_id:
      - binary_sensor.bathroom_door
    to: "on"
    id: unoccupied
  - platform: state
    entity_id:
      - binary_sensor.toilet_lid
    to: "off"
    id: unoccupied
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - occupied
          - condition: time
            before: "22:30:00"
            after: "07:00:00"
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.red_series_dimmer_bathroom
      - conditions:
          - condition: trigger
            id:
              - unoccupied
          - condition: state
            entity_id: binary_sensor.bathroom_door
            state: "on"
          - condition: state
            entity_id: binary_sensor.toilet_lid
            state: "off"
          - condition: state
            entity_id: binary_sensor.4_in_1_motion_sensor_motion_detection
            state: "off"
        sequence:
          - if:
              - condition: template
                value_template: >-
                  {{ states('sensor.4_in_1_motion_sensor_humidity') | float <=
                  states('sensor.mr_blue_sky_humidity') | float + 2}}
                alias: Bathroom humidity <= Bedroom humidity + 2
            then:
              - service: light.turn_off
                data: {}
                target:
                  entity_id: light.red_series_dimmer_bathroom
            else:
              - delay:
                  hours: 0
                  minutes: 30
                  seconds: 0
                  milliseconds: 0
              - if:
                  - condition: state
                    entity_id: binary_sensor.bathroom_door
                    state: "on"
                  - condition: state
                    entity_id: binary_sensor.toilet_lid
                    state: "off"
                then:
                  - service: light.turn_off
                    data: {}
                    target:
                      entity_id: light.red_series_dimmer_bathroom
      - conditions:
          - condition: trigger
            id: humidity_clear
          - condition: state
            state: "off"
            entity_id: binary_sensor.4_in_1_motion_sensor_motion_detection
          - condition: state
            entity_id: binary_sensor.bathroom_door
            state: "on"
          - condition: state
            entity_id: binary_sensor.toilet_lid
            state: "off"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.red_series_dimmer_bathroom
mode: single

This works, as far as I can tell. It just seems like bad code. I miss NodeRed. Anyway, please critique it. I’m open to suggestions to either simplify my yaml just so that it’s neater, or if there’s simpler logic I could be using that would simplify everything.

You have six triggers - why a single automation? There’s nothing to be gained and it will quickly become very difficult to maintain.

1 Like

Soon as we have folders for automations, sure. As it is, if I don’t group up related stuff into one automation, I’d struggle to find anything.

Why can’t find them? I have hundreds of automations and name them starting with the room and then what is being done. Then in each room I follow pretty much the same paradigm. I keep every automation very simple, each only change or act on one thing. Simple example: One automation that (re)starts a timer when there is motion. Another automation that when the timer is (re)started, the light goes on. One automation that when the timer finishes, the light goes off, and so on.

I think the biggest problem you are missing how much toilet paper is left on the roll and then the cost and brand sensor of the toilet paper with a loud booming voice from above that tells the person in the bathroom, “YOIU ARE USING TOO MUCH TOILET PAPER!”. Then you also need a camera tied to chatgpt to drive analysis of the feces including color, so it can automatically make an appointment with your doctor and put that in your calendar. Then you also need a scent detector, can’t live without that. And then a mechanism to automatically order batteries when the sensor batteries get low. To save $ with a big investment that will have an ROI of 9,862 years you should use the seat raising and lowering (as well as the toilet paper roll turning) hooked up to a system that will send the power back to your local utility to save $.01 off of your biill every 392.1 weeks. And don’t forget, a mainframe for enough computing power and an SSD at least 995 Petabytes.