Repeat until, mulitple triggers, trigger.entity_id

Hi there!

I think I have a rather specific problem with repeat until logic.
My automation looks as follows:

alias: Fenster offen!
description: ''
variables:
  time: -5
  timedelay: 5
trigger:
  - platform: state
    entity_id:
      - binary_sensor.req0542232_state
      - binary_sensor.req0542255_state
      - binary_sensor.req0542235_state
      - binary_sensor.req0542213_state
      - binary_sensor.req0542146_state
      - binary_sensor.peq0569322_state
    from: off
    to: on
condition: []
action:
  - repeat:
      sequence:
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
        - variables:
            time: '{{ time + timedelay }}'
        - service: input_text.set_value
          data_template:
            entity_id: input_text.message_title
            value: Fensterwarnung
        - service: input_text.set_value
          data_template:
            entity_id: input_text.message_text
            value: >-
              {{ trigger.from_state.attributes.friendly_name }} ist seit {{ time
              }} Sekunden offen.
        - service: script.send_message
      until:
        - condition: state
          entity_id: "{{ trigger.entity_id }}"
          state: off
mode: restart
max: 20

… as you can see, I am trying to accomplish a window open warning, which can be triggered by either of my windows opening.
I would like to repeat this message (ideally displaying the correct time for how long it is already open) with the stop condition being the closing of said window. However, when I am trying to save the code above, I get the error message that the entity_id is invalid: “Message malformed: Entity {{ trigger.entity_id }} is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘action’][0][‘repeat’][‘until’][0][‘entity_id’]”.
However what I don’t get is why the friendly name of the trigger can be evaluated, but the entity_id cannot.

Could anybody help me please? I also searched this forum, but I couldn’t find a solution. I think my problem is that I have mulitple triggers…

Thanks a lot in advance!

Patrick

The State Condition’s entity_id option wants a valid entity_id described as a string but got this {{ trigger.entity_id }} which is a template. Not all options support templates.

Change it to a Template Condition.

      until: "{{ is_state(trigger.entity_id, 'off') }}"

Hi there.

Thanks a lot for this input, this works like a charm!
The only thing missing now is the accurate tracking of time.
Still I have the problem that every message it resets the variable “time”. Is there any way to avoid the resetting of the variable, when being inside the repeat until?

Thanks a lot for the support :slight_smile: This is what I have been missing with other home automatisation softwares :slight_smile:

I suggest you modify the automation like this:

alias: Fenster offen!
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.req0542232_state
      - binary_sensor.req0542255_state
      - binary_sensor.req0542235_state
      - binary_sensor.req0542213_state
      - binary_sensor.req0542146_state
      - binary_sensor.peq0569322_state
    from: off
    to: on
    for: '00:00:05'
condition: []
action:
  - repeat:
      while: "{{ is_state(trigger.entity_id, 'on') }}"
      sequence:
        - service: input_text.set_value
          target:
            entity_id: input_text.message_title
          data:
            value: Fensterwarnung
        - service: input_text.set_value
          target:
            entity_id: input_text.message_text
          data:
            value: >-
              {{ trigger.from_state.attributes.friendly_name }} ist seit {{ repeat.index * 5 }} Sekunden offen.
        - service: script.send_message
        - delay: '00:00:05'
mode: restart
max: 20

The automation is triggered when one of the six binary_sensors changes from off to on and stays on for at least 5 seconds. The automation’s action uses a repeat - while to report the window is open every 5 seconds. It computes the elapsed time by simply multiplying repeat.index (a variable that is automatically incremented for each loop) by 5. The repeat - while iterates as long as the binary_sensor’s state remains on and ceases when it changes back to off.

Thanks a lot for the support.
I will try this solution this evening, but the code itself looks as it might work!
I did not know that I can access the repeat.index - this is of course much better than my solution!

I think this topic can be closed - again thanks for the support :slight_smile:

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

Sorry to open up this topic again.
Your solution works very fine, has, however a fundamental flaw - or maybe this is a general flaw of my overall approach: If two consecutive windows have been opened, i.e. the automation has been triggered once by one entity, and another enitity triggers the automation, the first automation will be overwritten, i.e. I will not receive any more message about the first opened window…
Is there any way to avoid it?

Again, thanks in advance for your support.

Sorry, solved it myself. The script, by accident was set to restart. parallel works though. Sorry.

I noticed that when I revised your automation but I left the mode as restart because I thought that’s the way you wanted it to work (the notification for one binary_sensor is superseded by the next binary_sensor). If you want all notifications to occur concurrently then, yes, you must change the mode to parallel.