Skip to content

Instantly share code, notes, and snippets.

@Danielbook
Last active April 22, 2024 17:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Danielbook/ad8dacabb8dc01e7cf062cbd8c1a00ed to your computer and use it in GitHub Desktop.
Save Danielbook/ad8dacabb8dc01e7cf062cbd8c1a00ed to your computer and use it in GitHub Desktop.
Home Assistant blueprint. Motion-activated Light with illuminance, nightmode and dimmable
blueprint:
name: Motion-activated Light with illuminance, nightmode and dimmable
description: Turn on a light when motion is detected and illuminance is below a
set Lux level. The light will dim before it is turned off to signal that it has
not detected motion in quite a while. There is also two timers, on for daytime
and one for nighttime.
domain: automation
input:
motion_entity:
name: Motion Sensor
selector:
entity:
domain:
- binary_sensor
device_class:
- motion
multiple: false
lux_entity:
name: Illuminance Sensor
selector:
entity:
domain:
- sensor
device_class:
- illuminance
multiple: false
lux_level:
name: Illuminance level
description: If lux is below this value and motion is detected, the light will
turn on.
default: 100
selector:
number:
min: 0.0
max: 1000.0
step: 1.0
mode: slider
light_target:
name: Light
selector:
target:
entity:
- domain:
- light
dim_time:
name: Dim light
description: The light will start dim this many seconds before it is turned
off if no motion has been detected to signal that the light will soon turn
off.
default: 30
selector:
number:
min: 0.0
max: 120.0
unit_of_measurement: seconds
step: 1.0
mode: slider
no_motion_wait_day:
name: Wait time (day)
description: Time to leave the light on after last motion is detected during
the day.
default: 300
selector:
number:
min: 0.0
max: 600.0
unit_of_measurement: seconds
step: 1.0
mode: slider
no_motion_wait_night:
name: Wait time (night)
description: Time to leave the light on after last motion is detected during
the night.
default: 120
selector:
number:
min: 0.0
max: 600.0
unit_of_measurement: seconds
step: 1.0
mode: slider
night_time_start_helper:
name: Night time helper (Start)
description: When should it be considered night time? Create an input date time
helper and set when you want the night to start.
selector:
entity:
domain:
- input_datetime
multiple: false
night_time_end_helper:
name: Night time helper (End)
description: When should it be considered night time? Create an input date time
helper and set when you want the night to end.
selector:
entity:
domain:
- input_datetime
multiple: false
source_url: https://gist.github.com/Danielbook/ad8dacabb8dc01e7cf062cbd8c1a00ed
mode: restart
max_exceeded: silent
variables:
local_night_end: !input night_time_end_helper
local_night_start: !input night_time_start_helper
local_motion_delay_day: !input no_motion_wait_day
local_motion_delay_night: !input no_motion_wait_night
local_motion_dim_time: !input dim_time
trigger:
platform: state
entity_id: !input motion_entity
from: 'off'
to: 'on'
condition:
condition: numeric_state
entity_id: !input lux_entity
below: !input lux_level
action:
- service: light.turn_on
target: !input light_target
- wait_for_trigger:
platform: state
entity_id: !input motion_entity
from: 'on'
- delay: ' {% if today_at(states(local_night_end)) < now() or now() < today_at(states(local_night_start)) %}
{{ local_motion_delay_day - local_motion_dim_time }} {% else %} {{ local_motion_delay_night
- local_motion_dim_time }} {% endif %} '
- service: light.turn_off
target: !input light_target
data:
transition: !input dim_time
@f35ta
Copy link

f35ta commented Mar 5, 2023

I miss 2 things, to be able to set 2 different brightness values for when to turn the light on (one value daytime and another for night time). Currently my Hue Buld starts with the brightness value that were the last when turned off. With this script after the first run the upcoming times the light turns on it get the most dimmed value as turn on value for brightness and this results in a very low dimmed light when turned on de following motion detections.... With the start brightness parameters the light would also increase the brightness with a new motion detection when the light have started to dim based on no motion.

@Danielbook
Copy link
Author

I miss 2 things, to be able to set 2 different brightness values for when to turn the light on (one value daytime and another for night time). Currently my Hue Buld starts with the brightness value that were the last when turned off. With this script after the first run the upcoming times the light turns on it get the most dimmed value as turn on value for brightness and this results in a very low dimmed light when turned on de following motion detections.... With the start brightness parameters the light would also increase the brightness with a new motion detection when the light have started to dim based on no motion.

So for me I use the excellent Adaptive lightning integration, so that is not really an issue for me. Feel free to just extend my code and add with those ideas you have. But for me, they do not apply :)

@joostvanmourik
Copy link

joostvanmourik commented Apr 17, 2023

Would it be possile to use the illuminance from my Xiaomi sensor with this bleuprint? Thats will make it perfect

The latet version is almost perfect for me, if you can add the option to change the brightness during night time it will be perfect.

@saschabrockel
Copy link

@f35ta have you edited it to work with brightness?

@psi-4ward
Copy link

So the light goes on and lux_entity level rises cause it's bright now. If the motion triggers again the condition evaluates to false and the delay gets not refreshed. Wonder if it works for you?

@ChepV
Copy link

ChepV commented Apr 11, 2024

  • delay: ' {% if today_at(states(local_night_end)) < now() or now() < today_at(states(local_night_start)) %}

now() and now()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment