blueprint: name: Motion active lights description: >- Motion active lights that respects the illumination in the room. Turrent light scene will be saved as a new scene so the lights can dim to a certain point before turning off. This way it will «alert» someone in the room that the light is about to go off. Additional criterias can be set in the `after_wait_actions` so you can listen for additional sensors to become off, before turning off the lights. E.g wait for the humitidy in the bathroom to come down before turning off the lights. domain: automation input: trigger_sensors: name: Sensors description: >- Sensors that will trigger the light to turn on. It's recommended to select entities explicitly instead of areas and/or devices. selector: target: entity: domain: binary_sensor synced_lights: name: Lights description: >- Lights that will adapt to the motion in the room. It's recommended to select entities explicitly instead of areas and/or devices. selector: target: entity: domain: light dim_percentage: name: Dim-percentage description: >- Dim lights to this level (%) of current light level, before turning lights off. Current level will be stored in a scene. Setting this to 0.0 or 1.0 will skip this action. default: 0.3 selector: number: max: 1.0 min: 0.0 step: 0.01 after_wait_actions: name: Wait actions description: >- Actions to run to delay further, e.g wait until media player state is off, housekeeping mode is off etc. default: [] selector: action: delay: name: Delay description: >- Time (minutes) to wait to turn off after last movement was detected. selector: number: min: 0.0 max: 300.0 step: 5.0 default: 3 mode: restart trigger_variables: trigger_sensors_input: !input trigger_sensors trigger_sensors: >- {% set sensors = namespace(entities=[]) %} {% if 'entity_id' in trigger_sensors_input %} {% for entity in ([ trigger_sensors_input['entity_id'] ] if trigger_sensors_input['entity_id'] is string else trigger_sensors_input['entity_id']) %} {% set sensors.entities = sensors.entities + [entity] %} {% endfor %} {% endif %} {% if 'area_id' in trigger_sensors_input %} {% for area in ([ trigger_sensors_input['area_id'] ] if trigger_sensors_input['area_id'] is string else trigger_sensors_input['area_id']) %} {% for entity in area_entities(area) if entity.startswith('binary_sensor.') %} {% set sensors.entities = sensors.entities + [entity] %} {% endfor %} {% endfor %} {% endif %} {% if 'device_id' in trigger_sensors_input %} {% for device in ([ trigger_sensors_input['device_id'] ] if trigger_sensors_input['device_id'] is string else trigger_sensors_input['device_id']) %} {% for entity in device_entities(device) if entity.startswith('binary_sensor.') %} {% set sensors.entities = sensors.entities + [entity] %} {% endfor %} {% endfor %} {% endif %} {{ sensors.entities|unique|list }} trigger: platform: template value_template: >- {% set sensors = namespace(triggered=false) %} {% for sensor in trigger_sensors if is_state(sensor, 'on') %} {% set sensors.triggered = true %} {% endfor %} {{ sensors.triggered }} variables: synced_lights_inputs: !input synced_lights synced_lights: >- {% set lights = namespace(entities=[]) %} {% if 'entity_id' in synced_lights_inputs %} {% for entity in ([ synced_lights_inputs['entity_id'] ] if synced_lights_inputs['entity_id'] is string else synced_lights_inputs['entity_id']) %} {% set lights.entities = lights.entities + [entity] %} {% endfor %} {% endif %} {% if 'area_id' in synced_lights_inputs %} {% for area in ([ synced_lights_inputs['area_id'] ] if synced_lights_inputs['area_id'] is string else synced_lights_inputs['area_id']) %} {% for entity in area_entities(area) if entity.startswith('light.') %} {% set lights.entities = lights.entities + [entity] %} {% endfor %} {% endfor %} {% endif %} {% if 'device_id' in synced_lights_inputs %} {% for device in ([ synced_lights_inputs['device_id'] ] if synced_lights_inputs['device_id'] is string else synced_lights_inputs['device_id']) %} {% for entity in device_entities(device) if entity.startswith('light.') %} {% set lights.entities = lights.entities + [entity] %} {% endfor %} {% endfor %} {% endif %} {{ lights.entities|unique|list|sort }} scene_name: >- {{ this.entity_id.replace('.', '_') }} dim_percentage: !input dim_percentage delay_minutes: !input delay delay_seconds: "{{ delay_minutes * 60 }}" log_level: warning action: [] # - service: system_log.write # data: # message: >- # Synced lights: {{ synced_lights }} # Scene name: {{ scene_name }} # Dim percentage: {{ (dim_percentage * 100)|int }}% # Delay: {{ delay_minutes }} ({{ delay_seconds }} sec) # level: "{{ log_level }}" # - choose: # alias: "Turn on state if it already exists, to restore state" # conditions: "{{ states.scene | selectattr('attributes.friendly_name', 'eq', scene_name) | list | count == 1 and synced_lights == state_attr('scene.' + scene_name, 'entity_id')|sort }}" # sequence: # - service: system_log.write # data: # message: >- # Turning on scene: {{ scene_name }} # level: "{{ log_level }}" # - service: scene.turn_on # target: # entity_id: "scene.{{ scene_name }}" # default: # - service: system_log.write # data: # message: >- # Scene does not exists, or invalid. Turning on lights: {{ synced_lights|join(', ') }} # level: "{{ log_level }}" # - service: light.turn_on # target: # entity_id: >- # {{ synced_lights }} # - variables: # lux: 0 # - wait_template: >- # {% set sensors = namespace(triggered=false) %} # {% for sensor in trigger_sensors if is_state(sensor, 'on') %} # {% set sensors.triggered = true %} # {% endfor %} # {{ not sensors.triggered }} # - delay: # seconds: "{{ (delay_seconds * 1/3)|int }}" # - choose: [] # default: !input after_wait_actions # - variables: # brightness: >- # {% set brightness = namespace(levels=[]) %} # {% for light in synced_lights if is_state(light, 'on') and state_attr(light, 'brightness')|int != 0 %} # {% set brightness.levels = brightness.levels + [ state_attr(light, 'brightness') ] %} # {% endfor %} # {{ 0 if brightness.levels|length == 0 else brightness.levels|sum / brightness.levels|length }} # - service: system_log.write # data: # message: >- # Brightness: {{ brightness }} # Upcoming test: {{ brightness|int > 0 and dim_percentage not in [0.0, 1.0] }} # level: "{{ log_level }}" # - choose: # conditions: "{{ brightness|int > 0 and dim_percentage not in [0.0, 1.0] }}" # sequence: # - service: system_log.write # data: # message: >- # Saving scene «{{ scene_name }}» with: {{ synced_lights|join(',') }} # level: "{{ log_level }}" # - service: scene.create # data: # scene_id: "{{ scene_name }}" # snapshot_entities: >- # {{ synced_lights }} # - delay: # seconds: "{{ (delay_seconds * 1/3)|int }}" # - service: system_log.write # data: # message: "Dimming lights to {{ (brightness * dim_percentage)|int }}" # level: "{{ log_level }}" # - alias: "Dim lights to avg-brightness»" # service: light.turn_on # target: # entity_id: >- # {{ synced_lights }} # data: # brightness: "{{ (brightness * dim_percentage)|int }}" # default: # - delay: # seconds: "{{ (delay_seconds * 1/3)|int }}" # - delay: # seconds: "{{ (delay_seconds * 1/3)|int }}" # - service: light.turn_off # target: # entity_id: >- # {{ synced_lights }}