Bathroom Lights Automation

Hello Smart People,
Can someone please help me with automating my bathroom lights.
I am using Xiaomi Binary sensor to detect motion and control tuya lights using HA, so far it’s great.
I would like to take this to the next level. I will add another motion sensor and Here’s what I want to achieve.

Three switches: A - Normal Lights B - Warm Lights C - Exhaust Fan.
Two Sensors: S1, S2 (inside shower)

  1. When dark - S1 detects Motion A Switches on but If the temperature is cold B switches on.

  2. Any time S1 detects Motion If the temperature is cold B switches ON

  3. S2 detects Motion C Switches ON A & B switches OFF if ON

  • At any point of time, there can be only one switch ON.
  • Everything off after 3 minutes if there’s no motion.

Hope I am clear, Thanks in advance.

Sounds like something that could work with some normal automations. But you’re lacking some information.

With what sensor do you want to check if the temperature is “cold”?. I mean do you got some kind of temperature sensor or do you want to use a generic outside temperature integration?. Then again you should decide what the lower limit of “cold” would mean for you.

The same goes for your “dark” trigger. How do you want to decide whether its dark. Is this the condition when the sun is down, or is it a lumen sensor you’ve laying around?

1 Like

In addition to @Infineghost 's questions… what do you want to do if S1 and S2 both detect motion?

In pseudo code:

condition:
  s1: on
  s2: off
  temp1: warm
  lux: dark
action:
  a: on
  b: off
  c: off

condition:
  s2: off
  temp1: warm
  lux: light
action
  a: off
  b: off
  c: off

condition:
  s1: on
  s2: off
  temp1: cold
action:
  a: off
  b: on
  c: off

condition:
  s2: on
action:
  a: off
  b: off
  c: on

condition:
  s1: off
  s2: off
action:
  a: off
  b: off
  c: off

So… 5 automations… in each of them you can either use a single template trigger (if you don’t mind your lights turning “on” again every time one of these states changes, which generally does nothing, but maybe with your lights it does do something), or you can specify each of these things as a trigger AND as a condition.

This will also handle cases like, when you went into the bathroom it was warm, and then it got cold. or it was light, and then it got dark, etc, etc.

1 Like

I was thinking outside temperature using the dark sky. On second thoughts I might get a temperature sensor. Thanks for your reply

Thank you so much. I’ll implement this and try.
How do I “template trigger”? Noobie here.

If S1&S2 detects motion… Hmm haven’t thought that far. I will fix them such a way that they both don’t see me at once. They should do their thing. Example if I come out of the S2(shower) exhaust switches off and S1 scenario activates…

How do I ensure that Iam not in the dark while the lights are being switched between A & B?

Thank you for your solution!!

I would indeed get a temperature sensor. As the outside temperature can be alot of °C off with your inside temperature, floors even.

To give you an example. I have a temperature sensor in my living room which had a temperature of 21°C but my bedroom (sunny side of my house) had a temperature of 27°C. I use automations for both floors

1 Like

Template Trigger Docs

I would make certain that I do any of the “turn on” actions before the “turn off” actions for starters. But, if your lights are a bit slow, I would add a delay. Something like…

automation:
  - alias: shower_occupied
    trigger:
      ...
    condition:
      ...
    action:
      - service: switch.turn_on
        data:
          entity_id: switch.a
      - delay: "00:00:02"
      - service: switch.turn_off
        data:
          entity_id: switch.b
2 Likes

Thanks a ton!!
lemme implement and try this.

I see conditions and actions. what about trigger?

The triggers are your motion sensors and temperature sensors.

conditions should be programmed as @swiftlyfalling mentioned above. If you want them to act as explained.

1 Like

Right, so there are a fews ways to handle triggers vs conditions when you have more than one thing you’re looking at. This has nothing to do with this particular automation and is just in regard to automations in general.

Let’s say I want switch.a to be “on” and switch.b to be “off” in order for my action to take place.

Method 1:

- alias: method one
  trigger:
    - platform: state
      entity_id: switch.a
      to: "on"
    - platform: state
      entity_id: switch.b
      to: "off"
  condition:
    - condition: state
      entity_id: switch.a
      state: "on"
    - condition: state
      entity_id: switch.b
      state: "off"
  action:
    - service: whatever

This is the safest, cleanest, easiest to read version. If switch.a goes “off” or switch.b goes “on” this will trigger. The conditions are then evaluated (this makes sure that, just because switch.a went “on” switch.b may not be “off”, so we check with the conditions.

Method 2

- alias: method two
  trigger:
    - platform: state
      entity_id: switch.a
      to: "on"
    - platform: state
      entity_id: switch.b
      to: "off"
  condition:
    - condition: template
      value_template: >-
        {{ is_state('switch.a','on') and is_state('switch.b', 'off') }}
  action:
    - service: whatever

This works exactly the same as method 1. However, the condition is less verbose. The downside is, the format of the condition is totally different than that of the trigger, so when you change something, you might have a bit more typing to do.

Method 3

- alias: method three
  trigger:
    - platform: template
      value_template: >-
        {{ is_state('switch.a','on') and is_state('switch.b', 'off') }}
  action:
    - service: whatever

This is the least verbose of them all. The downside is that every time the switch changes it’ll be re-evaluated and the actions will run again. For this small, two entity example, with only two states they can each be in, it makes almost no difference. It’s when you get into “or” situations that it can get messy. Let’s add a third entity, switch.c, and let’s say we want the action to happen when a is on, and either b OR c is off.

- alias: method three take two
  trigger:
    - platform: template
      value_template: >-
        {{ is_state('switch.a','on') and (is_state('switch.b', 'off') or is_state('switch.c', 'off')) }}
  action:
    - service: whatever

So, now with three entities… let’s say a is on, b is on, and c is on. Our trigger doesn’t fire. Easy. Now b changes to off which means the template evaluates to true and our action is performed. Now c changes to off. Since another thing changed, the template is evaluated again, it’s still true, so our action fires again. And maybe we don’t want that. For something like turning on a light, it’s not usually a big deal if you turn it on again. However, if your action starts a light fading slowly from 1% brightness to 100%, the fade would potentially start over again, and that’s probably not what you want.

So, to be safe, for things like this, you implement everything as a trigger AND as a condition. If you know it doesn’t matter, you can save some typing and just implement the trigger with a template.

2 Likes

Thanks buddy, appreciate you took the time out to write in non-pseudo code :), This helps me a lot, even in future automation. Still trying to wrap my head around templates.

Isn’t there an If else way to do things. Example. If its cold switch on A on, else switch on B?

Thank you so much!

There isn’t. This is because automations work on triggers, which are things that happened right now, as opposed to conditions. Is the difference between “did the light just turn on” and “is the light on”. It’s also because the “action” of an automaton is only supposed to run once. So the trigger is the thing that makes the action run. If a trigger had an else, that “else” action would run every time anything happened in all of Home Assistant that didn’t match the trigger.

It takes a bit to wrap your head around the subtle difference.

1 Like

Why is there no mention of Sensors in any of the methods?
Are the sensors state set separately?
Till now I thought Triggers should be sensors and actions should be switches…

Well, in my example code I used all switches. You can trigger on the state of anything.

In the case of your “turn on A when there is motion at s1 and it’s warm and it’s dark and there’s not motion at s2”:

- alias: method one
  trigger:
    - platform: state
      entity_id: binary_sensor.s1
      to: "on"
    - platform: state
      entity_id: binary_sensor.s2
      to: "off"
    - platform: numeric_state
      entity_id: sensor.lux
      below: 27
    - platform: numeric_state
      entity_id: sensor.temp
      above: 72
  condition:
    - condition: state
      entity_id: binary_sensor.s1
      state: "on"
    - condition: state
      entity_id: binary_sensor.s2
      state: "off"
    - condition: numeric_state
      entity_id: sensor.lux
      below: 27
    - condition: numeric_state
      entity_id: sensor.temp
      above: 72
  action:
    - service: homeassistant.turn_on
      data:
        entity_id: switch.a
    - service: homeassistant.turn_off
      data:
        entity_id:
          - switch.b
          - switch.c
1 Like

Thanks for being patient. this is extremely helpful!!!