Joachim M. Giæver 1 year ago
parent
commit
cff34715ea

+ 14 - 18
automation/cast_lovelace_on_motion.yaml

@@ -53,29 +53,25 @@ action:
               level: warning
               message: >-
                 Display {{ state_attr(repeat.item, "friendly_name") }} is ready for Lovelace «{{ dashboard_path }}/{{ dashboard_view }}».
-          - variables:
-              is_muted: "{{ is_state_attr(repeat.item, 'is_volume_muted', true) }}"
-          - if: "{{ not is_muted }}"
-            then:
-            - service: media_player.volume_mute
-              data:
-                is_volume_muted: true
-              target:
-                entity_id: "{{ repeat.item }}"
+          - service: media_player.volume_mute
+            data:
+              is_volume_muted: true
+            target:
+              entity_id: "{{ repeat.item }}"
           - service: cast.show_lovelace_view
             data:
               entity_id: "{{ repeat.item }}"
               dashboard_path: "{{ dashboard_path }}"
               view_path: "{{ dashboard_view }}"
-          - if: "{{ not is_muted }}"
-            then:
-            - wait_template: >-
-                {{ is_state_attr(repeat.item, 'app_name', 'Home Assistant Lovelace' ) }}
-            - service: media_player.volume_mute
-              data:
-                is_volume_muted: false
-              target:
-                entity_id: "{{ repeat.item }}"
+          - wait_template: >-
+              {{ is_state_attr(repeat.item, 'app_name', 'Home Assistant Lovelace' ) }}
+          - delay:
+              seconds: 2
+          - service: media_player.volume_mute
+            data:
+              is_volume_muted: false
+            target:
+              entity_id: "{{ repeat.item }}"
           else:
           - service: system_log.write
             data:

+ 8 - 20
automation/motion_detected_lights.yaml

@@ -108,17 +108,6 @@ trigger_variables:
     {{ synced_lights_input|unique|sort }}
 
 trigger:
-  ############ Hack for ############
-  # - platform: state              #
-  #   entity_id: "{{ sensors }}"   #
-  ##################################
-  # - 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: state
     # from: "off"
     to: "on"
@@ -191,7 +180,7 @@ action:
       - alias: "Motion: Turn on exisiting scene or 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 }}"
+          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
@@ -268,7 +257,7 @@ action:
                     Light levels changed: {{ synced_lights }} from {{ synced_lights_values }}.
                     Saving snapshot to scene «{{ scene_name }}».
 
-              - alias: "Store variable a variable with ON lights."
+              - alias: "Store a variable with ON lights."
                 variables:
                   synced_lights_on: >-
                     {% set lights = namespace(on=[]) %}
@@ -291,13 +280,12 @@ action:
                 data:
                   scene_id: "{{ scene_name }}"
                   snapshot_entities: >-
-                    {{ synced_lights if synced_lights_on|count != 0 else [ invalid_light ] }}
+                    {{ [ invalid_light ] if synced_lights_on|count == 0 else synced_lights }}
 
-        - 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"
-          
+        - sequence: # @ignore: Missing property "condition"
+          # 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.
           - alias: "Wait for motion to end."
             wait_template: >-
               {% set sensors = namespace(triggered=false) %}
@@ -312,7 +300,7 @@ action:
               logger: "{{ scene_name }}"
               message: >-
                 No motion detected by {{ trigger_sensors }}. Run wait actions
-                wait {{ (delay_seconds * 1/3)|int }} seconds,, then end 
+                wait {{ (delay_seconds * 2/3)|int }} seconds,, then end 
                 automation (parallelism) by issuing an event, and let automation «re-trigger».
 
           - alias: "Run and wait for actions to finish"

+ 31 - 6
script/create_device_class_groups.yaml

@@ -24,33 +24,58 @@ sequence:
         {{ 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 }}
+  - service: system_log.write
+    data:
+      level: "warning"
+      message: >-
+        Creating groups for:
+
+        - {{ device_classes | join("\n\n- ")}}
+
+        and areas:
+
+        - {{ areas | join("\n\n- ")}}
   - repeat:
       while: "{{ repeat.index <= num_device_classes }}"
       sequence:
         - variables:
-            device_class: "{{ device_classes[ repeat.index - 1 ] }}"
+            device_class_name: "{{ device_classes[ repeat.index - 1 ] }}"
+            devices: >-
+              {{ states|selectattr('attributes.device_class', 'eq', device_class_name)|map(attribute='entity_id')|list }}
         - choose:
-            conditions: "{{ states|selectattr('attributes.device_class', 'eq', device_class)|list|length > 0 }}"
+            conditions: "{{ devices|length > 0 }}"
             sequence: 
+              # - service: system_log.write
+              #   data:
+              #     level: "warning"
+              #     message: "Creating «group.{{ device_class_name }}» for {{ devices|length }} entities: {{ devices }}"
               - service: group.set
                 data:
-                  object_id: "{{ device_class }}"
-                  add_entities: "{{ states|selectattr('attributes.device_class', 'eq', device_class)|map(attribute='entity_id')|list }}"
+                  object_id: "{{ device_class_name }}"
+                  add_entities: "{{ devices }}"
               - repeat:
                   while: "{{ repeat.index <= num_areas }}"
                   sequence:
                     - variables:
                         area: "{{ areas[ repeat.index - 1 ] }}"
+                        area_group_name: "{{ area.replace(' ', '_')|lower + '_' + device_class_name }}"
                         area_entities: >-
                           {% set entities = namespace(list=[]) %}
-                          {% for entity in expand('group.' + device_class) if area_name(entity.entity_id) == area %}
+                          {% for entity in expand('group.' + device_class_name) 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: system_log.write
+                          #   data:
+                          #     level: "warning"
+                          #     message: >-
+                          #       Creating «group.{{ area_group_name }}» with:
+
+                          #       {{ area_entities | join("\n\n- ")}}
                           - service: group.set
                             data:
-                              object_id: "{{ area.replace(' ', '_')|lower + '_' + device_class }}"
+                              object_id: "{{ area_group_name }}"
                               add_entities: "{{ area_entities }}"

+ 129 - 11
script/notify_user.yaml

@@ -1,24 +1,142 @@
+# - service: script.notify_users
+#   data_template:
+#     title: "It's {{ first_name }}'s birthday!"
+#     users:
+#       - notify.owners
+#     message: >-
+#       It is {{ full_name }}'s birthday today. {% if age is not none %}
+#         {{- first_name }} is turning {{ age }} years.
+#       {% endif %}
+      
+#       Send them a text and say <b>Happy Birthday!</b> to let them know that you appriciate them! :)
+#     data_object:
+#       tag: "birthday-notification-{{ context.id }}-{{ first_name | upper }}"
+#       group: "birthday-notifications"
+#       color: "#A83285"
+#       icon_url: "https://home-assistant.kgv14.dev:8123/local/entities/birthday-cake.png"
+#       actions:
+#         - action: "{{ action_sms }}"
+#           title: "Send SMS"
+# - wait_for_trigger:
+#     platform: event
+#     event_type: mobile_app_notification_action
+#     event_data:
+#       action: "{{action_sms}}"
+#   timeout:
+#     hours: 24
+# - choose:
+#     conditions: "{{ wait.trigger is not none and wait.trigger.event.data.action == action_sms }}"
+#     sequence:
+#       service: notify.owners
+#       data:
+#         message: command_activity
+#         title: "sms:{{ phone_number }}?body=Gratulere med {{ age|string + 'års ' if age is not none else '' }}dagen  {{first_name}}!"
+#         data:
+#           tag: "android.intent.action.SENDTO"
+#   default:
+#     service: notify.owners
+#     data:
+#       message: clear_notification
+#       data:
+#         tag: "birthday-notification-{{ context.id }}-{{ first_name | upper }}"
+
 blueprint:
   domain: script
   name: Notify user
   description: >-
     Notify a user
   input:
-    notify_device:
-      name: "Device to notify"
+    notify_devices:
+      name: "Device(s) to notify"
       selector:
-        device:
-          integration: mobile_app
+        entity:
+          multiple: true
       default: []
+    title:
+      name: "Title"
+      selector:
+        text:
+          multiline: false
+      default: ""
+    message:
+      name: "Message"
+      selector:
+        text:
+          multiline: true
+    data:
+      name: "Data object"
+      selector:
+        object:
+      default: {}
+    tts:
+      name: "TTS"
+      selector:
+        boolean:
+      default: "{{ false }}"
 
 mode: parallel
 
 variables:
-  device: !input notify_device
-  data: "{{ {} }}"
-
+  devices_input: !input notify_devices
+  devices: >-
+    {% set devices = namespace(entity=[]) %}
+    {% for device in (
+          devices_input if devices_input is iterable and (
+            devices_input is not string and devices_input is not mapping
+          ) else [ devices_input ]
+        ) if device.startswith('notify.') or device.startswith('group.') %}
+      {% if device.startswith('group.') %}
+        {% for group_device in state_attr(device, 'entity_id') if group_device.startswith('notify.') %}
+          {% set devices.entity = devices.entity + [ group_device ] %}
+        {% endfor %}
+      {% else %}
+        {% set devices.entity = devices.entity + [ device ] %}
+      {% endif %}
+    {% endfor %}
+    {{ devices.entity }}
+  title: !input title
+  message: !input message
+  data: !input data
+  tts: !input tts
+  actions: >-
+    {% set actions = namespace(name=[]) %}
+    {% for action in (data.actions if 'actions' in data else []) %}
+      {% set actions.name = actions.name + [ action.action ] %}
+    {% endfor %}
+    {{ actions.name }}
+  tag: "tag-{{ this.context.id }}"
 sequence:
-  - alias: "Send notification"
-    service: "{{ device }}"
-    # data: >-
-    #   "{{ data if defined else {} }}"
+  - service: system_log.write
+    data:
+      level: warning
+      message: >-
+        Notify-devices:
+        
+        - {{ devices_input|join("\n\n - ") }}
+        
+        Devices: 
+
+        - {{ devices|join("\n\n - ") }}
+
+        Data: {{ data }}
+
+        Actions: {{ actions }}
+  - repeat:
+      for_each: "{{ devices }}"
+      sequence:
+      - variables:
+          tag: "{{ tag }}-{{ repeat.index }}"
+      - if: "{{ tts }}"
+        then:
+          alias: "Send TTS"
+          service: "{{ repeat.item }}"
+          data:
+            title: "{{ title if title.strip()|length > 0 else message|truncate(50, true) }}"
+            message: "TTS"
+      - alias: "Send notification"
+        service: "{{ repeat.item }}"
+        data:
+          title: "{{ title if title.strip()|length > 0 else '' }}"
+          message: "{{ message }} {{ tag }}"
+          data: >-
+            {{ data }}