Can I use state_attr() in a template for script service data?

I use this in my script (with UI, but also in code it fails), for service data

source: '{{ state_attr(''media_player.pioneer'', ''source'') }}'

But it always fails, “unknown input”
Am I doing something wrong, or is it really only possible to use objects like input_text in the templates in scripts ?

Yes, but for what service data?

The quote usage is kinda wonky here. You have to either swap the quote type, or escape them. Try this:

source: "{{ state_attr('media_player.pioneer', 'source') }}"

Though, I think the double single quote counts as escaping it and it’s just how the UI mode does things. So probably not the problem.

Are you sure that attribute exists at the time of the call? A lot of media players are polling devices and the attributes you expect to be there will be delayed.

Open up Developer Tools in your browser, go to states, and find ‘media_player.pioneer’. In the attributes field, look at ‘source’. Does it match what you expect? Try manually doing whatever you are trying to do and see how long it takes that value to update.

Also, now that we no longer use data_template your templated options must be in a data block. Like this:

service: media_player.some_service
data:
  source: "{{ etc...

Not like this:

service: media_player.some_service
source: "{{ etc...
1 Like

Thanks Jocnnor & Tom ! Well, those quotes were automatically changed by the script engine.

It has a value, I want to change the source of another media_player to the source of the pioneer. The sources have the same name. When I try this in the template editor, the output is ok.

But it seems I can’t use it in the script editor. I’ve also used in a data block, but no luck !

It might help if you post the entire script.

pioneer_samesource:
  alias: Pioneer_samesource
  mode: single
  sequence:
  - data: {}
    entity_id: media_player.pioneer
    service: media_player.turn_on
  - data:
      source: '{{ state_attr(''media_player.receiver2'', ''source'') }}'
    entity_id: media_player.pioneer
    service: media_player.select_source

So that looks ok, other than it being poorly ordered by the UI for human consumption.

Put this in the developer tools template editor:

{{ state_attr(''media_player.receiver2'', ''source'') }}

See if the result matches what is available as sources for the pioneer player in the developer tools states menu. Including capitalisation - that’s important.

If it all checks out try putting the entity id in the data block too:

pioneer_samesource:
  alias: Pioneer_samesource
  mode: single
  sequence:
  - data: {}
    entity_id: media_player.pioneer
    service: media_player.turn_on
  - data:
      source: '{{ state_attr(''media_player.receiver2'', ''source'') }}'
      entity_id: media_player.pioneer
    service: media_player.select_source

If none of that works you might need a slight delay between the turn_on action and the source set action.

1 Like

yep that would be my thought too, personally using wait_templates for those actions:

wait_template: >
  {{is_state('media_player.pioneer','on')}}

Thanks for your help ! It seemed to be a problem with my HA version (was 0.114 because of compatibility problems with my devices from 0.115)

Updated today to 0.118 and script is working !

FYI: in 0.114 it should have been like this:

  - data_template:
      source: '{{ state_attr(''media_player.receiver2'', ''source'') }}'
    entity_id: media_player.pioneer
    service: media_player.select_source

The depreciation of data_template: in favour of jut data: happened in 0.115.

1 Like