core icon indicating copy to clipboard operation
core copied to clipboard

Value_Templates not not work with Repeat Until/While in Scripts

Open GSVil opened this issue 1 year ago • 1 comments

The problem

Using templates with Repeat Until/While does not work when trying to validate against the state of an entity. It does not appear to pick up state changes. Using the Script Builder (GUI) works, but I'm using value_template's so it will not work for me.

I'm trying to check when the state of a media-player changes to IDLE. It only sees the initial state (for example playing), even if the state changes during the script execution.

I have tested with both is_state () and states (), both fail to retrieve the state changes of an entity.

What version of Home Assistant Core has the issue?

core-2024.10.2

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Scripts

Link to integration documentation on our website

https://www.home-assistant.io/integrations/script/

Diagnostics information

No response

Example YAML snippet

Tested both the following, they fail to validate correctly when the state of an entity changes.
**TEST 1**
repeat:
  until:
    - condition: template
      value_template: "{{ is_state('media_player.bar_voice_assistant_speaker', 'idle') }}"
  sequence:
    - delay:
        milliseconds: 500 
    enabled: 

**TEST 2**
repeat:
  until:
    - condition: template
      value_template: "{{ states('media_player.bar_voice_assistant_speaker') == 'idle' }}"
  sequence:
    - delay:
        milliseconds: 500 
    enabled: 

**TEST 3** - Works, but it does not use value_template, which is what I'm trying to use.
 repeat:
  until:
    - condition: state
      entity_id: media_player.bar_voice_assistant_speaker
      state: idle
  sequence:
    - delay:
        milliseconds: 500

Anything in the logs that might be useful for us?

No response

Additional information

No response

GSVil avatar Oct 14 '24 20:10 GSVil

Hey there @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (script) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of script can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign script Removes the current integration label and assignees on the issue, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


script documentation script source (message by IssueLinks)

home-assistant[bot] avatar Oct 14 '24 20:10 home-assistant[bot]

Edit - I retract my previous statement saying it might be with checking states for media_player. It turns out that the new (in 2024.10) code for TTS seems to return much quicker - before the media_player changes from idle to buffering or playing, which caused my code to instantly identify the idle state and assume it had finished playing the TTS. To fix this, you need to wait for the playing state, then check for idle.

esand avatar Oct 25 '24 18:10 esand

Hi. Can you please paste code for the solution? I'm struggling with this too.

litecross91 avatar Nov 12 '24 21:11 litecross91

Hi. Can you please paste code for the solution? I'm struggling with this too.

Not certain if you're addressing me - but if so, here's a link to a gist I created for my TTS script: https://gist.github.com/esand/63d14a68ca27773f6ffb537166c2f2a8

As mentioned, the fix was for me to wait for the state 'playing' and then wait for 'idle'. Previously, I was able to send the TTS out to the media players and wait for 'idle' and it worked ok. In recent HA versions though, the TTS command returns before the media player changes from a pre-existing idle state, so my wait would return prematurely, before it had even started playing the TTS.

esand avatar Nov 12 '24 22:11 esand

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.