Wow! Thank you, that’s so much tidier
I don’t think I do, my way of ‘rejecting’ light.bathroom_pendant
which has a different daytime default brightness was to remove it from lights_on_lt_day_threshold
. I’m not sure how to explain so here’s a screenshot.
Previously light.bathroom_pendant
was in lights_on_lt_day_threshold
and I was planning on using rejectattr()
/ reject()
so that I could set a different brightness for it compared to the other lights in the lights_on_lt_day_threshold
list, but settled on this method instead.
Also, can I ask - probably a really dumb question, but - what’s better/different about using target
for service calls rather than data
or data-template
?
You’ll be pleased to know I complicated the automation main even further by adding an additional default brightness for later on in the evening:
# Default brightness
- alias: default_brightness
mode: parallel
max: 100
trigger:
- platform: homeassistant
event: start
- platform: sun
event: sunrise
- platform: sun
event: sunset
- platform: template
value_template: >
{% set now_time = now().timestamp() | timestamp_custom("%H:%M", true) %}
{% set sunset_offset_1hr = (as_timestamp(state_attr("sun.sun", "next_setting")) + (60 * 60)) | timestamp_custom("%H:%M", true) %}
{% set sunrise_time = as_timestamp(state_attr("sun.sun", "next_rising")) | timestamp_custom("%H:%M", true) %}
{{ ("21:00" <= now_time <= "23:59") or ("00:00" <= now_time <= sunrise_time)
if sunset_offset_1hr < "21:00"
else (sunset_offset_1hr <= now_time <= "23:59") or ("00:00" <= now_time <= sunrise_time) }}
- platform: state
entity_id:
- light.hallway_pendant_zha
- light.bathroom_pendant_zha
- light.bedroom_pendant_zha
action:
- wait_template: >-
{% set ignore = ["unknown", "unavailable"] %}
{{ states("light.hallway_pendant_zha") not in ignore
and states("light.bathroom_pendant_zha") not in ignore
and states("light.bedroom_pendant_zha") not in ignore
and states("light.hallway_pendant") not in ignore
and states("light.bathroom_pendant") not in ignore
and states("light.bedroom_pendant") not in ignore }}
- variables:
# Is it 1 hour after sunset or 21:00 (whichever comes first) and before sunrise?
late_night: >
{% set now_time = now().timestamp() | timestamp_custom("%H:%M", true) %}
{% set sunset_offset_1hr = (as_timestamp(state_attr("sun.sun", "next_setting")) + (60 * 60)) | timestamp_custom("%H:%M", true) %}
{% set sunrise_time = as_timestamp(state_attr("sun.sun", "next_rising")) | timestamp_custom("%H:%M", true) %}
{{ ("21:00" <= now_time <= "23:59") or ("00:00" <= now_time <= sunrise_time)
if sunset_offset_1hr < "21:00"
else (sunset_offset_1hr <= now_time <= "23:59") or ("00:00" <= now_time <= sunrise_time) }}
# 80% brightness
day_threshold: 204
# 50% brightness
night_threshold: 128
# 25% brightness
late_night_threshold: 77
# Lights on <= day threshold 80% brightness
lights_on_lt_day_threshold: >
{{ expand("light.hallway_pendant", "light.bedroom_pendant")
| selectattr("state", "eq", "on")
| selectattr("attributes.brightness", "le", day_threshold)
| map(attribute="entity_id") | list | join(", ") }}
# Lights on >= night threshold 50% brightness
lights_on_gt_night_threshold: >
{{ expand("light.hallway_pendant", "light.bathroom_pendant", "light.bedroom_pendant")
| selectattr("state", "eq", "on")
| selectattr("attributes.brightness", "ge", night_threshold)
| map(attribute="entity_id") | list | join(", ") }}
# Lights on >= late night threshold 25% brightness
lights_on_gt_late_night_threshold: >
{{ expand("light.hallway_pendant", "light.bathroom_pendant", "light.bedroom_pendant")
| selectattr("state", "eq", "on")
| selectattr("attributes.brightness", "ge", late_night_threshold)
| map(attribute="entity_id") | list | join(", ") }}
# Sun above or below horizon?
sun_above: '{{ is_state("sun.sun", "above_horizon") }}'
sun_below: '{{ is_state("sun.sun", "below_horizon") }}'
- choose:
# IF triggered by a light turning on
- conditions: '{{ trigger.platform == "state" and trigger.from_state.state == "off" }}'
sequence:
- service: light.turn_on
target:
entity_id: '{{ trigger.entity_id.split("_zha")[0] if "_zha" in trigger.entity_id else trigger.entity_id }}'
data:
brightness_pct: >-
{% if sun_above %}
{{ 100 if trigger.entity_id == "light.bathroom_pendant_zha" else 80 }}
{% else %}
{{ 30 if late_night else 50 }}
{% endif %}
# IF triggered by sunrise or ha start and sun is above the horizon
- conditions: '{{ trigger.event in ["sunrise", "start"] and sun_above }}'
sequence:
- service: light.turn_on
target:
entity_id: '{{ lights_on_lt_day_threshold if lights_on_lt_day_threshold | length > 0 else "none" }}'
data:
brightness_pct: 80
transition: 300
- service: light.turn_on
target:
entity_id: >
{{ light.bathroom_pendant if is_state("light.bathroom_pendant", "on")
and state_attr("light.bathroom_pendant", "brightness") < 255 else "none" }}
data:
brightness_pct: 100
transition: 300
# IF triggered by sunset, ha start or late_night trigger template and sun is below the horizon
- conditions:
- '{{ trigger.event in ["sunset", "start"] or trigger.platform == "template" and sun_below }}'
- '{{ lights_on_gt_late_night_threshold | length > 0 if late_night else lights_on_gt_night_threshold | length > 0 }}'
sequence:
- service: light.turn_on
target:
entity_id: '{{ lights_on_gt_late_night_threshold if late_night else lights_on_gt_night_threshold }}'
data:
brightness_pct: '{{ 30 if late_night else 50 }}'
transition: 300