Joachim M. Giæver 5 months ago
parent
commit
956e479c59
2 changed files with 18 additions and 16 deletions
  1. 13 13
      automation/motion_detected_lights.yaml
  2. 5 3
      script/notify_user.yaml

+ 13 - 13
automation/motion_detected_lights.yaml

@@ -161,12 +161,12 @@ action:
         data:
           level: warning
           logger: "{{ scene_name }}"
-          message: "Lights: {{ synced_lights }} vs {{ synced_lights_input }}"
+          message: "Lights: {{ synced_lights }} vs {{ synced_lights_input }}. Invalid light ({{ invalid_light }})."
 
       - 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 invalid_light not in state_attr('scene.' + scene_name, 'entity_id') }}"
           sequence: # Scene do exist and is valid
 
             - service: system_log.write
@@ -174,7 +174,7 @@ action:
                 level: "{{ log_level }}"
                 logger: "{{ scene_name }}"
                 message: >-
-                  Scene «{{ scene_name }}» already exists and contains all lights. Restore scene.
+                  Scene «{{ scene_name }}» already exists and contains valid lights ({{ state_attr('scene.' + scene_name, 'entity_id') }}). Restore scene.
             
             - alias: "Motion: Turn on existing scene for synced lights"
               service: scene.turn_on
@@ -189,7 +189,7 @@ action:
               logger: "{{ scene_name }}"
               message: >-
                 Scene «{{ scene_name }} does NOT exist or is INVALID. 
-                Entities to sync: {{ synced_lights }} => Turn ON every light sourcce.
+                Entities to sync: {{ synced_lights }} => Turn ON every light source.
         
           - alias: "Motion: Turn on synced lights"
             service: light.turn_on
@@ -214,10 +214,10 @@ action:
                   synced_lights_values: >-
                     {% set lights = namespace(values=[]) %}
                     {% for light in synced_lights %}
-                      {%if is_state_attr(light, 'color_mode', 'brightness') %}
+                      {%if is_state_attr(light, 'supported_color_modes', 'brightness') %}
                         {% set lights.values = lights.values + [state_attr(light, 'brightness')] %}
                       {% else %}
-                        {% set lights.values = lights.values + [states(light)] %}
+                        {% set lights.values = lights.values + [ states(light) ] %}
                       {% endif %}
                     {% endfor %}
                     {{ lights.values }}
@@ -226,23 +226,23 @@ action:
                 wait_template: >-
                   {% set lights = namespace(changed=false) %}
                   {% for light in synced_lights %}
-                    {%if is_state_attr(light, 'color_mode', 'brightness') and not is_state_attr(light, 'brightness', synced_lights_values[loop.index0]) %}
+                    {%if is_state_attr(light, 'supported_color_modes', 'brightness') and not state_attr(light, 'brightness') == synced_lights_values[loop.index0] %}
                       {% set lights.changed = true %}
-                    {% elif not is_state_attr(light, 'color_mode', 'brightness') and not is_state(light, synced_lights_values[loop.index0]) %}
+                    {% elif not states(light) == synced_lights_values[loop.index0] %}
                       {% set lights.changed = true %}
                     {% endif %}
                   {% endfor %}
                   {{ lights.changed }}
 
-              - alias: "Store variable with new light values (on or brightness)"
+              - alias: "Store variable with new light values (on or brightness), for debuging purposes"
                 variables:
                   synced_lights_new_values: >-
                     {% set lights = namespace(values=[]) %}
                     {% for light in synced_lights %}
-                      {%if is_state_attr(light, 'color_mode', 'brightness') %}
+                      {%if is_state_attr(light, 'supported_color_modes', 'brightness') %}
                         {% set lights.values = lights.values + [state_attr(light, 'brightness')] %}
                       {% else %}
-                        {% set lights.values = lights.values + [states(light)] %}
+                        {% set lights.values = lights.values + [ states(light) ] %}
                       {% endif %}
                     {% endfor %}
                     {{ lights.values }}
@@ -253,7 +253,7 @@ action:
                   level: "{{ log_level }}"
                   logger: "{{ scene_name }}"
                   message: >-
-                    Light levels changed: {{ synced_lights }} from {{ synced_lights_values }} to {{ synced_lights_new_values }}.
+                    Light levels changed: {{ synced_lights }}\n\nfrom:{{ synced_lights_values }}\n\nto:{{ synced_lights_new_values }}.
                     Saving snapshot to scene «{{ scene_name }}».
 
               - alias: "Store a variable with ON lights."
@@ -338,7 +338,7 @@ action:
           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 %}
+              {% if is_state_attr(light, 'supported_color_modes', 'brightness') and state_attr(light, 'brightness')|int != 0 %}
                 {% set brightness.levels = brightness.levels + [ state_attr(light, 'brightness')|float ] %}
               {% else %}
                 {% set brightness.levels = brightness.levels + [ 100 ] %}

+ 5 - 3
script/notify_user.yaml

@@ -246,7 +246,7 @@ sequence:
   - service: system_log.write
     data:
       level: warning
-      message: "CONTEXT:{{ ctx }} \n\nID: {{ context.id }} == {{ this.context.id }} == {{ context.id == this.context.id }}\n\nDATA: {{update_data}}\n\n{{this}}"
+      message: "CONTEXT:{{ ctx }} \n\nID: {{ context.id }} == {{ this.context.id }} == {{ context.id == this.context.id }}\n\nDATA: {{update_data}}\n\n{{this}}\n\nDevice ID: {{device_id}}"
 
   - alias: "Parallize event and action listeners"
     parallel:
@@ -288,7 +288,8 @@ sequence:
                     timeout: >-
                       {{ update_timeout - now().timestamp() }}
 
-                - if: "{{ wait.trigger is none }}"
+                - alias: "Check if trigger is none"
+                  if: "{{ wait.trigger is none }}"
                   then:
                     alias: "Reached timeout, ending."
                     stop: "Reached timeout, ending."
@@ -344,7 +345,8 @@ sequence:
                       context: "{{ context.id }}"
                       user: '{{ states.person|selectattr("attributes.user_id", "==", wait.trigger.event.context.user_id)|map(attribute="attributes.friendly_name")|first }}'
 
-                - variables:
+                - alias: "Set action variable"
+                  variables:
                     action: >-
                       {{ action_handlers[ wait.trigger.event.data.action ]|default(false) }}