Browse Source

Init commit

Joachim M. Giæver 1 year ago
parent
commit
9b15477903

+ 54 - 0
automation/homeassistant/motion_light.yaml

@@ -0,0 +1,54 @@
+blueprint:
+  name: Motion-activated Light
+  description: Turn on a light when motion is detected.
+  domain: automation
+  source_url: https://github.com/home-assistant/core/blob/dev/homeassistant/components/automation/blueprints/motion_light.yaml
+  input:
+    motion_entity:
+      name: Motion Sensor
+      selector:
+        entity:
+          domain: binary_sensor
+          device_class: motion
+    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
+
+# If motion is detected within the delay,
+# we restart the script.
+mode: restart
+max_exceeded: silent
+
+trigger:
+  platform: state
+  entity_id: !input motion_entity
+  from: "off"
+  to: "on"
+
+action:
+  - alias: "Turn on the light"
+    service: light.turn_on
+    target: !input light_target
+  - alias: "Wait until there is no motion from device"
+    wait_for_trigger:
+      platform: state
+      entity_id: !input motion_entity
+      from: "on"
+      to: "off"
+  - alias: "Wait the number of seconds that has been set"
+    delay: !input no_motion_wait
+  - alias: "Turn off the light"
+    service: light.turn_off
+    target: !input light_target

+ 44 - 0
automation/homeassistant/notify_leaving_zone.yaml

@@ -0,0 +1,44 @@
+blueprint:
+  name: Zone Notification
+  description: Send a notification to a device when a person leaves a specific zone.
+  domain: automation
+  source_url: https://github.com/home-assistant/core/blob/dev/homeassistant/components/automation/blueprints/notify_leaving_zone.yaml
+  input:
+    person_entity:
+      name: Person
+      selector:
+        entity:
+          domain: person
+    zone_entity:
+      name: Zone
+      selector:
+        entity:
+          domain: zone
+    notify_device:
+      name: Device to notify
+      description: Device needs to run the official Home Assistant app to receive notifications.
+      selector:
+        device:
+          integration: mobile_app
+
+trigger:
+  platform: state
+  entity_id: !input person_entity
+
+variables:
+  zone_entity: !input zone_entity
+  # This is the state of the person when it's in this zone.
+  zone_state: "{{ states[zone_entity].name }}"
+  person_entity: !input person_entity
+  person_name: "{{ states[person_entity].name }}"
+
+condition:
+  condition: template
+  value_template: "{{ trigger.from_state.state == zone_state and trigger.to_state.state != zone_state }}"
+
+action:
+  - alias: "Notify that a person has left the zone"
+    domain: mobile_app
+    type: notify
+    device_id: !input notify_device
+    message: "{{ person_name }} has left {{ zone_state }}"

+ 336 - 0
automation/motion_detected_lights.yaml

@@ -0,0 +1,336 @@
+blueprint:
+  domain: automation
+  name: Motion detected lights
+  description: >-
+    Description
+  input:
+    trigger_sensors:
+      name: Trigger sensors
+      description: >-
+        Description
+      selector:
+        target:
+          entity:
+            domain: binary_sensor
+    synced_lights:
+      name: Synced lights
+      description: >-
+        Description
+      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:
+    wait_actions_before_dim:
+      name: Wait actions before dim
+      description: >-
+        Run wait actions before dimming down (not turning off). Set to false/OFF
+        if you want wait actions to run after dimming instead, but before turning 
+        OFF completely.
+      default: true
+      selector:
+        boolean: 
+    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 }}
+  scene_name: >-
+    {{ this.entity_id.replace('.', '_') }}
+
+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 }}
+  - platform: event
+    event_type: "motion_detected_lights"
+    event_data:
+      end: "{{ scene_name }}"
+
+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 }}
+  invalid_light: >-
+    {{ states.light|rejectattr('entity_id', 'in', synced_lights)|map(attribute='entity_id')|first }}
+  log_level: warning
+  dim_percentage: !input dim_percentage
+  delay_minutes: !input delay
+  delay_seconds: "{{ delay_minutes * 60 }}"
+  wait_actions_before_dim: !input wait_actions_before_dim
+
+action:
+  - alias: "Triggered on motion or (any) event"
+    if: "{{ trigger.platform != 'event' }}"
+    then:
+
+      - alias: "Turn on scene if it already exists, otherwise turn on lights."
+        continue_on_error: true # if scenes are empty (might happen after a scene.reload)
+        choose:
+          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: # Scene do exist and is valid
+
+            - service: system_log.write
+              data:
+                level: "{{ log_level }}"
+                message: >-
+                  Scene «{{ scene_name }}» already exists and contains all lights. Restore scene.
+            
+            - alias: "Turn on scene for synced lights"
+              service: scene.turn_on
+              target:
+                entity_id: "scene.{{scene_name}}"
+
+        default: # Scene does not exist
+          - service: system_log.write
+            continue_on_error: true
+            data:
+              level: "{{ log_level }}"
+              message: >-
+                Scene «{{ scene_name }} does NOT exist or is INVALID. 
+                Entities to sync: {{ synced_lights }} => Turn ON every light sourcce.
+        
+          - alias: "Turn on synced lights"
+            service: light.turn_on
+            target:
+              entity_id: "{{ synced_lights }}"
+
+      - alias: >-
+          Listen for light changes and motion in parallel. 
+          Save snapshorts to scene on light changes. 
+          End this part of automation when motion stops.
+        parallel: 
+        # Listen for light changes in parallel and save snapshot to a scene
+        # so we dont restore light every time the motion is triggered without being OFF
+        
+        - alias: >-
+            Listen for light changes for as long as this automation lives, 
+            means: until event is fired and automation is restarted.
+          repeat:
+            while: "{{ true }}" 
+            sequence:
+
+              - alias: "Store variable with current light values (on or brightness)"
+                variables:
+                  synced_lights_values: >-
+                    {% set lights = namespace(values=[]) %}
+                    {% for light in synced_lights %}
+                      {%if is_state_attr(light, 'color_mode', 'brightness') %}
+                        {% set lights.values = lights.values + [state_attr(light, 'brightness')] %}
+                      {% else %}
+                        {% set lights.values = lights.values + [states(light)] %}
+                      {% endif %}
+                    {% endfor %}
+                    {{ lights.values }}
+
+              - alias: "Wait for change in any light source"
+                wait_template: >-
+                  {% set lights = namespace(changed=false) %}
+                  {% for light in synced_lights %}
+                    {%if is_state_attr(light, 'color_mode', 'brightness') and state_attr(light, 'brightness') != synced_lights_values[loop.index - 1] %}
+                      {% set lights.changed = true %}
+                    {% elif not is_state_attr(light, 'color_mode', 'brightness') and states(light) != synced_lights_values[loop.index - 1] %}
+                      {% set lights.changed = true %}
+                    {% endif %}
+                  {% endfor %}
+                  {{ lights.changed }}
+
+              # Check here if values = 0 / OFF
+              - service: system_log.write
+                data:
+                  level: "{{ log_level }}"
+                  message: >-
+                    Light levels changed: {{ synced_lights }} from {{ synced_lights_values }}.
+                    Saving snapshot to scene «{{ scene_name }}».
+
+              - alias: "Store variable a variable with ON lights."
+                variables:
+                  synced_lights_on: >-
+                    {% set lights = namespace(on=[]) %}
+                    {% for light in synced_lights if not is_state(light, 'off') %}
+                      {% set lights.on = lights.on + [light] %}
+                    {% endfor %}
+                    {{ lights.on }}
+
+              - choose:
+                  conditions: "{{ synced_lights_on|count == 0 }}"
+                  sequence:
+                    service: system_log.write
+                    data:
+                      level: "{{ log_level }}"
+                      message: "No lights on: invalidating with {{ invalid_light }} which isn't in {{ synced_lights }}."
+                    
+              - alias: "Save scene for current change and return to loop. If no lights are ON; invalidate scene."
+                service: scene.create
+                data:
+                  scene_id: "{{ scene_name }}"
+                  snapshot_entities: >-
+                    {{ synced_lights if synced_lights_on|count != 0 else [ invalid_light ] }}
+
+        - alias: >-
+            Listen for motion change, and wait for it to become 'OFF',
+            wait for the delay and actions to finish, and then send event to kill parallelism.
+          sequence: # @ignore: Missing property "condition"
+          
+          - alias: "Wait for motion to end."
+            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 }}
+          
+          - service: system_log.write
+            data:
+              level: "{{ log_level }}"
+              message: >-
+                No motion detected by {{ trigger_sensors }}. Run wait actions
+                wait {{ (delay_seconds * 1/3)|int }} seconds,, then end 
+                automation (parallelism) by issuing an event, and let automation «re-trigger».
+
+          - alias: "Run and wait for actions to finish"
+            choose: 
+              conditions: >-
+                {{ wait_actions_before_dim }}
+              sequence: !input after_wait_actions
+
+          - alias: "Wait for delay to fire event."
+            delay:
+              seconds: "{{ (delay_seconds * 2/3)|int }}"
+
+          - alias: "Ending: Fire event to restart automation and kill parallel processes"
+            event: "motion_detected_lights"
+            event_data:
+              end: "{{ scene_name }}"
+    
+    else: # Event was fired
+
+      - service: system_log.write
+        data: 
+          level: "{{ log_level }}"
+          message: "Ending {{ scene_name }}, dim down and eventually turn off {{ synced_lights }}."
+      
+      - alias: "Calculate average brightness and set dim level"
+        variables:
+          brightness: >-
+            {% set brightness = namespace(levels=[]) %}
+            {% for light in synced_lights if is_state(light, 'on') %}
+              {% if is_state_attr(light, 'color_mode', 'brightness') and state_attr(light, 'brightness')|int != 0 %}
+                {% set brightness.levels = brightness.levels + [ state_attr(light, 'brightness') ] %}
+              {% else %}
+                {% set brightness.levels = brightness.levels + [ 255 ] %}
+              {% endif %}
+            {% endfor %}
+            {{ 0.0 if brightness.levels|length == 0 else brightness.levels|sum / brightness.levels|length }}
+          dim_level: "{{ (brightness * dim_percentage)|int }}"
+
+      - service: system_log.write
+        data:
+          message: >-
+            Light avg-brightness is: {{ brightness }}, now dimming lights 
+            with {{ (dim_percentage * 100)|int }}% to {{ dim_level }}".
+          level: "{{ log_level }}"
+      
+      - alias: "Dim lights to a low brightness to alert no motion"
+        service: light.turn_on
+        target:
+          entity_id: >-
+            {{ synced_lights }}
+        data:
+          brightness: "{{ dim_level }}"
+
+      - alias: "Run and wait for actions to finish"
+        choose: 
+          conditions: >-
+            {{ not wait_actions_before_dim }}
+          sequence: !input after_wait_actions
+      
+      - alias: "Wait for dim to be recognized by anyone before turning off lights"
+        delay: 
+          seconds: "{{ (delay_seconds * 1/3)|int }}"
+
+      - alias: "Turn off lights"
+        service: light.turn_off
+        target:
+          entity_id: "{{ synced_lights }}"
+
+      # - alias: "Invalidate scene."
+      #   service: scene.create
+      #   data:
+      #     scene_id: "{{ scene_name }}"
+      #     snapshot_entities: "{{ [ invalid_light ] }}"

+ 245 - 0
automation/motion_detection_light.yaml

@@ -0,0 +1,245 @@
+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 }}

+ 56 - 0
script/create_device_class_groups.yaml

@@ -0,0 +1,56 @@
+blueprint:
+  name: Create device class groups by room and class itself
+  description: >-
+    A script that sends an actionable notification with a confirmation before
+    running the specified action.
+  domain: script
+  input: 
+    areas:
+      selector:
+        object:
+      default: []
+    device_classes:
+      selector:
+        object:
+      default: []
+
+mode: restart
+
+sequence:
+  - variables:
+      areas: !input areas
+      device_classes: !input device_classes
+      num_areas: >-
+        {{ areas|length if areas is not string and areas is iterable else 0 }}
+      num_device_classes: >-
+        {{ device_classes|length if areas is not string and device_classes is iterable else 0 }}
+  - repeat:
+      while: "{{ repeat.index <= num_device_classes }}"
+      sequence:
+        - variables:
+            device_class: "{{ device_classes[ repeat.index - 1 ] }}"
+        - choose:
+            conditions: "{{ states|selectattr('attributes.device_class', 'eq', device_class)|list|length > 0 }}"
+            sequence: 
+              - service: group.set
+                data:
+                  object_id: "{{ device_class }}"
+                  add_entities: "{{ states|selectattr('attributes.device_class', 'eq', device_class)|map(attribute='entity_id')|list }}"
+              - repeat:
+                  while: "{{ repeat.index <= num_areas }}"
+                  sequence:
+                    - variables:
+                        area: "{{ areas[ repeat.index - 1 ] }}"
+                        area_entities: >-
+                          {% set entities = namespace(list=[]) %}
+                          {% for entity in expand('group.' + device_class) if area_name(entity.entity_id) == area %}
+                            {% set entities.list = entities.list + [ entity.entity_id ] %}
+                          {% endfor %}
+                          {{ entities.list }}
+                    - choose:
+                        conditions: "{{ area_entities|length > 0 }}"
+                        sequence:
+                          - service: group.set
+                            data:
+                              object_id: "{{ area.replace(' ', '_')|lower + '_' + device_class }}"
+                              add_entities: "{{ area_entities }}"

+ 10 - 0
script/helpers/loop_and_do.yaml

@@ -0,0 +1,10 @@
+# blueprint:
+#   name: "Loop and do sequence"
+#   input:
+#     from:
+#       selector:
+#         number:
+#     sequence:
+#       selector:
+#         object:
+#   sequence:

+ 84 - 0
script/homeassistant/confirmable_notification.yaml

@@ -0,0 +1,84 @@
+blueprint:
+  name: Confirmable Notification
+  description: >-
+    A script that sends an actionable notification with a confirmation before
+    running the specified action.
+  domain: script
+  source_url: https://github.com/home-assistant/core/blob/master/homeassistant/components/script/blueprints/confirmable_notification.yaml
+  input:
+    notify_device:
+      name: Device to notify
+      description: Device needs to run the official Home Assistant app to receive notifications.
+      selector:
+        device:
+          integration: mobile_app
+    title:
+      name: "Title"
+      description: "The title of the button shown in the notification."
+      default: ""
+      selector:
+        text:
+    message:
+      name: "Message"
+      description: "The message body"
+      selector:
+        text:
+    confirm_text:
+      name: "Confirmation Text"
+      description: "Text to show on the confirmation button"
+      default: "Confirm"
+      selector:
+        text:
+    confirm_action:
+      name: "Confirmation Action"
+      description: "Action to run when notification is confirmed"
+      default: []
+      selector:
+        action:
+    dismiss_text:
+      name: "Dismiss Text"
+      description: "Text to show on the dismiss button"
+      default: "Dismiss"
+      selector:
+        text:
+    dismiss_action:
+      name: "Dismiss Action"
+      description: "Action to run when notification is dismissed"
+      default: []
+      selector:
+        action:
+
+mode: restart
+
+sequence:
+  - alias: "Set up variables"
+    variables:
+      action_confirm: "{{ 'CONFIRM_' ~ context.id }}"
+      action_dismiss: "{{ 'DISMISS_' ~ context.id }}"
+  - alias: "Send notification"
+    domain: mobile_app
+    type: notify
+    device_id: !input notify_device
+    title: !input title
+    message: !input message
+    data:
+      actions:
+        - action: "{{ action_confirm }}"
+          title: !input confirm_text
+        - action: "{{ action_dismiss }}"
+          title: !input dismiss_text
+  - alias: "Awaiting response"
+    wait_for_trigger:
+      - platform: event
+        event_type: mobile_app_notification_action
+        event_data:
+          action: "{{ action_confirm }}"
+      - platform: event
+        event_type: mobile_app_notification_action
+        event_data:
+          action: "{{ action_dismiss }}"
+  - choose:
+      - conditions: "{{ wait.trigger.event.data.action == action_confirm }}"
+        sequence: !input confirm_action
+      - conditions: "{{ wait.trigger.event.data.action == action_dismiss }}"
+        sequence: !input dismiss_action

+ 24 - 0
script/notify_user.yaml

@@ -0,0 +1,24 @@
+blueprint:
+  domain: script
+  name: Notify user
+  description: >-
+    Notify a user
+  input:
+    notify_device:
+      name: "Device to notify"
+      selector:
+        entity:
+          integration: notify
+      default: []
+
+mode: parallel
+
+variables:
+  device: !input notify_device
+  data: "{{ {} }}"
+
+sequence:
+  - alias: "Send notification"
+    service: "{{ device }}"
+    data: >-
+     "{{ data if defined else {} }}"