Two almost identical scripts: one works, one not

Hi there. I’m new here. Exciting! :slight_smile:

I have two scripts to change the TV volume through Harmony Hub:

  1. This one is working with fixed button presses:
tv_much_quieter:
  alias: TV viel leiser
  sequence:
  - data_template:
      device: '36473354'
      command: ['VolumeDown', 'VolumeDown']
      entity_id: remote.wohnzimmer
    service: remote.send_command
  1. This one with templating and variables not:
tv_n_times_quieter:
  alias: TV n mal leiser
  sequence:
  - data_template:
      device: '36473354'
      command: "['VolumeDown'{% for n in range(times - 1) %}, 'VolumeDown'{% endfor %}]"
      entity_id: remote.wohnzimmer
    service: remote.send_command

I enabled logging to check what is going on. It looks the same to me?!?
From script 1 (working one):
2018-02-08 19:28:08 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=remote, service=send_command, service_data=device=36473354, command=['VolumeDown', 'VolumeDown'], entity_id=remote.wohnzimmer, service_call_id=139807374105568-74>

From script 2 (not working):
2018-02-08 19:40:29 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=remote, service=send_command, service_data=device=36473354, command=['VolumeDown', 'VolumeDown'], entity_id=remote.wohnzimmer, service_call_id=139807374105568-82>

There is more output in the log but it also looks the same. Any idea what happens here? What am I missing?

Thanks!

Hello and welcome to our community :slight_smile:

I don’t use Remote Harmony component or Harmony remote for that matter, but looking at the documentation does not mention working with template as commands.

Ok, I thought templating to be something generic? Like it first renders the template and then processes it. That would not have to be supported then by the implementation of the service because it happens before. Wouldn’t it?

Ran it in the templates and this code worked (substituting your variable):

"['VolumeDown'{% for n in range(4 - 1) %}, 'VolumeDown'{% endfor %}]"

Returned:
"['VolumeDown', 'VolumeDown', 'VolumeDown', 'VolumeDown']"

But is it actually a list, or a string?

I’m having the same issues here

The template editor isn’t very helpful because it could look like a list, but is actually a string. In fact, what you even showed, is quoted.

It is a list. It is quoted so that the templating engine knows to process it.At least this is how I understand it.

Why don’t you just use the repeat command:

tv_n_times_quieter:
  alias: TV n mal leiser
  sequence:
  - data_template:
      device: '36473354'
      command: 'VolumeDown'
      num_repeats: 5
      entity_id: remote.wohnzimmer
    service: remote.send_command

Documentation covers num_repeats: https://home-assistant.io/components/remote.harmony/

Then just use that as your value_template

1 Like

Also, as an FYI, this should work in any template {{ [ ‘VolumeDown’ ] * times }}

example:

{{ [ ‘VolumeDown’ ] * 6 }}

returns:

[‘VolumeDown’, ‘VolumeDown’, ‘VolumeDown’, ‘VolumeDown’, ‘VolumeDown’, ‘VolumeDown’]

1 Like

Wow! Thanks reading the docs for me… This works! Thanks a lot!

Thanks, but didn’t get this working. But never mind. The other reply worked. Thanks again and good night.

So the answer is (I found out), you can’t return lists from a template. They only return strings. So this just isn’t possible.

Where did you find that answer? I’ve been trying to help people with returning lists for the past few weeks. Every attempt fails. I haven’t found it in writing, but I had that hunch. Do you have a link?

Yeah, I shouldn’t take credit… Someone pointed it out to me awhile back too.

I talked to @balloob and he informed me that templates always return strings.

1 Like

Oh. Thanks for the update. That’s unfortunate…