Automatic smart light brightness and color temperature based on the sun

Not too bright, not too dim, not too cool, and not too warm.

I use Home Assistant to set my smart light brightness and colour temperature to different values based on the time of day and position of the sun. This (apparently) helps keep your lights in line with your circadian rhythms for better sleep and mental alertness.

This is done with a combination of template sensors, automations and a HACS integration called Circadian Lighting. Check out the YouTube video I made about this for more information!

Here are the various YAML code samples I use in the video for easy copy/pasting.

Template Sensors for Colour and Brightness

You’ll need to add these to your Home Assistant configuration.yaml file.

sensor:
  - platform: template
    sensors:
      light_brightness:
        friendly_name: 'Light Brightness'
        value_template: >
          {% if (now().hour >= 23) or (now().hour <= 7) %}
            10
          {% else %}
            30
          {% endif %}
        
      light_colour_temp:
        friendly_name: 'Light Colour Temp'
        value_template: >
          {% if states('sun.sun') == 'below_horizon' %}
            375
          {% else %}
            200
          {% endif %}

Automations to turn on lights at these brightness and color temperature levels

This is the YAML of one of my Motion Detection automations that turns on different numbers of lights based on the time of day, and at the right colour temperature and brightness. It will then turn them off again once motion is no longer detected. You’ll need to substitute your own entities in here, it’s only meant as a guide.

alias: "Light: Turn ON Lounge-First Stairwell Lights when Motion Detected"
description: ""
trigger:
  - platform: state
    entity_id: group.lounge_first_stairwell_presence_occupancy_group
    from: "off"
    to: "on"
condition:
  - type: is_illuminance
    condition: device
    entity_id: sensor.first_floor_presence_sensor_illuminance_lux
    domain: sensor
    below: 50
action:
  - service: light.turn_on
    data:
      transition: 2
      brightness_pct: "{{ states('sensor.light_brightness') }}"
      color_temp: "{{ states('sensor.light_colour_temp') }}"
    target:
      entity_id: light.lounge_first_light_strip
  - if:
      - condition: time
        after: "08:00:00"
        before: "23:00:00"
    then:
      - service: light.turn_on
        data:
          transition: 5
          brightness_pct: "{{ states('sensor.light_brightness') }}"
          color_temp: "{{ states('sensor.light_colour_temp') }}"
        target:
          entity_id: light.hallway_light_stairs
  - wait_for_trigger:
      - platform: state
        entity_id: group.lounge_first_stairwell_presence_occupancy_group
        from: "on"
        to: "off"
  - service: light.turn_off
    data:
      transition: 12
    target:
      entity_id:
        - light.hallway_light_stairs
        - light.lounge_first_light_strip
mode: restart

My Circadian Lighting Configuration

This will need to go into your configuration.yaml file after you have installed the Circadian Lighting HACS plugin.

circadian_lighting:

switch:

  - platform: circadian_lighting
    name: "Alan Office Circadian"
    lights_ct:
      - light.alan_office_corner_strip
      - light.alan_office_desk_lamp

  - platform: circadian_lighting
    name: "Main Lights Circadian"
    lights_ct:
      - light.kitchen_light_strip_east
      - light.kitchen_light_strip_west