Skip to content

Instantly share code, notes, and snippets.

@WizBangCrash
Created April 20, 2021 20:54
Show Gist options
  • Save WizBangCrash/e6d67e8a2340b998914fcabe952748b9 to your computer and use it in GitHub Desktop.
Save WizBangCrash/e6d67e8a2340b998914fcabe952748b9 to your computer and use it in GitHub Desktop.
Occupancy Blueprint for Philips Hue SML001 motion sensor
#
# Blueprint to control a light (or lights) using a Philips Hue SML001
# motion sensor connected to Home Assistant via ZHA
#
# Home Assitant configures the sensor to be an Occupancy Sensor at boot,
# setting the Occupied to Unoccipied time and sensitivity of the sensor.
# I used to use HA timers to do this, but Blueprint is now much more simple :-)
#
# Additionally the sensor can be disabled using a oneof two entities.
# I usually link this to a TV state, as I do not want my lights going on & off
# while sitting in the Lounge watching TV.
# I also use an input_boolean to enable me to disable all my occupancy sensors.
#
blueprint:
name: Occupancy Detection Response (SML001)
description: |
The basic occupancy sensor automation does the following:
The sets the sensor attributes when Home Assistant boots
Turn on light(s) when occupancy detected and off when unoccupied.
Uses a different brightness at night
Optionally, choose a TV and the occupancy sensor will be ignored when the TV is on
domain: automation
input:
occupancy_entity:
name: Occupancy Sensor
description: "The occupancy sensor to use for this automation"
selector:
entity:
domain: binary_sensor
device_class: occupancy
ieee_id:
name: Occupancy Sensor Zigbee IEEE ID
description: "The zigbee network id of the sensor"
selector:
text:
o_to_u_delay:
name: Occupied to Unoccupied delay
description: |
Set the time in seconds we require the light(s) to stay on.
This delay is reset each time motion is detected.
NOTE: Restart Home Assistant on changing this value
default: 180
selector:
number:
min: 0
max: 1800
mode: box
unit_of_measurement: "seconds"
sensitivity:
name: Sensitivity of sensor
description: |
Set how sensitive we want the sensor to be: 0 = least sensitive
NOTE: Restart Home Assistant on changing this value
default: 2
selector:
number:
min: 0
max: 2
illuminance_entity:
name: Illuminance
description: "The luminance sensor to use"
selector:
entity:
domain: sensor
device_class: illuminance
luminance_value:
name: Light trigger level
description: "Anything darker than this will turn on the light"
default: 6
selector:
number:
min: 2
max: 3000
mode: box
unit_of_measurement: "lx"
light_entity:
name: Lights
description: The light(s) to control
selector:
entity:
domain: light
daytime_brightness:
name: Daytime Brightness
description: "The brightness to set the light during the day (100%)"
default: 75
selector:
number:
min: 10
max: 100
mode: box
unit_of_measurement: "%"
nighttime_brightness:
name: Nighttime Brightness
description: "The brightness to set the light at night(100%)"
default: 10
selector:
number:
min: 10
max: 100
mode: box
unit_of_measurement: "%"
daytime_colour:
name: Daytime colour temperature
description: "The colour temperature during the day (4500K)"
default: 4500
selector:
number:
min: 2200
max: 6500
step: 100
mode: box
unit_of_measurement: "Kelvin"
nighttime_colour:
name: Nighttime colour temperature
description: "The colour temperature during the night (3000K)"
default: 3000
selector:
number:
min: 2200
max: 6500
step: 100
mode: box
unit_of_measurement: "Kelvin"
disable_entity:
name: Input Boolean to use for disabling sensor
description: >
Set to an input_boolean. If the input_boolean is in the 'on' state
then the occupancy sensor will be disabled
default: {}
selector:
target:
entity:
domain: input_boolean
media_entity:
name: If this media player is on than disable occupancy sensor
description: >
Set to a media_player e.g. TV. If the media player is in the 'on' state
then the occupancy sensor will be disabled e.g.
media_player.lounge_tv
default: {}
selector:
target:
entity:
domain: media_player
#
# The automation
#
variables:
ieee_id: !input ieee_id
disable_entity: !input disable_entity
media_entity: !input media_entity
# Trigger mode
mode: single
# Trigger
trigger:
# When HA starts
- platform: homeassistant
event: start
# Occupancy sensor. Any state change
- platform: state
entity_id: !input occupancy_entity
# Conditions
condition:
# Using a template allows us to cater for either of these entities being "None"
- alias: "Exit if sensor is disabled"
condition: template
value_template: "{{ True if not disable_entity else is_state(disable_entity.entity_id, 'off') }}"
- alias: "Exit if media player on"
condition: template
value_template: "{{ True if not media_entity else is_state(media_entity.entity_id, 'off') }}"
# Actions
# TODO: Figure out how to get ieee address of sensor from entity
action:
- alias: "What caused the trigger?"
choose:
- conditions:
- condition: template
value_template: "{{ trigger.platform == 'homeassistant' }}"
sequence:
# Set device occupancy off delay
- service: zha.set_zigbee_cluster_attribute
data:
ieee: "{{ ieee_id }}"
endpoint_id: 2
cluster_id: 0x0406
cluster_type: in
attribute: 0x0010
value: !input o_to_u_delay
# Set device sensitivity
- service: zha.set_zigbee_cluster_attribute
data:
ieee: "{{ ieee_id }}"
endpoint_id: 2
cluster_id: 0x0406
cluster_type: in
attribute: 0x0030
value: !input sensitivity
- conditions:
- condition: template
value_template: >
{{
trigger.platform == 'state'
and trigger.to_state.state == 'on'
}}
- condition: numeric_state
entity_id: !input illuminance_entity
below: !input luminance_value
sequence:
- alias: "Night time or day time?"
choose:
- conditions:
- condition: time
after: "22:00:00"
before: "07:00:00"
sequence:
- service: light.turn_on
entity_id: !input light_entity
data:
brightness_pct: !input nighttime_brightness
kelvin: !input nighttime_colour
default:
- service: light.turn_on
entity_id: !input light_entity
data:
brightness_pct: !input daytime_brightness
kelvin: !input daytime_colour
- conditions:
- condition: template
value_template: >
{{
trigger.platform == 'state'
and trigger.to_state.state == 'off'
}}
sequence:
- service: light.turn_off
entity_id: !input light_entity
default: []
@cstrand89
Copy link

Hi! Is it possible to set this up so it works with more than one sensor? If I for example have two sensors in the same room? From the settings, it seems that I am only able to input one entity and one Zigbee IEEE ID? Thanks for a great blueprint!

@WizBangCrash
Copy link
Author

I've changed things quite a bit since I posted this one. I ended up splitting the blueprint into multiple blueprints as I had different requirements for different rooms and trying to make one blueprint do everything became a bit overwhelming.
I have moved the setup off the sensor delay and sensitivity into its own blueprint here
I have also created a new media room blueprint here. This would need some modifications to support multiple occupancy sensors, but would be possible. I may look at that when I get some time as I have been thinking of adding a second occupancy sensor to one of the rooms.
I added in killswitch functionality to stop the occupancy sensor turning on lights when the TV was on and to stop the lights turning off if they have been explicitly turned on at a switch.
Not sure it helps you much, but just thought I'd share them.

@cstrand89
Copy link

cstrand89 commented Jun 30, 2021

Thank you

@WizBangCrash
Copy link
Author

I forgot to add this one.
A combination of a Philips light switch with an occupancy sensor & killswitch.
Hope they help.

@markyeew
Copy link

Hi, hue sensor has an issue as it only updates the illuminance when motion is detected or after quite a long while. I am trying to add in a delay that will allow the illuminance to update first before it inputs the value to trigger the lights but I can't seem to find the right place.
hope you can help.

@WizBangCrash
Copy link
Author

@markyeew Hue motion sensors only report illuminance when light levels change by a few lux or on the back of a motion detected message. I'm not sure how to do what you're requesting,.
Why do you need to detect illuminance levels before inputting the value to trigger the lights?

@markyeew
Copy link

markyeew commented Jul 18, 2021

@WizBangCrash What i’m trying to do is get the light to come on below 10lux. Like a night light. So after the automation runs for the first time, the illuminance will be stuck at the high value (maybe 70lux) when the light was on and does not refresh until around a few minutes. When the second person enters the area during this time, the automation will not trigger as the motion is detected (but lux is still 70) and when the lux value updates to <10, the occupancy sensor is also already active and there is no change in state to trigger the automation. Hope it’s a bit clearer.

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