or condition in Google Home
This image is generated using Dall-EPrompt: Generate an image of a small home during the day with multiple lights that are all turned off in a minimalistic flat style
Introduction
In the previous post we’ve seen that we can use and
conditions in automations. We also saw that there is an or
condition operator. We’ll in this post we’re going to update our previous example and add an or
condition.
If you remember the first case, we wanted to turn on the hallway lights when motion is detected using a motion sensor. However, the lights should only turn on when it’s dark outside and when I’m at home. This gave us the following diagram.
flowchart TD
event[Motion detected]
requirementOne{Am I home?}
requirementTwo{Is it dark outside?}
action(Turn on the lights)
event --> requirementOne
requirementOne --Yes--> requirementTwo
requirementOne --No--> stopOne((x))
requirementTwo --Yes--> action
requirementTwo --No--> stopTwo((x))
Case
For this post we want to expand our automation by only turning on the hallway lights when the previous conditions are met, and when either the bedroom lights are on or the living room lights are on. When either of those lights are off, we can safely assume that we are at home, however we chose to turn those lights of because we are in bed. Otherwise, we would be in our bedroom with the lights on or in the living room with the lights on.
So, let’s update our diagram with the new requirements.
flowchart TD
event[Motion detected]
requirementOne{Am I home?}
requirementTwo{Is it dark outside?}
requirementThree{Is the bedroom light on?}
requirementFour{Is the living room lght on?}
event --> requirementOne
requirementOne --Yes--> requirementTwo
requirementOne --No--> stopOne((x))
requirementTwo --Yes--> yesTwo{Yes}
requirementTwo --No--> stopTwo((x))
yesTwo --> requirementThree
yesTwo --> requirementFour
requirementThree --Yes--> actionThree(Turn on the lights)
requirementThree --No--> stopThree((x))
requirementFour --Yes--> actionFour(Turn on the lights)
requirementFour --No--> stopFour((x))
Conditions
In the previous post we saw that we can use the or
operator to add multiple conditions that will allow the execution of the automation when at least one of them is true.
So we have two conditions to add to the or
operator
- Lights bedroom are on
- Lights living room are on
This will make sure our animation won’t be executed if both lights are off, thus we can sleep without having to worry about the cats turning on the hallway lights.
Automation YAML
Now we know how we can achieve our goal, let’s create the actual automation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
metadata:
name: Motion in hallway
description: Turn on the hallway lights when motion is detected while I'm home and it's dark outside
automations:
# Trigger our automation when the motion sensor detects motion
starters:
- type: device.state.MotionDetection
state: motionDetectionEventInProgress
is: true
device: MS Hallway - Default
# Setup our requirements
condition:
type: and
conditions:
# Setup our "when it's dark" requirement
- type: time.between
after: sunset
before: sunrise
# Setup our "when I'm home" requirement
- type: home.state.HomePresence
state: homePresenceMode
is: HOME
# Only turn the hallway lights on when either the bedroom light is on or the living room light is on
- type: or
conditions:
- type: device.state.OnOff
state: on
is: true
device: Bedroom Light - Bedroom
- type: device.state.OnOff
state: on
is: true
device: Living room Light - Living Room
# Execute the following actions when motion is detected and our requirements are both met
actions:
- type: device.command.OnOff
on: true
devices: Hallway light - Hallway
Categories
Related articles