Home Assistant light automations

Some background

Automations

input_number:
brightness_bathroom:
name: Brightness Bathroom
min: 0
max: 100
step: 1
brightness_kitchen_table:
name: Brightness Kitchen Table
min: 0
max: 100
step: 1
brightness_kitchen_ceiling:
name: Brightness Kitchen ceiling
min: 0
max: 100
step: 1
input_datetime:
lights_off_timer_bathroom:
name: Timer lights off - Bathroom
has_date: false
has_time: true
lights_off_timer_kitchen_table:
name: Timer lights off - Kitchen Table
has_date: false
has_time: true
lights_off_timer_kitchen_ceiling:
name: Timer lights off - Kitchen ceiling
has_date: false
has_time: true
- id: motion_on_bathroom
alias: Motion on - Bathroom
trigger:
- entity_id: binary_sensor.motion_bathroom
platform: state
from: 'off'
to: 'on'
action:
- event: motion_on_bathroom
- id: motion_off_bathroom
alias: Motion off - Bathroom
trigger:
- entity_id: binary_sensor.motion_bathroom
platform: state
from: 'on'
to: 'off'
for: "{{ states('input_datetime.lights_off_timer_bathroom') }}"
action:
- event: motion_off_bathroom
- id: motion_on_kitchen_1
alias: Motion on - Kitchen Sensor 1
trigger:
- entity_id: binary_sensor.motion_kitchen_1
platform: state
from: 'off'
to: 'on'
action:
- event: motion_on_kitchen
- event: motion_on_kitchen_sensor_1
- id: motion_on_kitchen_2
alias: Motion on - Kitchen Sensor 2
trigger:
- entity_id: binary_sensor.motion_kitchen_2
platform: state
from: 'off'
to: 'on'
action:
- event: motion_on_kitchen
- event: motion_on_kitchen_sensor_2
- id: motion_off_kitchen
alias: Motion off - kitchen
trigger:
- entity_id: group.kitchen_motion_sensors
platform: state
from: 'on'
to: 'off'
for: "{{ states('input_datetime.lights_off_timer_kitchen_table') }}"
action:
- event: motion_off_kitchen
- id: motion_off_kitchen_1
alias: Motion off - kitchen 1
trigger:
- entity_id: binary_sensor.motion_kitchen_1
platform: state
from: 'on'
to: 'off'
for: "{{ states('input_datetime.lights_off_timer_kitchen_table') }}"
condition:
- condition: state
entity_id: binary_sensor.motion_kitchen_2
state: 'off'
action:
- event: motion_off_kitchen
- id: motion_off_kitchen_2
alias: Motion off - kitchen 2
trigger:
- entity_id: binary_sensor.motion_kitchen_2
platform: state
from: 'on'
to: 'off'
for: "{{ states('input_datetime.lights_off_timer_kitchen_table') }}"
condition:
- condition: state
entity_id: binary_sensor.motion_kitchen_1
state: 'off'
action:
- event: motion_off_kitchen
- id: lights_on_bathroom
alias: Lights On - Bathroom
trigger:
- platform: event
event_type: motion_on_bathroom
- platform: event
event_type: lights_on_bathroom
action:
- event: lights_on
event_data_template:
entity_id: light.dimmer_bathroom
light_source: bathroom
- id: lights_off_bathroom
alias: Lights Off - Bathroom
trigger:
- platform: event
event_type: motion_off_bathroom
- platform: event
event_type: lights_off_bathroom
- platform: event
event_type: lights_off_all
- platform: event
event_type: lights_off_away
- platform: event
event_type: lights_off_night
action:
- event: lights_on
event_data_template:
entity_id: light.dimmer_bathroom
light_source: bathroom
- id: lights_on
alias: Lights On
mode: parallel
trigger:
- platform: event
event_type: lights_on
action:
- choose:
- conditions:
- condition: template
value_template: "{{ trigger.event.data.entity_id[:6] == 'switch' }}"
- condition: template
value_template: "{{ states(trigger.event.data.entity_id) == 'off' }}"
sequence:
- service: switch.turn_on
data_template:
entity_id: "{{ trigger.event.data.entity_id }}"
- conditions:
- condition: template
value_template: "{{ trigger.event.data.entity_id[:5] == 'light' }}"
- condition: or
conditions:
- condition: template
value_template: "{{ states(trigger.event.data.entity_id) == 'off' }}"
- condition: template
value_template: >
{% set brightness = 'input_number.brightness_' ~ trigger.event.data.light_source %}
{{ (state_attr(trigger.event.data.entity_id, 'brightness') | float / 255 * 100) | round(0) != states(brightness) | float }}
sequence:
- service: light.turn_on
data_template:
entity_id: "{{ trigger.event.data.entity_id }}"
brightness_pct: >
{% set brightness = 'input_number.brightness_' ~ trigger.event.data.light_source %}
{{ states(brightness) | float }}
- id: lights_off
alias: Lights Off
mode: parallel
trigger:
- platform: event
event_type: lights_off
condition:
condition: template
value_template: "{{ states(trigger.event.data.entity_id) == 'on' }}"
action:
- service: homeassistant.turn_off
data_template:
entity_id: "{{ trigger.event.data.entity_id }}"

Conditions

Pause and unpause automations

- id: pause_auto_off
alias: Pause Auto Off
mode: queued
trigger:
- platform: event
event_type: pause_auto_off
condition:
- condition: template
value_template: '{{ states("automation.lights_off_" ~ trigger.event.data.light_source) == "on" }}'
action:
- service: homeassistant.turn_off
data_template:
entity_id: "automation.lights_off_{{ trigger.event.data.light_source }}"
- id: pause_auto_on
alias: Pause Auto On
mode: queued
trigger:
- platform: event
event_type: pause_auto_on
condition:
- condition: template
value_template: '{{ states("automation.lights_on_" ~ trigger.event.data.light_source) == "on" }}'
action:
- service: homeassistant.turn_off
data_template:
entity_id: "automation.lights_on_{{ trigger.event.data.light_source }}"
- id: unpause_auto_on
alias: UnPause Auto On
mode: queued
trigger:
- platform: event
event_type: unpause_auto_on
condition:
- condition: template
value_template: '{{ states("automation.lights_on_" ~ trigger.event.data.light_source) == "off" }}'
action:
- service: homeassistant.turn_on
data_template:
entity_id: "automation.lights_on_{{ trigger.event.data.light_source }}"
- id: unpause_auto_off
alias: UnPause Auto Off
mode: queued
trigger:
- platform: event
event_type: unpause_auto_off
condition:
- condition: template
value_template: '{{ states("automation.lights_off_" ~ trigger.event.data.light_source) == "off" }}'
action:
- service: homeassistant.turn_on
data_template:
entity_id: "automation.lights_off_{{ trigger.event.data.light_source }}"

Set timer and dimmer level

- id: set_lights_off_timer
alias: Set lights off timer
mode: queued
trigger:
- platform: event
event_type: set_lights_off_timer
condition:
- condition: template
value_template: >
{% set timer = 'input_datetime.lights_off_timer_' ~ trigger.event.data.light_source %}
{{ trigger.event.data.off_timer != states(timer) }}
action:
- service: input_datetime.set_datetime
data_template:
entity_id: input_datetime.lights_off_timer_{{ trigger.event.data.light_source }}
time: "{{ trigger.event.data.off_timer }}"
- id: set_dimmer_level
alias: Set dimmer level
mode: queued
trigger:
- platform: event
event_type: set_dimmer_level
condition:
- condition: template
value_template: >
{% set brightness = 'input_number.brightness_' ~ trigger.event.data.light_source %}
{{ trigger.event.data.dimmer_level | int != states(brightness) | int }}
action:
- service: input_number.set_value
data_template:
entity_id: input_number.brightness_{{ trigger.event.data.light_source }}
value: "{{ trigger.event.data.dimmer_level | float }}"
- event: pause_auto_on
event_data_template:
light_source: bathroom
- event: unpause_auto_on
event_data_template:
light_source: kitchen_table
- event: set_dimmer_level
event_data_template:
light_source: toilet
dimmer_level: 30
- event: set_lights_off_timer
event_data_template:
light_source: toilet
off_timer: "00:02:00"

Variables

  action:
# Toilet
- event: set_dimmer_level
event_data_template:
light_source: toilet
dimmer_level: "{{ states('input_number.brightness_medium') }}"
- event: set_lights_off_timer
event_data_template:
light_source: toilet
off_timer: "{{ states('input_datetime.delay_short') }}"
# Bathroom
- event: set_dimmer_level
event_data_template:
light_source: bathroom
dimmer_level: "{{ states('input_number.brightness_medium') }}"
- event: set_lights_off_timer
event_data_template:
light_source: bathroom
off_timer: "{{ states('input_datetime.delay_medium') }}"
# Hallway
- event: set_dimmer_level
event_data_template:
light_source: hallway
dimmer_level: "{{ states('input_number.brightness_high') }}"
- event: set_lights_off_timer
event_data_template:
light_source: hallway
off_timer: "{{ states('input_datetime.delay_long') }}"

Administration

Lovelace view of the office lights
  • The lighs are turned on (and has been so for 4 hours)
  • The input_number.brightness is set to 75% (and it was set to this value a day ago)
  • There is a 30 minute timeout after motion is detected before the lights are turned off (and this timeout was changed 2 days ago)
  • The “Lights on”-automation was turned on two hours ago
  • The “Lights off”-automation was turned off just now
Varibale for brightness and timeouts used throughout the automations
- event: set_reenable_time
event_data_template:
automation: lights_on_bathroom
in: 2h
- event: set_reenable_time
event_data_template:
automation: lights_off_bathroom
reenable_time: "2020-08-14 13:19:00"
Reenable time

--

--

--

Noe over gjennomsnittlig interessert. Kjentmann i IP- og nettverksjungelen, og jobber i nLogic AS.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Python with hidden sources

Setting up Visual Studio Code with Docker as a development environment for Python

What Programming language should I learn ?

Word Suggestion using NLTK

How Do Mobile App Analytics Work? Are They Different From Web Analytics?

How are mobile analytics different than web analytics?

BigQuery Dataset Metadata Queries

My H1–212 CTF writeup

#30DaysOfCode Day 6: Project Submitted

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ola Thoresen

Ola Thoresen

Noe over gjennomsnittlig interessert. Kjentmann i IP- og nettverksjungelen, og jobber i nLogic AS.

More from Medium

The art of photography

Why should I create a program to protect my eyes?

Notes on Jim Kwik’s Unleash Your Super Brain to Learn Faster

Mac keyboard shortcuts for moving app windows