Github Gist: Home Assistant BluePrint to turn on light, switch, scene or script based on motion and illuminance · GitHub
Importing
Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)
Or import this Blueprint by using the Gist URL.
Main features
Turn on a light, switch, scene or script based on motion detection, and (optionally) low illuminance level.
Also you are able to set a blocker entity, and time limits for the automation.
Only activating the automation on a certain sun state (below / above horizon is also possible.
Optionally, you can have the entity turn off after a certain amount of minutes of no motion detection.
Prerequisites
Some of the (optional) input values in this blueprint use helper entities you have to create yourself, to be able to dynamically set limits. It is not possible to set hardcoded limits for these inputs.
Optional features:
Is is also possible to define a blocking entity, which blocks the automation from running when this entity’s state is on. This could be for instance that sleep mode is enabled.
Time limits can also be defined to limit the time before and after the automation should trigger.
Outside of these limits the automation should not trigger.
If you do not configure the optional entities the automation will skip these conditions.
Optional entities:
- Illuminance sensor (sensor)
- Illuminance cutoff value (input_number)
- Blocking entity (any entity with state on/off)
- Time limit before (input_datetime)
- Time limit after (input_datetime)
- Wait time (input_number) [IN MINUTES!]
Defining optional entities in YAML: example code:
input_number:
illumination_bedroom_low_cutoff:
name: 'Illumination bedroom low cutoff value'
min: 1
max: 100
step: 1
bedroom_no_motion_turn_off_delay:
name: 'Time in minutes automation waits to turn off bedroom lights after no motion'
min: 1
max: 120
input_boolean:
sleepmode:
name: 'Sleepmode'
input_datetime:
bedroom_lights_turn_on_before_limit:
name: 'Bedroom lights no longer turn on before this time'
has_date: false
has_time: true
bedroom_lights_turn_on_after_limit:
name: 'Bedroom lights no longer turn on after this time'
has_date: false
has_time: true
Defining optional entities in the UI
If you want to create the optional entities in the UI instead of in YAML, you van do so in the following way:
- To to Configuration
- Go to Helpers
- Click the ‘Add helper’ button
- Select the entity type (in this case, number, toggle or date/time)
- Enter required fields
- Click on create
Setting the values for the helper entities
To set a value for your newly created helper, find it in the helper list as described in the section above, press the settings icon on the top right of the screen and set the value there.
F.A.Q.
The blueprint is not (fully) working, what is wrong?
Something that often goes wrong when using this blueprint, is using hardcoded numbers instead of helper entities where required. For instance the no_motion_wait and the illuminance_cutoff parameters have to be an input_number helper entity, instead of a hardcoded number. Please read the full post again to see how to create the helper entities.
If it is still not working, please post your generated automation YAML code from the file automations.yaml.
How do I make sure my light sets set to a specific brightness, color etc?
Do do this, define a scene with your required settings. Then use that scene as the target entity for this script. If you want to trigger specific scenes at different times of the day, create multiple automations from this blueprint with different target entities (scenes) and with different time limits.
My target entity is triggered (light on) but it does not turn off after the wait time
You are probably using a scene or script as a target entity.
If you do, you must also define a target off entity.
This is because HA cannot ‘turn off’ a scene, it does now know how to return to the previous state.
The turn of entity could be either a light or a light group for instance.
Why do I need to create helper entities for some of the conditions instead of hardcoded number in the blueprint?
Requiring the helpers is indeed increasing the effort to get this blueprint to work. It was a design choice I made in the beginning of it’s development. I wanted to be able to dynamically change thresholds without having to change the automation itself. I do not want to change this right now because this would break the automations for updating users. I also thought about supporting both hardcoded numbers and helpers, but I think the blueprint is already complex enough to be maintainable.
Changelog
- 2020-12-18: Initial version
- 2020-12-18.1: Fix for hardcoded time limits
- 2020-12-18.2: Refactored templates to be shorter, thanks to @123
- 2020-12-19: Simplified action template
- 2020-12-19.1: Added optional input to turn off entity after a certain amount of minutes of no motion
- 2020-12-20: Added group support, fixed time condition bug
- 2020-12-20.1: Made illumination sensor and cutoff value optional.
- 2020-12-22: Removed entity state ‘on’ condition and set automation mode to restart to allow for entity off timer restart.
- 2020-12-24: Added skip illuminance check if entity just turned on (when automation was restarted)
- 2020-12-28: Added turn off blocking entity.
- 2021-01-03: Fixed bug where time_limit_before did not work correctly when it was on the next day.
- 2021-01-03.1: Added possibility to define turn-off entity other than target_entity.
- 2021-01-08: Removed restriction of motion sensor needing to be binary_sensor in motion class. Now also possible to use groups of motion sensors. Also improved time condition.
- 2021-01-21: Swapped input fields for time after and before to be more logical
- 2021-09-02: Rewrote time condition to possibly fix time issue with blueprint not triggering when time window spans midnight.
- 2021-12-14: Added default value to int casts.
- 2022-03-20: Issues with scene as target entity not restarting automation on retrigger of motion sensor likely fixed.
- 2022-03-24: Fixed issue in conditions pertaining to missing illuminance check when using scene as triggering entity
- 2022-04-13: Added missing condition for non script/scene entities and no illuminance defined.
- 2022-04-29: Refactored to use state object directly less often.
- 2022-07-10: Fixed entity not turning off then no_motion_wait is undefined.
- 2022-11-01: Added (backwards compatible) optional condition for triggering automation based on sunstate (below/above horizon).
- 2022-11-08: Fixed turn off blocker entity state check being done before wait time.
- 2022-11-21: Add support for skipping illuminance check on secondary motion detection, for scene target entities that control a single entity_id.
If you have any issues or questions, please let me know