Wait for trigger triggers on event that happened before

Hi,
The following problem occurred after I updated from 2024.2.3 till 2024.4.4 on my NAS. I have an automation configured in parallell mode. In the UI I have an input_number (call it “amount”) which I modify and that triggers the automation. I also have an input_select entity called maltid that can be changed from my dashboard or by the automation.
Use case is that I enter the amount of medicine I take, and that should make the input_select entity to change to “Medicine”. After 5 minutes I am expected to having finished the medicine so the automation will make the input_select entity go back to “Not eating”. However, I might manually select “Breakfast” during the 5 min. Then, the automation should not change to “Not eating” after 5 min.

Automation conf:
Trigger: Amount changes

(Input select entity is initially set to A=“Not eating”)

Actions:
1) Input select, select option B = “Medicine”
2) Define two variables:
i) currenttime: “{{now}}”
ii) mealtypechangetime: “{{states.input_select.maltid.last_updated}}”
3) Wait (with timeout 300 seconds) for input_select.maltid to change
4) Input select, select option A=“Nothing”.

Inspection of the trace:


	1) Executed at 22:55:21.
	2) Executed at 22:55:21.
		Changed variables:
			currenttime: 		'2024-05-04 22:55:21.102391+02:00'
			mealtypechangetime: '2024-05-04 20:55:21.099189+00:00'
	3) Executed at 22:55:21.
		Result:
		wait:
			remaining: 299.99044739996316  
			trigger:
			entity_id: input_select.maltid
			from_state:
				entity_id: input_select.maltid
				state: Not eating
				last_changed: '2024-05-04T14:27:32.975470+00:00'
			to_state:
				entity_id: input_select.maltid
				state: Medicine
				last_changed: '2024-05-04T20:55:21.099189+00:00'
	4) Executed at 22:55:21.

My comments:
1) OK
2) OK
3) Ends almost immediately - not expected. The trace tell me that it reacted to the change in input_select, that had already occured in step 1 (note the separation of six hours between changes in maltid, which means that there was no other automation that did this).
4) Not expected. Expected 23:00:21.

yaml code for the trigger part:

wait_for_trigger:
  - platform: state
    entity_id:
      - input_select.maltid
timeout:
  hours: 0
  minutes: 5
  seconds: 0
  milliseconds: 0

Post the automation.

alias: Test timeout medicin
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.testbutton
condition: []
action:
  - service: input_select.select_option
    data:
      option: Medicine
    target:
      entity_id: input_select.maltid
  - variables:
      currenttime: "{{now()}}"
      maltidlastchange: "{{states.input_select.maltid.last_changed}}"
  - wait_for_trigger:
      - platform: state
        entity_id:
          - input_select.maltid
    timeout:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: input_select.select_option
    data:
      option: Not eating
    target:
      entity_id: input_select.maltid
mode: single

wonder if the select_option is configured to run async. i didn’t think so, but… someone expert in that may be able to tell.

what happens if you try adding a from like this

alias: Test timeout medicin
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.testbutton
condition: []
action:
  - service: input_select.select_option
    data:
      option: Medicine
    target:
      entity_id: input_select.maltid
  - variables:
      currenttime: "{{now()}}"
      maltidlastchange: "{{states.input_select.maltid.last_changed}}"
  - wait_for_trigger:
      - platform: state
        entity_id:
          - input_select.maltid
        from: Medicine
    timeout:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: input_select.select_option
    data:
      option: Not eating
    target:
      entity_id: input_select.maltid
mode: single
1 Like

I solved it temporarily by adding a "wait for 1 sec " block immediately before this.
My posted solution worked for months in older HA versions so something has changed in the latest version of HA. Guess the solution with adding a “from” state would work too. I had some reason for not using it earlier but dont remember why now.