💡 Simple automated lights with configurable timer

:bulb: Simple automated lights with configurable timer

In a fairly short amount of time I’ve helped a few with a common setup and wanted to share my version of it. My setup is heavily borrowed from the documentation and inspired by many of our community.
My full configuration is available at Github.com/Landrash/homeassistant-config

This setup consists of:

Requirements

Example of view
automation

################################################
## Group
################################################

group:
  timer_lights_exterior:
    control: hidden
    entities:
      - automation.lights_exterior_time_night_off
      - input_datetime.lights_exterior_time_night_off
      - automation.lights_exterior_time_night_on
      - input_datetime.lights_exterior_time_morning_on

################################################
## Automation
################################################

automation:
  - alias: lights_exterior_time_night_off
    trigger:
      - platform: template
        value_template: "{{ states.sensor.time.state == (states.input_datetime.lights_exterior_time_night_off.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
      - platform: sun
        event: sunrise
        offset: '-00:15:00'
    action:
      - service: homeassistant.turn_off
        entity_id: group.back_porch

  - alias: lights_exterior_time_night_on
    trigger:
      - platform: template
        value_template: "{{ states.sensor.time.state == (states.input_datetime.lights_exterior_time_morning_on.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
      - platform: sun
        event: sunset
        offset: '-00:15:00'
    condition:
      condition: or
      conditions:
        - condition: sun
          after: sunset
          after_offset: '-00:30:00'
        - condition: sun
          before: sunrise
          after_offset: '-00:01:00'
    action:
      - service: light.turn_on
        data:
          entity_id: group.back_porch
          brightness_pct: '40'

################################################
## Input Date/time
################################################

input_datetime:
  lights_exterior_time_night_off:
    has_time: true
    initial: '22:35'
  lights_exterior_time_morning_on:
    has_time: true
    initial: '05:45'

Source for this package configuration can be found at https://github.com/Landrash/homeassistant-config/blob/master/packages/automations/lights_exterior.yaml .

Best of luck and happy automating!

9 Likes

Cool! Simple setups are just as important!

Super cool.

I’m going to ask a could of very noob questions, and the of course implement this in to my HA. :slight_smile:

So the input “exterior light night time off”, is that when the exterior lights will turn off? and time on is when they would turn on? Seems backwards, but I think I’m missing something.

I see some logic dealing with sunset and sunrise in the automation as well. It looks like the lights turn off 15 min before sunrise. It also looks like they turn on 15 minutes before sunset, but not sure of the two conditions, what they do. Then overall I’m not sure what the inputs do at all. It’s the value template I’m still new at grasping.

Sorry can you ELI5 this just a little bit.

I gu

Only way to learn is to ask :wink:

I’ll try to explain with some code comments

# Automation for turning exterior light's off
# 2 triggers
#  - Triggers 15 minutes before sun rises (Night is over)
#  - At set time from sensor.input_datetime

  - alias: lights_exterior_time_night_off
    trigger:
      - platform: template
        value_template: "{{ states.sensor.time.state == (states.input_datetime.lights_exterior_time_night_off.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
      - platform: sun
        event: sunrise
        offset: '-00:15:00'
    action:
      - service: homeassistant.turn_off
        entity_id: group.back_porch


############################################


# Automation for turning exterior light's on
# 2 triggers
#  - Triggers 15 minutes before sun sunset 
#  - At set time from sensor.input_datetime before sun rises
#
# 2 conditions to combat lights being turned on in the morning after sun rises
# - Check that it's after sunset or at least 30 minutes before sunset.
# - Check that it's before sunrise or at least a minute before sunrise. (Had sunrise and trigger for on happening at the same minute and the results where lights on during the day)

  - alias: lights_exterior_time_night_on
    trigger:
      - platform: template
        value_template: "{{ states.sensor.time.state == (states.input_datetime.lights_exterior_time_morning_on.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
      - platform: sun
        event: sunset
        offset: '-00:15:00'
    condition:
      condition: or
      conditions:
        - condition: sun
          after: sunset
          after_offset: '-00:30:00'
        - condition: sun
          before: sunrise
          after_offset: '-00:01:00'
    action:
      - service: light.turn_on
        data:
          entity_id: group.back_porch
          brightness_pct: '40'

Awesome, so it either triggers with sunset logic OR manual input value. Very nice. I will be using this tonight!

1 Like