Template in Action

I try to use a template value in a automation (action), but I do something wrong??

Action is:

entity_id: media_player.kodi
media_content_id: |
  "{{ state_attr('media_player.kodi', 'media_content_id') + 1 | int }}"
media_content_type: CHANNEL

When I test the template in the template-editor it shows me the right channel number.
When I test the service whit a number instead of the template it works also.

The log shows the following error:

"ValueError: invalid literal for int() with base 10: '"{{ state_attr(\'media_player.kodi\', \'media_content_id\') + 1 | int }}"\n'"

I tried alos with data_template and service_template, but always the same error.

What do I have to chance to use the template in a action??
Thanks

Try flipping the +1 and | int. You can’t add 1 to a string.

Good point, it seems logical.
But unfortunately it don’t fix the problem.
Still the same error in the log:

ValueError: invalid literal for int() with base 10: '"{{ state_attr(\'media_player.kodi\', \'media_content_id\') | int + 1 }}"\n'

Thanks

entity_id: media_player.kodi
media_content_id: >
  {{ state_attr('media_player.kodi', 'media_content_id') | int + 1 }}
media_content_type: CHANNEL

EDIT
You have this:

media_content_id: |

What do you believe that vertical bar | does there?

What is the value of the media_content_id attribute? It seems it’s not a decimal value. Could it be hex?

If you use > you don’t use " ". One or the other. Quotes are for single line. > is for multi line.

@123 Taras
I use the frontend editor to write the automation, and when I save and reopen it, the “>” is replaced by “|”. So I believe its the same.

@pnbruckner
It is a number, so I guess it is an integer.

@micque
As you say, editor accepts both versions. But > is always replaced by |. And it don’t work with niter syntax, error is always the same.

Do I have to use a value_template instead, mostly templates are used as a condition, and not as a action?

Thanks
Thanks

Ah, ok, I see it now. @micque is correct. You need to remove the outside quotes.

EDIT: BTW, in the future, please post the entire automation, not just a snippet. Without the whole thing we’re forced to guess the context, and the part you didn’t post might be the problem, or at least might make it easier to interpret the little you did post.

I tried it without quotes, no success.

You are right, following is the hole automation:

- id: '1573423951624'
  alias: ManChannelPlus
  description: ''
  trigger:
  - event_data:
      event: 1001
      id: button01
    event_type: deconz_event
    platform: event
  condition: []
  action:
  - alias: ''
    data:
      entity_id: media_player.kodi
      media_content_id: "{{ state_attr('media_player.kodi', 'media_content_id') | int + 1 }}"
      media_content_type: CHANNEL
    service: media_player.play_media

Thanks

See the example I provided above.

  • If the template is NOT on the same line as the option then > is used and the template is NOT delimited with quotes.
  • If the template IS on the same line as the option then > is NOT needed and the template MUST be delimited with quotes.

Random thoughts:
The Automation Editor, for all its good intentions, often leads to user-support issues. It can’t produce all the code that Home Assistant is capable of understanding and what code it does produce often doesn’t look anything like what is shown in examples:

  • alphabetically sorted options (good for robots, not humans)
  • doubled single-quotes (because, no idea)
  • vertical bar to indicate line continuation (that’s a new one for me)
1 Like

You need to replace data: with data_template:.

It’s just another option for multiline YAML.

… that’s not shown in any examples found in the documentation nor in the majority of what’s shared in the forum. The Automation Editor ought to speak street-YAML (not the ''dialect'' it uses).

@pnbruckner
I chanced, but it don’t work.

- id: '1573423951624'
  alias: ManChannelPlus
  description: ''
  trigger:
  - event_data:
      event: 1001
      id: button01
    event_type: deconz_event
    platform: event
  condition: []
  action:
  - alias: ''
    data_template:
      entity_id: media_player.kodi
      media_content_id: "{{ state_attr('media_player.kodi', 'media_content_id') | int + 1 }}"
      media_content_type: CHANNEL
    service: media_player.play_media

I made the chances in the file, and now I can not open it in frontend editor.

Is the data_template just for the template line?

Thanks

As many have said, the frontend editor is not fully featured yet. And this seems to be one of the main limitations – i.e., you can’t use data_template in the editor.

In a service call, templates will not be rendered if you don’t use data_template. So, you have two choices, at least for now: use the frontend editor but not templates, or hand edit YAML files so that you can use templates.

Many thanks for the help, I had no chance without all of you!

I edited the file, and after a reboot every thing work like it should.

 id: '1573423951624'
  alias: ManChannelPlus
  description: ''
  trigger:
  - event_data:
      event: 1001
      id: button01
    event_type: deconz_event
    platform: event
  condition: []
  action:
  - alias: ''
    data_template:
      entity_id: media_player.kodi
      media_content_id: >
        {{ state_attr('media_player.kodi', 'media_content_id') | int + 1 }}
      media_content_type: CHANNEL
    service: media_player.play_media

Thanks again!

1 Like

For future reference, it’s the custom of this community forum to tag the post containing the answer to your question as the ‘Solution’. In other words, whichever post helped to solve the original problem is the solution. The original problem was to fix a template containing syntax errors.