Exploring Smart Lighting Automation with Template Sensors

I’ve been diving into creating more sophisticated lighting automations in Home Assistant, and I wanted to share my approach and some questions I have along the way. My goal is to move beyond simple motion-based lighting and create something more dynamic and context-aware.

Here’s the setup: I’ve been using a combination of template binary sensors and light entities with timers. The idea is that when the template sensor turns “on,” the light turns on, and when it turns “off,” the automation starts or restarts a timer. Once the timer runs out, the light turns off. This allows for more nuanced control compared to just reacting to motion.

One key aspect of this setup is the use of attributes on the binary sensor. For example, I have an “eligible” attribute that determines whether the light should turn on when the sensor state changes to “on.” If “eligible” is false, the light immediately turns off instead of waiting for the timer to expire. This is really useful for scenarios like bedtime, where you might want lights off immediately rather than after a delay.

Here’s a sample of one of the template sensors I’m using:
yaml

  • name: kitchen_sink_light_settings
    state: >
    {% set motion = is_state(‘binary_sensor.kitchen_motion_occupancy_group’, ‘on’) %}
    {% set dark_outside = is_state(‘binary_sensor.is_dark_outside’, ‘on’) %}
    {% set shade_closed = is_state(‘cover.kitchen_covers’, ‘closed’) %}
    {{ motion and (dark_outside or shade_closed) }}
    attributes:
    color_name: “white”
    normal_brightness: 60
    low_brightness: 30
    transition: 3
    eligible: >
    {% set someone_home = is_state(‘binary_sensor.someone_is_home’, ‘on’) %}
    {% set auto = is_state(‘input_select.home_ambiance’, ‘Automatic’) %}
    {% set sleeping = is_state(‘input_boolean.sleeping’, ‘on’) %}
    {% set movie = is_state(‘input_boolean.watching_movie’, ‘on’) %}
    {{ someone_home and auto and not sleeping and not movie }}

This setup allows me to control not just when the light turns on, but also its brightness and color based on different conditions. The automation blueprint I’ve created triggers on state changes and specific attributes, but I’m not entirely sure if I’m using the right approach for monitoring attribute changes like “eligible.”

My current triggers look like this:
yaml
trigger:

  • platform: state
    entity_id: !input trigger_sensor
    to: “on”
    id: “turn-on”
  • platform: state
    entity_id: !input trigger_sensor
    attribute: color_name
    id: “color-changed”
  • platform: state
    entity_id: !input trigger_sensor
    to: “off”
    id: “pending-off”
  • platform: state
    entity_id: !input trigger_sensor
    attribute: eligible
    id: “eligible”
  • platform: event
    id: “timer-finished”
    event_type: timer.finished
    event_data:
    entity_id: !input light_timer
  • platform: state
    entity_id: binary_sensor.lights_should_be_low
    id: “brightness”

In the actions, I retrieve the “eligible” attribute using eligible_var: "{{ state_attr(trigger_sensor_var, 'eligible') }}". However, I’m not sure if this is the most efficient way to monitor changes in the “eligible” attribute. Should I be using a different trigger or approach to detect when “eligible” changes from true to false or vice versa?

I’d love to hear from others who have implemented similar automations or have insights into optimizing this setup. Any tips or alternative methods would be greatly appreciated!