motion_detection_light.yaml 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. blueprint:
  2. name: Motion active lights
  3. description: >-
  4. Motion active lights that respects the illumination in the room.
  5. Turrent light scene will be saved as a new scene so the lights can dim
  6. to a certain point before turning off. This way it will «alert» someone
  7. in the room that the light is about to go off.
  8. Additional criterias can be set in the `after_wait_actions` so you can
  9. listen for additional sensors to become off, before turning off the lights.
  10. E.g wait for the humitidy in the bathroom to come down before turning off
  11. the lights.
  12. domain: automation
  13. input:
  14. trigger_sensors:
  15. name: Sensors
  16. description: >-
  17. Sensors that will trigger the light to turn on. It's
  18. recommended to select entities explicitly instead of areas
  19. and/or devices.
  20. selector:
  21. target:
  22. entity:
  23. domain: binary_sensor
  24. synced_lights:
  25. name: Lights
  26. description: >-
  27. Lights that will adapt to the motion in the room. It's
  28. recommended to select entities explicitly instead of areas
  29. and/or devices.
  30. selector:
  31. target:
  32. entity:
  33. domain: light
  34. dim_percentage:
  35. name: Dim-percentage
  36. description: >-
  37. Dim lights to this level (%) of current light level,
  38. before turning lights off. Current level will be stored
  39. in a scene. Setting this to 0.0 or 1.0 will skip this action.
  40. default: 0.3
  41. selector:
  42. number:
  43. max: 1.0
  44. min: 0.0
  45. step: 0.01
  46. after_wait_actions:
  47. name: Wait actions
  48. description: >-
  49. Actions to run to delay further, e.g wait until media player state is off,
  50. housekeeping mode is off etc.
  51. default: []
  52. selector:
  53. action:
  54. delay:
  55. name: Delay
  56. description: >-
  57. Time (minutes) to wait to turn off after last movement was detected.
  58. selector:
  59. number:
  60. min: 0.0
  61. max: 300.0
  62. step: 5.0
  63. default: 3
  64. mode: restart
  65. trigger_variables:
  66. trigger_sensors_input: !input trigger_sensors
  67. trigger_sensors: >-
  68. {% set sensors = namespace(entities=[]) %}
  69. {% if 'entity_id' in trigger_sensors_input %}
  70. {% for entity in ([ trigger_sensors_input['entity_id'] ] if trigger_sensors_input['entity_id'] is string else trigger_sensors_input['entity_id']) %}
  71. {% set sensors.entities = sensors.entities + [entity] %}
  72. {% endfor %}
  73. {% endif %}
  74. {% if 'area_id' in trigger_sensors_input %}
  75. {% for area in ([ trigger_sensors_input['area_id'] ] if trigger_sensors_input['area_id'] is string else trigger_sensors_input['area_id']) %}
  76. {% for entity in area_entities(area) if entity.startswith('binary_sensor.') %}
  77. {% set sensors.entities = sensors.entities + [entity] %}
  78. {% endfor %}
  79. {% endfor %}
  80. {% endif %}
  81. {% if 'device_id' in trigger_sensors_input %}
  82. {% for device in ([ trigger_sensors_input['device_id'] ] if trigger_sensors_input['device_id'] is string else trigger_sensors_input['device_id']) %}
  83. {% for entity in device_entities(device) if entity.startswith('binary_sensor.') %}
  84. {% set sensors.entities = sensors.entities + [entity] %}
  85. {% endfor %}
  86. {% endfor %}
  87. {% endif %}
  88. {{ sensors.entities|unique|list }}
  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. variables:
  98. synced_lights_inputs: !input synced_lights
  99. synced_lights: >-
  100. {% set lights = namespace(entities=[]) %}
  101. {% if 'entity_id' in synced_lights_inputs %}
  102. {% for entity in ([ synced_lights_inputs['entity_id'] ] if synced_lights_inputs['entity_id'] is string else synced_lights_inputs['entity_id']) %}
  103. {% set lights.entities = lights.entities + [entity] %}
  104. {% endfor %}
  105. {% endif %}
  106. {% if 'area_id' in synced_lights_inputs %}
  107. {% for area in ([ synced_lights_inputs['area_id'] ] if synced_lights_inputs['area_id'] is string else synced_lights_inputs['area_id']) %}
  108. {% for entity in area_entities(area) if entity.startswith('light.') %}
  109. {% set lights.entities = lights.entities + [entity] %}
  110. {% endfor %}
  111. {% endfor %}
  112. {% endif %}
  113. {% if 'device_id' in synced_lights_inputs %}
  114. {% for device in ([ synced_lights_inputs['device_id'] ] if synced_lights_inputs['device_id'] is string else synced_lights_inputs['device_id']) %}
  115. {% for entity in device_entities(device) if entity.startswith('light.') %}
  116. {% set lights.entities = lights.entities + [entity] %}
  117. {% endfor %}
  118. {% endfor %}
  119. {% endif %}
  120. {{ lights.entities|unique|list|sort }}
  121. scene_name: >-
  122. {{ this.entity_id.replace('.', '_') }}
  123. dim_percentage: !input dim_percentage
  124. delay_minutes: !input delay
  125. delay_seconds: "{{ delay_minutes * 60 }}"
  126. log_level: warning
  127. action: []
  128. # - service: system_log.write
  129. # data:
  130. # message: >-
  131. # Synced lights: {{ synced_lights }}
  132. # Scene name: {{ scene_name }}
  133. # Dim percentage: {{ (dim_percentage * 100)|int }}%
  134. # Delay: {{ delay_minutes }} ({{ delay_seconds }} sec)
  135. # level: "{{ log_level }}"
  136. # - choose:
  137. # alias: "Turn on state if it already exists, to restore state"
  138. # conditions: "{{ states.scene | selectattr('attributes.friendly_name', 'eq', scene_name) | list | count == 1 and synced_lights == state_attr('scene.' + scene_name, 'entity_id')|sort }}"
  139. # sequence:
  140. # - service: system_log.write
  141. # data:
  142. # message: >-
  143. # Turning on scene: {{ scene_name }}
  144. # level: "{{ log_level }}"
  145. # - service: scene.turn_on
  146. # target:
  147. # entity_id: "scene.{{ scene_name }}"
  148. # default:
  149. # - service: system_log.write
  150. # data:
  151. # message: >-
  152. # Scene does not exists, or invalid. Turning on lights: {{ synced_lights|join(', ') }}
  153. # level: "{{ log_level }}"
  154. # - service: light.turn_on
  155. # target:
  156. # entity_id: >-
  157. # {{ synced_lights }}
  158. # - variables:
  159. # lux: 0
  160. # - wait_template: >-
  161. # {% set sensors = namespace(triggered=false) %}
  162. # {% for sensor in trigger_sensors if is_state(sensor, 'on') %}
  163. # {% set sensors.triggered = true %}
  164. # {% endfor %}
  165. # {{ not sensors.triggered }}
  166. # - delay:
  167. # seconds: "{{ (delay_seconds * 1/3)|int }}"
  168. # - choose: []
  169. # default: !input after_wait_actions
  170. # - variables:
  171. # brightness: >-
  172. # {% set brightness = namespace(levels=[]) %}
  173. # {% for light in synced_lights if is_state(light, 'on') and state_attr(light, 'brightness')|int != 0 %}
  174. # {% set brightness.levels = brightness.levels + [ state_attr(light, 'brightness') ] %}
  175. # {% endfor %}
  176. # {{ 0 if brightness.levels|length == 0 else brightness.levels|sum / brightness.levels|length }}
  177. # - service: system_log.write
  178. # data:
  179. # message: >-
  180. # Brightness: {{ brightness }}
  181. # Upcoming test: {{ brightness|int > 0 and dim_percentage not in [0.0, 1.0] }}
  182. # level: "{{ log_level }}"
  183. # - choose:
  184. # conditions: "{{ brightness|int > 0 and dim_percentage not in [0.0, 1.0] }}"
  185. # sequence:
  186. # - service: system_log.write
  187. # data:
  188. # message: >-
  189. # Saving scene «{{ scene_name }}» with: {{ synced_lights|join(',') }}
  190. # level: "{{ log_level }}"
  191. # - service: scene.create
  192. # data:
  193. # scene_id: "{{ scene_name }}"
  194. # snapshot_entities: >-
  195. # {{ synced_lights }}
  196. # - delay:
  197. # seconds: "{{ (delay_seconds * 1/3)|int }}"
  198. # - service: system_log.write
  199. # data:
  200. # message: "Dimming lights to {{ (brightness * dim_percentage)|int }}"
  201. # level: "{{ log_level }}"
  202. # - alias: "Dim lights to avg-brightness»"
  203. # service: light.turn_on
  204. # target:
  205. # entity_id: >-
  206. # {{ synced_lights }}
  207. # data:
  208. # brightness: "{{ (brightness * dim_percentage)|int }}"
  209. # default:
  210. # - delay:
  211. # seconds: "{{ (delay_seconds * 1/3)|int }}"
  212. # - delay:
  213. # seconds: "{{ (delay_seconds * 1/3)|int }}"
  214. # - service: light.turn_off
  215. # target:
  216. # entity_id: >-
  217. # {{ synced_lights }}