Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 53 You must be signed in to fork a gist
  • Save networkingcat/a1876d7e706e07c8bdcf974113940fb8 to your computer and use it in GitHub Desktop.
Save networkingcat/a1876d7e706e07c8bdcf974113940fb8 to your computer and use it in GitHub Desktop.
Homeassistant blueprint for motion-activated light scene
blueprint:
name: Yet Another Motion Automation
description: >
# YAMA V10
Turn on lights or scenes when motion is detected.
Four different scenes can be defined depending on time of day.
For Details see this forum post:
https://community.home-assistant.io/t/yama-yet-another-motion-automation-scenes-ambient-light-and-some-conditions/257062?u=networkingcat
Capabilitys:
- Trigger on motion (in fact can be triggered by anything that switches between “on” and off")
- Wait time for turning off
- Only run if entity is in desired state (optional)
- Sun elevation check (optional)
- 4 Scenes for different times of day (optional)
- Ambient support with time frame (optional)
- Default scene when motion stops (optional)
- “no motion blocker” with user choosable state (optional)
domain: automation
source_url: https://gist.github.com/networkingcat/a1876d7e706e07c8bdcf974113940fb8
input:
motion_entity:
name: Motion Sensor
description: Motion Sensor or a group with Motion Sensors (But can be anything switching between "on" and "off")
selector:
entity:
light_target:
name: Light
selector:
target:
entity:
domain: light
no_motion_wait:
name: Wait time
description: Time to leave the light on after last motion is detected.
default: 120
selector:
number:
min: 0
max: 3600
unit_of_measurement: seconds
automation_blocker:
name: Automation Blocker (Optional)
description: Only run if this boolean is in desired state (see next input)
default:
selector:
entity:
automation_blocker_boolean:
name: Automation Blocker Chooser (Optional)
description: Desired state of automation blocker, choose on for on and off for off
default: false
selector:
boolean:
no_motion_blocker:
name: No Motion Blocker (Optional)
description: No motion sequence is not run if this boolean is in desired state (see next input)
default:
selector:
entity:
no_motion_blocker_boolean:
name: No Motion Chooser (Optional)
description: Desired state of no motion blocker, choose on for on and off for off
default: false
selector:
boolean:
elevation_check:
name: Sun elevation check (Optional)
description: This is the angle between the sun and the horizon. Negative values mean the sun is BELOW the horizon.
default: none
selector:
number:
min: -90
max: 90
unit_of_measurement: degrees
scene_ambient:
name: Ambient Scene (Optional)
description: Scene for ambient state. Will be activated when no motion is detected.
default: scene.none
selector:
entity:
domain: scene
time_scene_ambient_start:
name: Ambient time frame start (Optional)
description: Time from which on ambient scene will be activated
default: "00:00:00"
selector:
time:
time_scene_ambient_end:
name: Ambient time frame end (Optional)
description: Time from which on ambient scene will be not activated
default: "00:00:00"
selector:
time:
scene_morning:
name: Scene for the morning (Optional)
default: scene.none
selector:
entity:
domain: scene
time_scene_morning:
name: Time for the morning scene (Optional)
description: A time input which defines the time from which on the scene will be activated if motion is detected.
default: "00:00:00"
selector:
time:
scene_day:
name: Scene for the bright day (Optional)
default: scene.none
selector:
entity:
domain: scene
time_scene_day:
name: Time for the day scene (Optional)
description: A time input which defines the time from which on the scene will be activated if motion is detected.
default: "00:00:00"
selector:
time:
scene_evening:
name: Scene for the evening (Optional)
default: scene.none
selector:
entity:
domain: scene
time_scene_evening:
name: Time for the evening scene (Optional)
description: A time input which defines the time from which on the scene will be activated if motion is detected.
default: "00:00:00"
selector:
time:
scene_night:
name: Scene for the dark night (Optional)
default: scene.none
selector:
entity:
domain: scene
time_scene_night:
name: Time for the night scene (Optional)
description: A time input which defines the time from which on the scene will be activated if motion is detectedd.
default: "00:00:00"
selector:
time:
scene_no_motion:
name: Default scene for no motion (Optional)
description: Set this Scene if you want to activate a scene if motion stops
default: scene.none
selector:
entity:
domain: scene
mode: restart
max_exceeded: silent
variables:
scene_ambient: !input scene_ambient
scene_morning: !input scene_morning
scene_day: !input scene_day
scene_evening: !input scene_evening
scene_night: !input scene_night
automation_blocker: !input automation_blocker
automation_blocker_boolean : !input automation_blocker_boolean
no_motion_blocker: !input no_motion_blocker
no_motion_blocker_boolean: !input no_motion_blocker_boolean
elevation_check: !input elevation_check
scene_no_motion: !input scene_no_motion
motion_entity: !input motion_entity
trigger:
- platform: state
entity_id: !input motion_entity
from: "off"
to: "on"
- platform: state
entity_id: !input motion_entity
from: "on"
to: "off"
for: !input no_motion_wait
# All Conditions must be true
condition:
# Check if automation blocker exist or is in desired state, otherwise do not run
- condition: or
conditions:
- "{{ automation_blocker == none }}"
- "{{ automation_blocker_boolean and states[automation_blocker].state == 'on' }}"
- "{{ not automation_blocker_boolean and states[automation_blocker].state == 'off' }}"
# Check if elevation input exist or is below desired value, otherwise do not run
- condition: template
value_template: "{{ (elevation_check == none) or (state_attr('sun.sun','elevation') <= elevation_check | float(90)) }}"
action:
- choose:
# Trigger to state == "on"
- conditions:
- condition: template
value_template: "{{ trigger.to_state.state == 'on' }}"
sequence:
- choose:
# Morning Scene
- conditions:
- "{{ scene_morning != 'scene.none'}}"
- condition: time
after: !input time_scene_morning
before: !input time_scene_day
sequence:
- scene: !input scene_morning
# Day Scene
- conditions:
- "{{ scene_day != 'scene.none'}}"
- condition: time
after: !input time_scene_day
before: !input time_scene_evening
sequence:
- scene: !input scene_day
# Evening Scene
- conditions:
- "{{ scene_evening != 'scene.none'}}"
- condition: time
after: !input time_scene_evening
before: !input time_scene_night
sequence:
- scene: !input scene_evening
# Night Scene
- conditions:
- "{{ scene_night != 'scene.none'}}"
- condition: time
after: !input time_scene_night
before: !input time_scene_morning
sequence:
- scene: !input scene_night
# If State -> "On" defaulting to turn light on
default:
- service: light.turn_on
target: !input light_target
# Trigger to state = "off"
- conditions:
- condition: template
value_template: "{{ trigger.to_state.state == 'off' }}"
# Check if no motion blocker exist or in in desired state, otherwise do not run any no motion action
- condition: or
conditions:
- "{{ no_motion_blocker == none }}"
- "{{ no_motion_blocker_boolean and states[no_motion_blocker].state == 'on' }}"
- "{{ not no_motion_blocker_boolean and states[no_motion_blocker].state == 'off' }}"
sequence:
- choose:
# Used if ambient scene is true and in ambient scene time frame
- conditions:
- "{{ scene_ambient != 'scene.none' }}"
- condition: time
after: !input time_scene_ambient_start
before: !input time_scene_ambient_end
sequence:
- scene: !input scene_ambient
# Used if default no motion scene is set
- conditions:
- "{{ scene_no_motion != 'scene.none' }}"
sequence:
- scene: !input scene_no_motion
# If State -> "off" default to turn lights off
default:
- service: light.turn_off
target: !input light_target
@sohnvonoff
Copy link

I'm still a beginner in home automation. Your script helped me a lot to understand yaml better. I use automation for several lights. I noticed some misconduct. If the motion detector triggers the automation to switch on before sunrise and then should trigger a little later, but after sunrise, to switch it off, the light will no longer go out.

@iCSpotRun
Copy link

Would be great to add an Illuminance condition check
Would activate on Motion only if Illuminance < X Lux

I know this is in other Blueprints, but YAMA is simply better all around and would be perfect with this last addition.

@tuxmeister
Copy link

Would be great to add an Illuminance condition check Would activate on Motion only if Illuminance < X Lux

I know this is in other Blueprints, but YAMA is simply better all around and would be perfect with this last addition.

Another vote for a Lux-based blocker.

@ivovangent
Copy link

Yes that will be great to have that inside!

@robertbeal
Copy link

robertbeal commented Dec 14, 2022

Think I found a flaw in the design of the automation... Can't yet see an elegant fix. Can see how you've designed it though using two triggers, one immediate and one for a set period.

  1. blocker is false
  2. motion triggers so light turns on for 3 minutes
  3. during this time blocker changes to true (blocking)
  4. the subsequent after-set-period-of-time action then never fires, as the blocker now stops it from doing so, so the light never turns off

@thesainthell
Copy link

I can confirm this flaw posted by @robertbeal.
For now I have to create separate automation which brings the entity in the desired state (if its stuck) as a workaround.

@mspitzenberg
Copy link

@thesainthell can you give an example how you do this?

@robertbeal
Copy link

I think I've fixed the blocker issue I mentioned above. Forgot to post back here, but I made a revision on my fork of the gist. I've been using it for 10 months without issue.
https://gist.github.com/robertbeal/b33176497ba48f945e84f08b8301cace/revisions#

@lukasjakobi
Copy link

I think I found a minor issue with the sun elevation check. If the light is turned on when the elevation check is still true and about to turn off when the elevation check is false, the light is not getting turned off. I forked your work and added a revision to fix the behavior. https://gist.github.com/lukasjakobi/b63d09be776d2df340c16e5006d3451c/revisions#diff-611710a9bc61bfa58d4d8d357379ee308af1a27cd607dd783d347bd0d5d538b5

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