Hello everyone!
I would like to share with you my approach on light/sensor automation that I use here on my home.
The main idea is:
- turn the light on and off automatically
- wait until a programmable time is reached to automate the light
- if light is already on (manual input), do not turn off automatically (manual mode override)
- if light is turned off manually the automation stops and wait for the next trigger
- everything is on a single automation
This script uses the 'No motion since'
attribute from the Xiaomi sensor.
I have a couple of routines automation using sunset and sunrise that turns on and off these lights automation created for every room.
Here is everything on a single package file:
automation:
- alias: Bedroom Light
trigger:
platform: state
entity_id: binary_sensor.motion_sensor
to: 'on'
condition:
condition: state
entity_id: light.bed_light
state: 'off'
action:
- service: light.turn_on
entity_id: light.bed_light
- delay: 00:00:05 #delays five seconds so wait template can start properly
- wait_template: >
{% set light = "light.bed_light" -%} #set light entity
{%- set timmer_input = "input_select.bedroom" -%} #set input select entity
{%- set motion_sensor = "binary_sensor.motion_sensor" -%} #set motion sensor entity
{%- if is_state(light , 'off') or (state_attr(motion_sensor, 'No motion since') | int >= states(timmer_input) | int and is_state(motion_sensor , 'off')) -%}true{%- else -%}false{%- endif %} #if light is off or no motion equals timmer value
- condition: state #checks if light is still on
entity_id: light.bed_light
state: 'on'
- service: light.turn_off
entity_id: light.bed_light
- alias: Routine - Sunset
trigger:
platform: sun
event: sunset
offset: '-00:15:00'
action:
- service: homeassistant.turn_on
entity_id:
- automation.bedroom_light
- alias: Routine - Sunrise
trigger:
platform: sun
event: sunrise
offset: '-00:15:00'
action:
- service: homeassistant.turn_off
entity_id:
- automation.bedroom_light
input_select:
bed_light_timmer:
name: 'Bedroom'
options:
- 120
- 180
- 300
- 600
- 1200
initial: 180
icon: mdi:timer
Hope it helps!