Hello all.
I recently bought a motion detector and millimeter wave sensor to automate the living room lights to turn on after sunset. I have the automation set to detect presence and motion and to leave the lights on if they are detected. I tried to add a dimming function after two minutes of no motion or no presence detected, and after another two minutes to turn the lights off. However, I do not think it is working properly as every time I wake up in the morning, all of my lights are on. I was hoping someone could look at the Yaml file and see if I did something incorrectly. Thank you for your help in advance.
#Help with my first automation
1 messages · Page 1 of 1 (latest)
Hello @warm nacelle,
Sharing the yaml is the first step.
To format your text as code, enter three backticks on the first line, press Enter for a new line, paste your code, press Enter again for another new line, and lastly three more backticks.
```yaml
example: here
```
Don't forget you can edit your post rather than repeatedly posting the same thing.
alias: Living room light automation
description: >-
Turn lights on when motion or presence detected and then turn them off if no
motion or presence for 4 minutes.
triggers:
- trigger: state
entity_id:
- binary_sensor.living_room_motion_sensor_occupancy
from: null
to: "on"
conditions:
- condition: sun
after: sunrise
actions:
- action: light.turn_on
metadata: {}
data:
brightness_pct: 100
target:
device_id: 7ad58cc1430b10beee788b774bf55e59
- if:
- condition: state
entity_id: binary_sensor.living_room_motion_sensor_occupancy
state: "off"
for:
hours: 0
minutes: 2
seconds: 0
- condition: state
entity_id: binary_sensor.living_room_presence_detector_presence
state: "off"
for:
hours: 0
minutes: 2
seconds: 0
then:
- action: light.turn_on
metadata: {}
data:
brightness_pct: 50
target:
device_id: 7ad58cc1430b10beee788b774bf55e59
- if:
- condition: state
entity_id: binary_sensor.living_room_presence_detector_presence
state: "off"
for:
hours: 0
minutes: 4
seconds: 0
- condition: state
entity_id: binary_sensor.living_room_motion_sensor_occupancy
state: "off"
for:
hours: 0
minutes: 4
seconds: 0
then:
- action: light.turn_off
metadata: {}
data: {}
target:
device_id: 7ad58cc1430b10beee788b774bf55e59
mode: single
also my original code had a bit about trying to turn the light to 50% if motion or presence is detected after 10 PM to use as a night light but i took that out to troubleshoot
First thing I see is you trigger when occupancy goes from null to on. This turns the light on (Assumimg after sunrise, so likely until midnight).
The conditions in the lower if statements both require that occupancy to be off and-ed with the presence sensor to be off.
The occupancy cannot be both on and off.
So it will never dim or turn off.
A choose statement is perfect for this.
https://www.home-assistant.io/docs/scripts/#choose-a-group-of-actions.
Draw a flowchart to start…
So would I do a choose action that chooses if it is after sunset but before 10 PM it will turn the light on to 100, but if it is after 10PM but before sunrise, set light to 50%?
A couple of triggers, one when something turns on, one when it turns off. Trigger_id can tell you which triggered.
Then in the bottom, choose condition I saw this, do #1, and another condition I saw that, do #2.
You can add another out of the choose to always do these other things, and you can default if it triggered but it wasn't by this or that, do the default thing.
It seems like the if-then is what you want, but usually it isn't
I am updating my automation, would you mind looking through the updated one?
It will have to by in 2 parts due to discord limitations
alias: Living room light automation
description: >-
Turn lights on when motion or presence detected and then turn them off if no
motion or presence for 4 minutes.
triggers:
- trigger: state
entity_id:
- binary_sensor.living_room_motion_sensor_occupancy
from: "off"
to: "on"
- trigger: state
entity_id:
- binary_sensor.living_room_presence_detector_presence
from: "off"
to: "on"
conditions:
- condition: sun
before: sunrise
after: sunset
actions:
- choose:
- conditions:
- condition: sun
after: sunset
- condition: time
before: "22:00:00"
sequence:
- action: light.turn_on
metadata: {}
data:
brightness_pct: 100
target:
device_id: 7ad58cc1430b10beee788b774bf55e59
- conditions:
- condition: sun
before: sunrise
- condition: time
after: "22:00:00"
sequence:
- action: light.turn_on
metadata: {}
data:
brightness_pct: 50
target:
device_id: 7ad58cc1430b10beee788b774bf55e59
- conditions:
- condition: state
entity_id: binary_sensor.living_room_presence_detector_presence
state: "off"
for:
hours: 0
minutes: 2
seconds: 0
sequence:
- action: light.turn_on
metadata: {}
data:
brightness_pct: 50
target:
device_id: 7ad58cc1430b10beee788b774bf55e59
- conditions:
- condition: state
entity_id: binary_sensor.living_room_presence_detector_presence
state: "off"
for:
hours: 0
minutes: 4
seconds: 0
sequence:
- action: light.turn_off
metadata: {}
data: {}
target:
device_id: 7ad58cc1430b10beee788b774bf55e59
mode: single
So that has a scenario where it makes sense, but make sure you make a flow chart and know what it's doing is what you want to do.
I'm thinking the light is going to be going on and off a lot.