I moving all my lights from the Philips hue over to Home Assistant. Now I try to get the hang of using the motion sensor (PIR) to control the lights. With the help of Gemini I got a functioning YAML code, that works well. However, I tried several ways to integrate an additional condition, so that the light is not triggered when there is enough light. Every time the automation just didn't work anymore. I need help to understand how to deal with that.
Here's the functioning automation:
alias: Motion Hallway front (new)
description: >-
Turns on light based on time of day when motion is detected; turns off after 5
minutes of no motion.
triggers:
- type: occupied
device_id: 51c8ebb5a497aaafe3a816b8c092a313
entity_id: be0ab254feaa96064d1013129e4e8fef
domain: binary_sensor
trigger: device
id: motion_on
- type: not_occupied
device_id: 51c8ebb5a497aaafe3a816b8c092a313
entity_id: be0ab254feaa96064d1013129e4e8fef
domain: binary_sensor
for:
minutes: 5
trigger: device
id: motion_off
conditions: []
actions:
- choose:
- conditions:
- condition: trigger
id: motion_on
sequence:
- choose:
- conditions:
- condition: time
after: "23:00:00"
before: "07:00:00"
sequence:
- action: light.turn_on
target:
entity_id: light.lamp_hallway_front
data:
brightness_pct: 10
- conditions:
- condition: time
after: "20:00:00"
before: "23:00:00"
sequence:
- action: light.turn_on
target:
entity_id: light.lamp_hallway_front
data:
brightness_pct: 60
default:
- action: light.turn_on
target:
entity_id: light.lamp_hallway_front
data:
brightness_pct: 100
- conditions:
- condition: trigger
id: motion_off
sequence:
- action: light.turn_off
target:
entity_id: light.lamp_hallway_front
mode: restart
Here's the code snipped considering the brightness, I have based on the GUI:
I would at the condition to your original automation like below. The way it works is when the automation is triggered the choose option checks to which one it was. If it was not_occupied and it lasted for 5 minutes then the light is turned off. If it was occupied then it checks to see what additional conditions are met. The first is if the trigger occurred between 23:00 and 7:00. If so the light is turned on at a brightness of 10. The second is if the trigger occurred between 20:00 and 23:00. If so the light is turned on at a brightness of 60. The third is if the trigger occurred between 7:00 and 20:00. In addition, the illuminance must be above 11. If so the light is turned on at a brightness of 100. If none of the above conditions are met then as a default the light is turned on at a brightness of 100. Based on this you should be able to adjust the following code to meet your needs.
If you are interested, the following version takes advantage of Trigger Variables to move all of the control logic into each trigger (as opposed to employing a choose in actions). The result is just one action light.turn_on that sets the light to the brightness computed by the respective trigger's level variable (a value of 0 turns off the light).
(I don't have your light entity on my system so that's why it indicates 'Unknown entity')
In YAMl mode
alias: Motion Hallway front (new)
description: >-
Turns on light based on time of day when motion is detected; turns off after 3
minutes at night, and 5 minutes during the day/evening.
triggers:
- alias: Motion Detected
trigger: state
entity_id: binary_sensor.motion_sensor_philips_sml001_2
to: "on"
variables:
level: >
{% set h = now().hour %}
{% set is_low =
states('sensor.motion_sensor_philips_sml001_illuminance_2') | float(0) < 12 %}
{{ 10 if h < 7 or h == 23 else
60 if 20 <= h < 23 else
100 if 7 <= h < 20 and is_low else none }}
is_true: "{{ level is not none }}"
- alias: No Motion Night
trigger: state
entity_id: binary_sensor.motion_sensor_philips_sml001_2
to: "off"
for:
minutes: 2
variables:
level: 0
is_true: "{{ now().hour < 7 or now().hour == 23 }}"
- alias: No Motion day
trigger: state
entity_id: binary_sensor.motion_sensor_philips_sml001_2
to: "off"
for:
minutes: 5
variables:
level: 0
is_true: "{{ 7 <= now().hour < 23 }}"
conditions:
- alias: Can proceed to action
condition: template
value_template: "{{ is_true }}"
actions:
- alias: Turn light on/off
action: light.turn_on
target:
entity_id: light.lamp_hallway_front
data:
brightness_pct: "{{ level }}"
mode: restart