Wait automation not triggering

I have just created an automation that determines the order in which the radiators heat up but it’s not triggering.

Any idea what the problem might be ?

alias: Wiser Order
trigger: []
action:
  - wait_template: |-
      {%- for item in result.trvs -%}
        {%- if state_attr(item[0], 'temperature')|float >= (item[1]|float+degrees) -%}
          {%- set r2.done = r2.done + 1 -%}
          {%- set result.trvs = result.trvs[:r2.done] + [(item[0], item[1], (now() - r2.start).total_seconds()|int)] + result.trvs[r2.done+1:] -%}
        {%- endif -%}
      {%- endfor -%}
      {%- if r2.done >= r2.count -%}
        true
      {%- endif -%}
  - service: persistent_notification.create
    data:
      message: "{{ sensors }}"
variables:
  degrees: 0.1
  start: "{{ now() }}"
  sensors: |-
    {% set result = namespace(trvs=[],start=now()) %}
    {% for trv in states.sensor %}
      {% if 'trv_' in trv.entity_id and '_signal' in trv.entity_id %}
          {% set result.trvs = result.trvs + [(trv.entity_id, trv.attributes.temperature,0)] %}
      {% endif %}
    {% endfor %}
    {%- set r2 = namespace(start=now(),done=0, count=result.trvs|length) -%}
    {{ result.trvs }}
mode: single

You don’t have any triggers.

Sorry, should have explained I trigger this manually.

There’s also a lot of issues with the templates. You’re making an assumption that variables are shared through the templates, which they are not.

Thanks for helping, appreciate that, been trying this on and off all day.

Are you referring to this:

{%- set r2 = namespace(start=now(),done=0, count=result.trvs|length) -%}

I have now moved it to to top of the wait_template

Yes, but there’s a lot more you need to fix. You’re using result.trvs in the wait template, namespaces cannot be passed between templates.

Oh yes, I did not notice that, I should be using the sensors variable.

I also think you have a miss-conception on how the template will work. It’s going to roll up immediately. It’s not going to spend time executing the template over and over again.

EDIT: Well, nevermind, it will. Sorry, disregard.

alias: Wiser Order
trigger: []
variables:
  degrees: 0.1
  start: "{{ now() }}"
  sensors: |-
    {% set result = namespace(trvs=[]) %}
    {% for trv in states.sensor %}
      {% if 'trv_' in trv.entity_id and '_signal' in trv.entity_id %}
          {% set result.trvs = result.trvs + [(trv.entity_id, trv.attributes.temperature)] %}
      {% endif %}
    {% endfor %}
    {{ result.trvs }}
action:
  - wait_template: |-
      {%- set result = namespace(done=[]) -%}
      {%- for entity_id, temperature in sensors -%}
        {%- if state_attr(entity_id, 'temperature') >= (temperature + degrees) -%}
          {%- set result.done = result.done + [(entity_id, temperature, (now() - start).total_seconds()|int)] -%}
        {%- endif -%}
      {%- endfor -%}
      {{ result.done | length == sensor | length }}
  - service: persistent_notification.create
    data:
      message: All trvs complete

mode: single

Pretty sure this is what you’re looking for. Unfortunately, because you’re using a wait template, you can’t get the done results out.

Thanks very much, but that’s exactly what I need, I need to know the order in which each radiator heats up by x degrees so that I can then balance them by adjusting the lockshield valves on each radiator, starting with the one that heats up first (closeset to the boiler)

I dont really care how much time they took, all I need is the order

I’ll have to think this over for a bit.

Thanks very much

It you have no triggers and you run this manually you should be using a script, not an automation. All the same actions are available. Automations are just triggered scripts.

Thanks, but how would a script work and prompt the order when it ends where the automation would not ?

@petro - I just tried your last code, unfortunately it does not appear to wait for each temp to reach and completes immediately.

Like this:

script:
  wiser_order:
    sequence:
      - variables:
          degrees: 0.1
          start: "{{ now() }}"
          sensors: |-
            {% set result = namespace(trvs=[]) %}
            {% for trv in states.sensor %}
              {% if 'trv_' in trv.entity_id and '_signal' in trv.entity_id %}
                  {% set result.trvs = result.trvs + [(trv.entity_id, trv.attributes.temperature)] %}
              {% endif %}
            {% endfor %}
            {{ result.trvs }}
      - wait_template: |-
          {%- set result = namespace(done=[]) -%}
          {%- for entity_id, temperature in sensors -%}
            {%- if state_attr(entity_id, 'temperature') >= (temperature + degrees) -%}
              {%- set result.done = result.done + [(entity_id, temperature, (now() - start).total_seconds()|int)] -%}
            {%- endif -%}
          {%- endfor -%}
          {{ result.done | length == sensor | length }}
      - service: persistent_notification.create
        data:
          message: All trvs complete

Scripts are services so you can call it from a button tap action in your dashboard.

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: script.wiser_order
icon: mdi:valve

EDIT: if petro’s automation has issues so will that script. It is the same.

Thanks for that, but the problem is I dont just need to know when all TRVs have reached their temperature, but I need to know the order in which they reach temperature so that I can determine what the sequence is in order to adjust the lockshield valves and balance the system.

The limitation with Petro’s script is that it cannot report out the data and only a text message saying “All trvs complete”.