motion_detected_lights.yaml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. blueprint:
  2. domain: automation
  3. name: Motion detected lights
  4. description: >-
  5. Description
  6. input:
  7. trigger_sensors:
  8. name: Trigger sensors
  9. description: >-
  10. Description
  11. selector:
  12. target:
  13. entity:
  14. domain: binary_sensor
  15. synced_lights:
  16. name: Synced lights
  17. description: >-
  18. Description
  19. selector:
  20. target:
  21. entity:
  22. domain: light
  23. dim_percentage:
  24. name: Dim-percentage
  25. description: >-
  26. Dim lights to this level (%) of current light level,
  27. before turning lights off. Current level will be stored
  28. in a scene. Setting this to 0.0 or 1.0 will skip this action.
  29. default: 0.3
  30. selector:
  31. number:
  32. max: 1.0
  33. min: 0.0
  34. step: 0.01
  35. after_wait_actions:
  36. name: Wait actions
  37. description: >-
  38. Actions to run to delay further, e.g wait until media player state is off,
  39. housekeeping mode is off etc.
  40. default: []
  41. selector:
  42. action:
  43. wait_actions_before_dim:
  44. name: Wait actions before dim
  45. description: >-
  46. Run wait actions before dimming down (not turning off). Set to false/OFF
  47. if you want wait actions to run after dimming instead, but before turning
  48. OFF completely.
  49. default: true
  50. selector:
  51. boolean:
  52. delay:
  53. name: Delay
  54. description: >-
  55. Time (minutes) to wait to turn off after last movement was detected.
  56. selector:
  57. number:
  58. min: 0.0
  59. max: 300.0
  60. step: 5.0
  61. default: 3
  62. mode: restart
  63. trigger_variables:
  64. trigger_sensors_input: !input trigger_sensors
  65. trigger_sensors: >-
  66. {% set sensors = namespace(entities=[]) %}
  67. {% if 'entity_id' in trigger_sensors_input %}
  68. {% for entity in ([ trigger_sensors_input['entity_id'] ] if trigger_sensors_input['entity_id'] is string else trigger_sensors_input['entity_id']) %}
  69. {% set sensors.entities = sensors.entities + [entity] %}
  70. {% endfor %}
  71. {% endif %}
  72. {% if 'area_id' in trigger_sensors_input %}
  73. {% for area in ([ trigger_sensors_input['area_id'] ] if trigger_sensors_input['area_id'] is string else trigger_sensors_input['area_id']) %}
  74. {% for entity in area_entities(area) if entity.startswith('binary_sensor.') %}
  75. {% set sensors.entities = sensors.entities + [entity] %}
  76. {% endfor %}
  77. {% endfor %}
  78. {% endif %}
  79. {% if 'device_id' in trigger_sensors_input %}
  80. {% for device in ([ trigger_sensors_input['device_id'] ] if trigger_sensors_input['device_id'] is string else trigger_sensors_input['device_id']) %}
  81. {% for entity in device_entities(device) if entity.startswith('binary_sensor.') %}
  82. {% set sensors.entities = sensors.entities + [entity] %}
  83. {% endfor %}
  84. {% endfor %}
  85. {% endif %}
  86. {{ sensors.entities|unique|list }}
  87. scene_name: >-
  88. {{ this.entity_id.replace('.', '_') }}
  89. trigger:
  90. - platform: template
  91. value_template: >-
  92. {% set sensors = namespace(triggered=false) %}
  93. {% for sensor in trigger_sensors if is_state(sensor, 'on') %}
  94. {% set sensors.triggered = true %}
  95. {% endfor %}
  96. {{ sensors.triggered }}
  97. - platform: event
  98. event_type: "motion_detected_lights"
  99. event_data:
  100. end: "{{ scene_name }}"
  101. variables:
  102. synced_lights_inputs: !input synced_lights
  103. synced_lights: >-
  104. {% set lights = namespace(entities=[]) %}
  105. {% if 'entity_id' in synced_lights_inputs %}
  106. {% for entity in ([ synced_lights_inputs['entity_id'] ] if synced_lights_inputs['entity_id'] is string else synced_lights_inputs['entity_id']) %}
  107. {% set lights.entities = lights.entities + [entity] %}
  108. {% endfor %}
  109. {% endif %}
  110. {% if 'area_id' in synced_lights_inputs %}
  111. {% for area in ([ synced_lights_inputs['area_id'] ] if synced_lights_inputs['area_id'] is string else synced_lights_inputs['area_id']) %}
  112. {% for entity in area_entities(area) if entity.startswith('light.') %}
  113. {% set lights.entities = lights.entities + [entity] %}
  114. {% endfor %}
  115. {% endfor %}
  116. {% endif %}
  117. {% if 'device_id' in synced_lights_inputs %}
  118. {% for device in ([ synced_lights_inputs['device_id'] ] if synced_lights_inputs['device_id'] is string else synced_lights_inputs['device_id']) %}
  119. {% for entity in device_entities(device) if entity.startswith('light.') %}
  120. {% set lights.entities = lights.entities + [entity] %}
  121. {% endfor %}
  122. {% endfor %}
  123. {% endif %}
  124. {{ lights.entities|unique|list|sort }}
  125. invalid_light: >-
  126. {{ states.light|rejectattr('entity_id', 'in', synced_lights)|map(attribute='entity_id')|first }}
  127. log_level: warning
  128. dim_percentage: !input dim_percentage
  129. delay_minutes: !input delay
  130. delay_seconds: "{{ delay_minutes * 60 }}"
  131. wait_actions_before_dim: !input wait_actions_before_dim
  132. action:
  133. - alias: "Triggered on motion or (any) event"
  134. if: "{{ trigger.platform != 'event' }}"
  135. then:
  136. - alias: "Turn on scene if it already exists, otherwise turn on lights."
  137. continue_on_error: true # if scenes are empty (might happen after a scene.reload)
  138. choose:
  139. conditions: "{{ states.scene | selectattr('attributes.friendly_name', 'eq', scene_name) | list | count == 1 and synced_lights == state_attr('scene.' + scene_name, 'entity_id')|sort }}"
  140. sequence: # Scene do exist and is valid
  141. - service: system_log.write
  142. data:
  143. level: "{{ log_level }}"
  144. message: >-
  145. Scene «{{ scene_name }}» already exists and contains all lights. Restore scene.
  146. - alias: "Turn on scene for synced lights"
  147. service: scene.turn_on
  148. target:
  149. entity_id: "scene.{{scene_name}}"
  150. default: # Scene does not exist
  151. - service: system_log.write
  152. continue_on_error: true
  153. data:
  154. level: "{{ log_level }}"
  155. message: >-
  156. Scene «{{ scene_name }} does NOT exist or is INVALID.
  157. Entities to sync: {{ synced_lights }} => Turn ON every light sourcce.
  158. - alias: "Turn on synced lights"
  159. service: light.turn_on
  160. target:
  161. entity_id: "{{ synced_lights }}"
  162. - alias: >-
  163. Listen for light changes and motion in parallel.
  164. Save snapshorts to scene on light changes.
  165. End this part of automation when motion stops.
  166. parallel:
  167. # Listen for light changes in parallel and save snapshot to a scene
  168. # so we dont restore light every time the motion is triggered without being OFF
  169. - alias: >-
  170. Listen for light changes for as long as this automation lives,
  171. means: until event is fired and automation is restarted.
  172. repeat:
  173. while: "{{ true }}"
  174. sequence:
  175. - alias: "Store variable with current light values (on or brightness)"
  176. variables:
  177. synced_lights_values: >-
  178. {% set lights = namespace(values=[]) %}
  179. {% for light in synced_lights %}
  180. {%if is_state_attr(light, 'color_mode', 'brightness') %}
  181. {% set lights.values = lights.values + [state_attr(light, 'brightness')] %}
  182. {% else %}
  183. {% set lights.values = lights.values + [states(light)] %}
  184. {% endif %}
  185. {% endfor %}
  186. {{ lights.values }}
  187. - alias: "Wait for change in any light source"
  188. wait_template: >-
  189. {% set lights = namespace(changed=false) %}
  190. {% for light in synced_lights %}
  191. {%if is_state_attr(light, 'color_mode', 'brightness') and state_attr(light, 'brightness') != synced_lights_values[loop.index - 1] %}
  192. {% set lights.changed = true %}
  193. {% elif not is_state_attr(light, 'color_mode', 'brightness') and states(light) != synced_lights_values[loop.index - 1] %}
  194. {% set lights.changed = true %}
  195. {% endif %}
  196. {% endfor %}
  197. {{ lights.changed }}
  198. # Check here if values = 0 / OFF
  199. - service: system_log.write
  200. data:
  201. level: "{{ log_level }}"
  202. message: >-
  203. Light levels changed: {{ synced_lights }} from {{ synced_lights_values }}.
  204. Saving snapshot to scene «{{ scene_name }}».
  205. - alias: "Store variable a variable with ON lights."
  206. variables:
  207. synced_lights_on: >-
  208. {% set lights = namespace(on=[]) %}
  209. {% for light in synced_lights if not is_state(light, 'off') %}
  210. {% set lights.on = lights.on + [light] %}
  211. {% endfor %}
  212. {{ lights.on }}
  213. - choose:
  214. conditions: "{{ synced_lights_on|count == 0 }}"
  215. sequence:
  216. service: system_log.write
  217. data:
  218. level: "{{ log_level }}"
  219. message: "No lights on: invalidating with {{ invalid_light }} which isn't in {{ synced_lights }}."
  220. - alias: "Save scene for current change and return to loop. If no lights are ON; invalidate scene."
  221. service: scene.create
  222. data:
  223. scene_id: "{{ scene_name }}"
  224. snapshot_entities: >-
  225. {{ synced_lights if synced_lights_on|count != 0 else [ invalid_light ] }}
  226. - alias: >-
  227. Listen for motion change, and wait for it to become 'OFF',
  228. wait for the delay and actions to finish, and then send event to kill parallelism.
  229. sequence: # @ignore: Missing property "condition"
  230. - alias: "Wait for motion to end."
  231. wait_template: >-
  232. {% set sensors = namespace(triggered=false) %}
  233. {% for sensor in trigger_sensors if is_state(sensor, 'on') %}
  234. {% set sensors.triggered = true %}
  235. {% endfor %}
  236. {{ not sensors.triggered }}
  237. - service: system_log.write
  238. data:
  239. level: "{{ log_level }}"
  240. message: >-
  241. No motion detected by {{ trigger_sensors }}. Run wait actions
  242. wait {{ (delay_seconds * 1/3)|int }} seconds,, then end
  243. automation (parallelism) by issuing an event, and let automation «re-trigger».
  244. - alias: "Run and wait for actions to finish"
  245. choose:
  246. conditions: >-
  247. {{ wait_actions_before_dim }}
  248. sequence: !input after_wait_actions
  249. - alias: "Wait for delay to fire event."
  250. delay:
  251. seconds: "{{ (delay_seconds * 2/3)|int }}"
  252. - alias: "Ending: Fire event to restart automation and kill parallel processes"
  253. event: "motion_detected_lights"
  254. event_data:
  255. end: "{{ scene_name }}"
  256. else: # Event was fired
  257. - service: system_log.write
  258. data:
  259. level: "{{ log_level }}"
  260. message: "Ending {{ scene_name }}, dim down and eventually turn off {{ synced_lights }}."
  261. - alias: "Calculate average brightness and set dim level"
  262. variables:
  263. brightness: >-
  264. {% set brightness = namespace(levels=[]) %}
  265. {% for light in synced_lights if is_state(light, 'on') %}
  266. {% if is_state_attr(light, 'color_mode', 'brightness') and state_attr(light, 'brightness')|int != 0 %}
  267. {% set brightness.levels = brightness.levels + [ state_attr(light, 'brightness') ] %}
  268. {% else %}
  269. {% set brightness.levels = brightness.levels + [ 255 ] %}
  270. {% endif %}
  271. {% endfor %}
  272. {{ 0.0 if brightness.levels|length == 0 else brightness.levels|sum / brightness.levels|length }}
  273. dim_level: "{{ (brightness * dim_percentage)|int }}"
  274. - service: system_log.write
  275. data:
  276. message: >-
  277. Light avg-brightness is: {{ brightness }}, now dimming lights
  278. with {{ (dim_percentage * 100)|int }}% to {{ dim_level }}".
  279. level: "{{ log_level }}"
  280. - alias: "Dim lights to a low brightness to alert no motion"
  281. service: light.turn_on
  282. target:
  283. entity_id: >-
  284. {{ synced_lights }}
  285. data:
  286. brightness: "{{ dim_level }}"
  287. - alias: "Run and wait for actions to finish"
  288. choose:
  289. conditions: >-
  290. {{ not wait_actions_before_dim }}
  291. sequence: !input after_wait_actions
  292. - alias: "Wait for dim to be recognized by anyone before turning off lights"
  293. delay:
  294. seconds: "{{ (delay_seconds * 1/3)|int }}"
  295. - alias: "Turn off lights"
  296. service: light.turn_off
  297. target:
  298. entity_id: "{{ synced_lights }}"
  299. # - alias: "Invalidate scene."
  300. # service: scene.create
  301. # data:
  302. # scene_id: "{{ scene_name }}"
  303. # snapshot_entities: "{{ [ invalid_light ] }}"