How to implement script feedback and script success evaluation?

Hello everyone,
not sure if I am in the right place or not. In Discord there is an area for automations but somehow I prefer the forum.

So, I have two questions regarding scripts and automations.

Number one:
I would like to create a feedback to the device that ran a script or automation. However, I use the same user across multiple devices, so the user_id is not helpful. So I need to somehow notify the mobile_app that ran the script.
Does anybody know if this is possible and if it is, how I can achieve this?

Number two:
I have found that scripts do not always run reliably or rather, the script runs but the recipient of the broadcast is not listening/reacting.
In my case, I send commands to my thermostats (TRV) via MQTT. And I find that if I address multiple ones at once (e.g. turn all off), that I have to run the script 2-3 times to ensure that each one actually performs the action I requested.
Is there a way to automatically run the script again, if the targets did not react as planned? Ideally built in to the script itself?
Alternatively, a global automation that is trigger when a script is called and then checks the content of the script, checks if everything has been performed, and then runs the script again if not. So the automation would see that I am calling the action

    - action: climate.set_temperature
      data:
        temperature: 4.5
      target:
        entity_id: "{{states.climate
          |selectattr('entity_id', 'search', 'climate.eq3')
          |map(attribute='entity_id')
          |list}}"

but one or more of the TRVs has remained at a different value, then the script is automatically re-run until all have the correct target temperature.

Thank you
Alex

You could use the while loop and condition the state (set_temperature) of the TRV.
Just remember to add a delay for a few seconds in the loop also so that you don’t send commands too quickly

3 Likes

Ah, yes, took some tinkering, but I think I have it working now

Summary
script:
  climate_all_off:
    alias: "Climate: All Off"
    mode: queued
    sequence:
      # --- Step 1: ensure hvac_mode == heat for all EQ3 climates
      - repeat:
          while:
            - condition: template
              value_template: >
                {% set ids = states.climate
                  | selectattr('entity_id','search','climate.eq3')
                  | map(attribute='entity_id') | list %}
                {% if ids | length == 0 %}
                  false
                {% else %}
                  {% set ns = namespace(not_ready=false) %}
                  {% for s in expand(ids) %}
                    {% if s.state == 'auto' %}
                      {% set ns.not_ready = true %}
                      {% break %}
                    {% endif %}
                  {% endfor %}
                  {{ ns.not_ready }}
                {% endif %}
          sequence:
            - repeat:
                for_each: >
                  {{ states.climate
                    | selectattr('entity_id','search','climate.eq3')
                    | selectattr('state','search','auto')
                    | map(attribute='entity_id')
                    | list }}
                sequence:
                  - service: climate.set_hvac_mode
                    target:
                      entity_id: "{{ repeat.item }}"
                    data:
                      hvac_mode: heat
                  - delay:
                      seconds: 4.3
            - delay:
                seconds: 10.3  

      # --- Step 2: set temperature for all EQ3 climates and wait until set
      - repeat:
          while:
            - condition: template
              value_template: >
                {% set ids = states.climate
                  | selectattr('entity_id','search','climate.eq3')
                  | map(attribute='entity_id') | list %}
                {% if ids | length == 0 %}
                  false
                {% else %}
                  {% set target = 4.5 %}
                  {% set ns = namespace(not_ready=false) %}
                  {% for s in expand(ids) %}
                    {% if s.attributes.current_temperature is not defined %}
                      {% set ns.not_ready = true %}
                      {% break %}
                    {% endif %}
                    {% if (s.attributes.temperature | float) != (target | float) %}
                      {% set ns.not_ready = true %}
                      {% break %}
                    {% endif %}
                  {% endfor %}
                  {{ ns.not_ready }}
                {% endif %}
          sequence:
            - repeat:
                for_each: >
                  {% set target = 4.5 %}
                  {% set ns = namespace(ids=[]) %}
                  {% for s in states.climate
                    | selectattr('entity_id','search','climate.eq3')
                    | selectattr('attributes.temperature','defined')
                    | selectattr('attributes.temperature','defined') %}
                    {% if (s.attributes.temperature | float) != (target | float) %}
                      {% set ns.ids = ns.ids + [s.entity_id] %}
                    {% endif %}
                  {% endfor %}
                  {{ ns.ids }}
                sequence:
                  - service: climate.set_temperature
                    target:
                      entity_id: "{{ repeat.item }}"
                    data:
                      temperature: 4.5
                  - delay:
                      seconds: 4.3
            - delay:
                seconds: 10.3                       

Any ideas about the response to the issuing device?

Not any solution that would be usable.

1 Like