[solved] Syntax for script with service_template. Can't find the right way to define entity

All,

I’ve been playing around with this problem for a while. Can’t get it to work.
I call a script form an automation with the following:

   action:
  - service: script.media_mode
    data_template:
      device: '{{ trigger.to_state.attributes.friendly_name }}'
      old_state: '{{ states.input_select.harmony_livingroom.state }}'

The script is like this:

script:
media_mode:
alias: Set media mode script
sequence:
- service_template: >-
{% set media_devices =
[
(‘PowerOff’, ‘poweroff’),
(‘Televisie’, ‘television’),
(‘Mediaplayer’, ‘mediaplayer’),
(‘Apple-TV’, ‘appletv’),
(‘Radio’, ‘radio’),
(‘Muziek’, ‘music’),
(‘DVD’, ‘dvd’),
(‘Teletekst’, ‘teletext’),
(‘Smart-TV’, ‘smarttv’)
]
%}
{% for key, value in media_devices %}
{% if key == old_state %}
input_boolean.turn_off
data:
entity_id: input_boolean.{{ value }}
{% endif %}
{% endfor %}

Note: indentions are somehow deleted in the blockquote, they are in the code!

All the stuff in the script validates, I tested the jinja, that seems ok. But I get this error in Hass:

Error while executing automation automation.set_media_mode_automation. Invalid data for call_service at pos 1: Service input_boolean.turn_off data: entity_id: input_boolean.television does not match format <domain>.<name>
Can any one tell me where i’m worng. This is driving me nuts.

Thanks,

Ralph

Service templates require a result at all times, so you have to use a condition to stop the script instead of using a service template.

script:
  media_mode:
  alias: Set media mode script 
  sequence:
  - condition: template
    value_template: >
      {% set media_devices =
        {
        'PowerOff': 'poweroff',
        'Televisie': 'television',
        'Mediaplayer': 'mediaplayer',
        'Apple-TV': 'appletv',
        'Radio':, 'radio',
        'Muziek': 'music',
        'DVD': 'dvd',
        'Teletekst': 'teletext',
        'Smart-TV': 'smarttv'
        }
      %}
      {{ old_state in media_devices.keys() }}
  - service: input_boolean.turn_off
    data_template:
      entity_id: >
        {% set media_devices =
        {
          'PowerOff': 'poweroff',
          'Televisie': 'television',
          'Mediaplayer': 'mediaplayer',
          'Apple-TV': 'appletv',
          'Radio':, 'radio',
          'Muziek': 'music',
          'DVD': 'dvd',
          'Teletekst': 'teletext',
          'Smart-TV': 'smarttv'
        }
        %}
      input_boolean.{{ media_devices[old_state] }}

So, it appears as if you are trying to use toggles to switch your harmony activities. There’s a much better way to do this without scripts, switch templates. Check out the switch template section in this post:

Basically the switches will only be on when the activity is on. You don’t have to worry about toggling other input_booleans off when you turn on another activity.

EDIT: I also want to clarify. The reason your template isn’t working is because you are attempting to use a list of tuples to get your information. You need to use a dictionary. Notice the syntax change for media_devices. That is the proper way to make a dictionary in jinja. The way you were writing it was a list of tuples ().

Hi @petro: Thanks for the fast response and for sharing the link to your solution. Indeed the intention was to toggle a few input_booleans, where there can only be one ‘on’ for - or better representing - the remotes’ state. In the lovelace UI screen I want to see this:
image
I want to use the buttons as toggles. Switching to another state should change the buttons as well as the remote itself. Is this also something I can do with your setup?

As a background: I have all my devices (except the remote obviously) switched off with an external mains switch. When I switch everything on (with hass in this case) all devices get powered and after 30 seconds or so the actual remote command is issued. This allowes my devices to power up before setting the correct modes with the harmony.

Thanks

Ralph

Yes, you just don’t need to integrate all the other crap i did. Only the switch template sections. Each switch will update it’s state independently of the service call. So when 1 is on, the others will silently turn off without you doing anything.

@petro This is working, so thanks for that. However it does not do all I want. The thing is that the commands are instant. So the moment I hit the button on the UI, the harmony responds and sets the correct mode. However when I start from my starting position, I need the harmony to wait until everthing is on. So my flow should be (with in this example Television):
switch Television on via UI >> switch mains power on >> wait 30 secs >> switch harmony to Television mode.
Bottomline: in need an extra step to accomodate the 30 seconds. At the same time the UI button should change directly and not after 30 seconds to give feedback of the action.

Any tips?

Thx

Ralph

For the initial anser:

This line results in an error when validating:

Error loading /config/configuration.yaml: while scanning a simple key
in “/config/packages/package_media_center.yaml”, line 332, column 7
could not find expected ‘:’
in “/config/packages/package_media_center.yaml”, line 334, column 1

If that’s the case just build a delay into your turn on sequence. You’ll never get instant feed back from the harmony to the UI based on how it’s designed. I have about a 8 second delay from when I power on to when the UI changes. This is because the harmony has a slow polling rate and it just can’t be updated.

Just indent it 2 times. Looks like I had wrong indentation in my post.


also as a word of advice. I went down the road you are currently going about 2 years ago. In the end no matter how the code was formatted, there was always a feedback loop because of how the harmony works. The stablest approach was the switch templates. You’ll spend so much time trying to get the input_booleans to be ‘instant’ and you’ll just end up with things cycling at times. You’ll spend most of your time dealing with the off sequence because the off sequence is treated as an activity. And all input_booleans will call power off, but what happens when you want to switch from activity A to activity B. How can you suppress the power off? Good luck.

@petro I basically agree with you dealing with this is difficult. I had the same thing with Domoticz )my old HA). Solved it there. However hass operates differently. I will give it some thinking.

Thx again for thinking along with me. Much appreciated!

Hi All,

After a lot of trial and error, finally I found the solution for my problem. Thx to @petro fo the head start.
So her is the code that works for me:
script:
media_mode:
alias: Set media mode script

sequence:
  - service: input_boolean.turn_off
    data_template:
      entity_id: >-
        {% set media_devices = 
          {
          "PowerOff":"poweroff",
          "Televisie":"television",
          "Mediaplayer":"mediaplayer",
          "Apple-TV":"appletv",
          "Radio":"radio",
          "Muziek":"music",
          "DVD":"dvd",
          "Teletekst":"teletext",
          "Smart-TV":"smarttv"
          } 
        %}
        {% set entity = ["input_boolean",media_devices[old_state]]|join(".") %}
        {{ entity }} 

What is does is switch of a button after another button was selected. {{ old_state }} is used as a parameter passed to the script.
A word of warning: I had a lot lot of errors typing the code. The code was 100% accurate, but hass flagged an error at validating. Even for a simple line like {{ entity }}. Retyping solved that issues. Probably something with spaces and returns.

Ralph .