Skip to content

Instantly share code, notes, and snippets.

@niemyjski
Created December 15, 2020 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niemyjski/6bf7e08588d194916a0a25f11088dc8c to your computer and use it in GitHub Desktop.
Save niemyjski/6bf7e08588d194916a0a25f11088dc8c to your computer and use it in GitHub Desktop.
homeassistant blueprint - Turn on a switch when motion is detected and Illuminance is low.
blueprint:
name: Turn the switch on when motion is detected
description: Turn on a switch when motion is detected and Illuminance is low.
domain: automation
source_url: https://github.com/niemyjski/home-assistant-config/tree/master/blueprints/automation/niemyjski/motion_switch.yaml
input:
motion:
name: Motion Sensor
description: The motion sensor wich triggers the light to turn on
selector:
target:
entity:
domain: binary_sensor
device_class: motion
switch:
name: Switch
selector:
target:
entity:
domain: switch
illuminance:
name: Illuminance
description: The illuminance sensor
selector:
entity:
domain: sensor
device_class: illuminance
illuminance_threshold_below:
name: Illuminance Threshold
description: Turn on the light after motion is detected and illuminance is below lx
default: 3000
selector:
number:
min: 0
max: 100000
unit_of_measurement: lx
sleep_mode:
name: Sleep Mode (Optional)
default: False
selector:
entity:
domain: input_boolean
variables:
motion_entities: !input motion
sleep_mode_input: !input sleep_mode
trigger:
- platform: state
entity_id: !input motion
from: 'off'
to: 'on'
- platform: state
entity_id: !input illuminance
- platform: state
entity_id: !input sleep_mode
from: 'on'
to: 'off'
condition:
- "{{ expand(motion_entities) | selectattr('state', 'eq', 'on') | list | count > 0 }}"
- condition: numeric_state
entity_id: !input illuminance
below: !input illuminance_threshold_below
- "{{ not sleep_mode_input or is_state(sleep_mode_input, 'off') }}"
action:
- service: switch.turn_on
target: !input switch
@niemyjski
Copy link
Author

This is a WIP. There are some issues when an area is selected.

@clempat
Copy link

clempat commented Dec 15, 2020

Any reason to take switch vs light ?

@clempat
Copy link

clempat commented Dec 15, 2020

I guess next step will be to turn it off or you separate the logic ?

@niemyjski
Copy link
Author

@clempat, you can't select multiple domains with blueprints today so if your controlling method is a switch you'll have to use one or the other, they are exactly the same minus the domain :.

I also found it much more intuitive to separate the logic for turning on/off. That way you can compose multiple automations for turning on lights and have one smart one for turning off. Does kind of complicate blueprints a little bit but your automations are much easier to debug and work with. Now you only ask yourself. Why didn't it turn on or why didn't it turn off.

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