Automation: Different service calls with different data keys depending on a condition

Hello all. I need your help! I want to create an automation that calls, depending on the trigger state, different service calls.
Basically, I want to make use of the scanned_tag automation for media playback with the addition to media_player.select_source for radio playback.
The problem: media_player.select_source (data key source) needs other data keys than media_player.play_media (data keys media_content_id, media_content_type).
I tried several solution and already realized, that data_template does not work. The last try leads to the error “extra keys are not allowed” (which makes sense).


- id: handle_tag_scan
  alias: Handle Tag Scan
  mode: single
  max_exceeded: silent
  variables:
    media_players:
      db2adc00c4b0003165cf5556f5c284ea: media_player.kueche
    tags:
      5F:A4:9E:68:
        media_content_id: spotify:playlist:1OGshruN5ORY6dA4aisiul
        media_content_type: playlist
      40:B1:8C:CF:
        media_content_id: spotify:playlist:3cdknj5Xwy39Hv75Zh1nFx
        media_content_type: playlist
      4B:B5:F6:46:
        media_content_id: spotify:playlist:3cdknj5Xwy39Hv75Zh1nFx
        media_content_type: playlist
      71:3F:11:86:
        media_content_id: WDR 2
        media_content_type: radio
  trigger:
    platform: event
    event_type: tag_scanned
  condition:
  - '{{ trigger.event.data.tag_id in tags }}'
  - '{{ trigger.event.data.device_id in media_players }}'
  action:
  - variables:
      media_player_entity_id: '{{ media_players[trigger.event.data.device_id] }}'
      media_content_id: '{{ tags[trigger.event.data.tag_id].media_content_id }}'
      media_content_type: '{{ tags[trigger.event.data.tag_id].media_content_type }}'
  - service: '{% if media_content_type == "radio" %}media_player.select_source{% else %}media_player.play_media{% endif %}'
    target:
      entity_id: '{{ media_player_entity_id }}'
    data:
      source: '{% if media_content_type == "radio" %}{{ media_content_id }}{% endif %}'
      media_content_id: '{% if media_content_type != "radio" %}{{ media_content_id }}{% endif %}'
      media_content_type: '{% if media_content_type != "radio" %}{{ media_content_type }}{% endif %}'
  - delay: 2

Use choose to determine which service call should be used.

- id: handle_tag_scan
  alias: Handle Tag Scan
  mode: single
  max_exceeded: silent
  variables:
    media_players:
      db2adc00c4b0003165cf5556f5c284ea: media_player.kueche
    tags:
      5F:A4:9E:68:
        media_content_id: 'spotify:playlist:1OGshruN5ORY6dA4aisiul'
        media_content_type: playlist
      40:B1:8C:CF:
        media_content_id: 'spotify:playlist:3cdknj5Xwy39Hv75Zh1nFx'
        media_content_type: playlist
      4B:B5:F6:46:
        media_content_id: 'spotify:playlist:3cdknj5Xwy39Hv75Zh1nFx'
        media_content_type: playlist
      71:3F:11:86:
        media_content_id: 'WDR 2'
        media_content_type: radio
  trigger:
    platform: event
    event_type: tag_scanned
  condition:
  - '{{ trigger.event.data.tag_id in tags }}'
  - '{{ trigger.event.data.device_id in media_players }}'
  action:
  - variables:
      media_player_entity_id: '{{ media_players[trigger.event.data.device_id] }}'
      media_content_id: '{{ tags[trigger.event.data.tag_id].media_content_id }}'
      media_content_type: '{{ tags[trigger.event.data.tag_id].media_content_type }}'
  - choose:
    - conditions: "{{ media_content_type == 'radio' }}"
      sequence:
      - service: media_player.select_source
        target:
          entity_id: '{{ media_player_entity_id }}'
        data:
          source: '{{ media_content_id }}'
    default:
    - service: media_player.play_media
      target:
        entity_id: '{{ media_player_entity_id }}'
      data:
        media_content_id: '{{ media_content_id }}'
        media_content_type: '{{ media_content_type }}'
  - delay: 2

1 Like

Thanks for the quick and precise help! I don’t know how I missed that command.

1 Like