Not sure if I completely understand the and/or function of conditions or my formatting is wrong, but I can’t get this to work. Looking for a little guidance.
I have a PIR sensor that turns the bathroom light on, but I’m trying to add additional conditions to produce different brightness at different time windows.
You can use conditions combined with different actions, but then you have to narrow down the conditions.
Say:
Between 16:00 and 08:00 lights on ‘brightness: 229’
Between 23:30 and 05:00 lights on ‘brightness: 76’
The second will dim the lights back to 76/255th if it is between 23:30 and 05:00.
However the easier and more flexible way is to use the rather new CHOOSE option. It will explain itself once you pick the CHOOSE option, give your condition, followed by your action.
My preference would be to use a data_template to compute the desired brightness.
- alias: "Bathroom Light PIR"
trigger:
- platform: state
entity_id: binary_sensor.bathroom_pir
to: 'on'
condition:
condition: not
conditions:
- condition: time
after: '08:00'
before: '16:00'
action:
- service: light.turn_on
data_template:
entity_id: light.bathroom_light
brightness: >
{% set t = (now().time() | string)[:5] %}
{% if '16:00' <= t < '23:30' %} 229
{% elif '23:30' <= t < '05:00' %} 76
{% elif '05:00' <= t < '08:00' %} 229
{% else %} 0
{% endif %}
The condition excludes the time period between 08:00 and 16:00.
The brightness template gets the current local time as a string (like 07:35) and proceeds to compare it to three time ranges.
Thanks everyone, this really helps. Going to test run the data_template method as it’s more compact. Much appreciated. I don’t use code generators, purely experimenting based on other codes and writing it myself (well, trying )
I’m trying to implement the “1st” option, but somehow the light does’t turn on the light between 22:00h and sunrise.
Looks like I’m missing or misinterpreting something, right?
- id: motion_sensor
alias: "Motion Sensor"
trigger:
- entity_id: binary_sensor.tradfri_motion_sensor_2
platform: state
to: 'on'
action:
- choose:
# BETWEEN sunrise and 22:00
- conditions:
- condition: sun
after: sunrise
- condition: time
before: '22:00'
sequence:
- service: light.turn_on
data:
entity_id: light.color_temperature_light_2
brightness_pct: 100
color_temp: 250
transition: 5
# BETWEEN 22:00 and sunrise
- conditions:
- condition: time
after: '22:00'
- condition: sun
before: sunrise
sequence:
- service: light.turn_on
data:
entity_id: light.color_temperature_light_2
brightness_pct: 10
color_temp: 454
transition: 5
# A DEFAULT WHEN NOTHING ABOVE IS TRUE IN CONDITION
default:
- service: light.turn_off
data_template:
entity_id: light.color_temperature_light_2
Thanks for the help!
Btw, I’m turning the light off after 3 min when motion sensor is off, with an automation.