I am trying to change TV Channels on a webOS LG TV with script.
There is a condition to see if the TV is off then turn it on first.
Everything works the channels change the TV turns on.
But I need a delay to wait for the channel change, if the TV has just been turned on.
The turning on process takes too long and the channel change is missed.
How can I put the delay in the IF sequence?
I am getting only "Template rendered invalid service: „ if i try.
First, the simpler way would be to just turn the TV on, even if it’s already on, and add a delay immediately after it. This should work if turning on the TV if it’s already on has no negative side effects, and you don’t mind always having the delay.
If that doesn’t work for you, then the other thing to try is to have a second script that starts with a condition that the TV is off. If it isn’t (i.e., it’s already on), then the script would just exit. But if the TV is off, then the next steps would be to turn it on and then delay. Then in the original script, replace the step to turn the TV on with a call to the second script, followed by a wait_template that waits until the second script is ‘off’. (You may need to add a one or two second delay between calling the script and the wait_template because sometimes a script’s state doesn’t change to ‘on’ immediately when it is called.)
Let me know if you need any help with implementing one of these if you’d like to try it.
EDIT: Oh, and a third option would be to write it as a python_script instead.
### Changing TV channels
tv_channel_nova:
alias: Nova
sequence:
- service: script.turn_tv_obyvak_on
- delay: 2
- wait_template: "{{ is_state('script.turn_tv_obyvak_on', 'off') }}"
- service: media_player.play_media
data:
entity_id: media_player.tv_obyvak
media_content_id: "Nova | T2"
media_content_type: "channel"
turn_tv_obyvak_on:
alias: Turn TV obyvak on
sequence:
- condition: template
value_template: "{{is_state('media_player.tv_obyvak', 'off') }}"
- service: media_player.turn_on
entity_id: media_player.tv_obyvak
# Make this delay whatever is necessary to give the TV time to turn on.
- delay: 5
So some explanation is in order. When one script starts (aka calls) another, it doesn’t normally wait for the called script to finish. Effectively they both continue to execute “in parallel.” So you have to make the first script wait for the second script to finish. (Note that the second script will either finish almost immediately if the TV is already on, or it will finish after turning the TV on and completing the delay at the end of the script.)
Unfortunately the state of the called script will not change immediately. (The state of a script indicates whether or not it is running - ‘on’ if it is running, and ‘off’ if it is not.) If the next step in the calling script (i.e., the wait_template) were to run before the called script’s state changed to ‘on’, the calling script would end up not actually waiting for the called script to finish. To help guarantee it will, you can add a delay before the wait_template, like I did above. Unfortunately, again, there’s really no absolute way to know how long to make this delay. I can tell you I have used this technique in a few of my scripts and in some I get away with no delay at all before the wait_template, and it works fine. But in other cases I’ve had to introduce a delay before the wait_template.
Sorry, but this seems to be the state of affairs at this time. I should probably start scratching my head about how to enhance the calling of a script to allow an option to make the caller wait for the callee to finish. Hmm…