Value should be string for a dictionary value @data['source']

Hi,

I have a script that when someone rings the front door bell, if my TV is on, I get the live footage of the frontdoor in my TV. After a timeout, it will switch source back to ‘whatever it was’ and pause the broadcast (since my TV resumes it by default when the source is changed). I am setting an input_text that holds the last_source before it changes to the live cam. The script works but it does not change the source back to “Netflix, YouTube, etc.”, it pauses the live footage instead so I can tell that it reached to the end.

When I check {{ states(‘input_text.last_source’) }} in developer tools > template after running the script, I see that the value is indeed the last boradcast service I was watching. However, when I call the service below in developer tools > services;

  - service: media_player.select_source
    data_template:
      source: input_text.last_source
    target:
      entity_id: media_player.living_room_tv

I get an error that says value should be string for a dictionary value @data[‘source’]. I’ve tried different combinations with data:, data_template: but none of them seem to be working. When I put “Netflix” in the source and call it, it switches to Netflix as expected. So, I guess, I cannot pass the variable input_text.last_source to the media_player.select_source as string. I thought calling the “input_text” value in the source of media_player.select_source would be enough because the value itself is already a string.

Here is my script. What am I missing here?

alias: show frontdoor live in the TV
sequence:
  - service: input_text.set_value
    data:
      value: '{{ state_attr(''media_player.living_room_tv'', ''source'') }}'
    target:
      entity_id: input_text.last_source
  - service: camera.play_stream
    data:
      media_player: media_player.chromecastultra
    entity_id: camera.frontdoorcam
  - wait_for_trigger:
      - type: not_opened
        platform: device
        device_id: d5ceadbb0db67454bd03f5ce1c703af6
        entity_id: binary_sensor.openclose_3
        domain: binary_sensor
    continue_on_timeout: true
    timeout: '10'
  - service: media_player.select_source
    data_template:
      source: input_text.last_source
    target:
      entity_id: media_player.living_room_tv
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - service: media_player.media_pause
    target:
      entity_id: media_player.living_room_tv
mode: single

Several versions ago, data_template was deprecated in favor of simply data but that’s not the cause of the problem. The issue is that you are not passing the state value of input_text.last_source to the source option.

      source: input_text.last_source #<---- this is incorrect

Try this:

  - service: media_player.select_source
    data:
      source: "{{ states('input_text.last_source') }}"
    target:
      entity_id: media_player.living_room_tv

@123 Thanks for your reply.

I actually have tried that one as well, nothing happened. “{{ variable }}” worked for many other automations for me already but for the input_text specific, I guess, when you put the value between quotes, it passes the value between the quotes, {{ states(‘input_text.last_source’) }} in this case, not Netflix which is the real value when I check the templates.

I tried your solution in developer tools> service anyways but the TV did not react at all.

Interesting. It seems to work for others:

okay, I figured the scirpt was already working with source: “{{ states(‘input_text.last_source’) }}”. I was deceived by the services tool in the developer tools because for some reason I cannot call the same structure in there.

The latest version of Home Assistant (currently 2021.3.X) supports the use of templates in Developer Tools > Services. Older versions do not.