Script stops execution after template if statement returns false

Hi community,

I have spent the better part of my morning trying to figure out my service_templates. I have a script that runs when my chromecast starts playing. I want it to turn on the stereo (if it was off), change the input and turn on a specific light scene if the sun is down.

The commands are IR commands, and the on command is the same as the off command, so hence why I try to check if it is currently on or off. It works fine when the stereo was off, but it seems to stop executing everything if the stereo was already on. I.e., it doesn’t change the channel and activate the scene. This is my code:

tv_on_for_chromecast:
  sequence:
  - data:
      entity_id: switch.logitech_z906
    service_template: >
      {% if is_state('switch.logitech_z906', 'off') %}
        switch.turn_on
      {% endif %}
  - delay: 00:00:04
  - data:
      entity_id:
    service: shell_command.toggle_z906_input
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - data:
      entity_id: scene.movies
    service: scene.turn_on
  alias: Chromecast Plays

I appreciate your help, thank you in advance!

If you look at your error log, you probably have an error related to passing the entity ID data with no service when your template returns false. If you don’t pass a service, you can’t pass the data for that service (oh how I wish HA would just skip that step in these cases though!)

How are you activating this script? You may need to set this up with a bit of a workaround where you launch a script that simply determines if the stereo is on first, and then from there launches one of two secondary scripts; one that starts by turning the stereo on if it was off, and another that doesn’t include that step at all if it was already on.

Hi Brett,

Thank you for your response. Sorry for my late reply, I haven’t had the time to look at it again before now. It seems you are absolutely right. That is quite annoying indeed. I was launching this script by checking if the state of the chromecast changed from ‘off’’ to ‘playing’.

Would it be possible to use a data_template for the entity_id, together with the service_template? That way I could either also supply an empty entity_id, or call a script that does nothing but wait for a couple of milliseconds. I had a fiddle around but it kept saying invalid config when I attempted the data_template.

My guess would be that you cannot do that as I think HA will still try to pass the blank value for both which causes the error.

Another alternative could be to do something else like submit something to a log as your action with data instead of it just being blank, or splitting things up as I suggested in my previous post.

Remove the service template for the switch and just use the turn on service.

If it is off it will be turned on, and if it is on already it doesn’t matter.

Using something else worked, thanks!
I used the homeassistant.update_entity service, which could take the same entity_id as the switch.turn_on.

Unfortunately, that sends the on signal again, which would then turn it off.

Ah, I assumed you had discrete on and off commands. If it’s only a toggle, yeah that won’t work.

You even said it in your original post:

Sorry.