Pass Variable from Automation to Script Not Working

I have an automation that reads NFC cards and then based on the card scanned kicks off on of three different scripts.

The relevant parts of the automation:

Defining the variables for each of the NFC cards
``

  • id: DancePartyA
    platform: tag
    tag_id: 04-8B-CF-36-B8-2A-81
    variables:
    c_type: audio
    pl_type: Kids Rock! - Rock Music for Children
    m_type: null
    l_type: DanceParty
    ``

the c_type varian;e identifies the branch to execute.

Here’s the conditional logic

``
action:

  • service: persistent_notification.create
    data:
    title: Debug Notification
    message: “Content Type: {{ trigger.c_type }}”
  • choose:
    • conditions:
      • condition: template
        value_template: “{{trigger.c_type == audio }}”
        sequence:
      • service: script.1692643185235
        data:
        playlist: “{{trigger.variables.pl_type}}”
    • conditions:
      • condition: template
        value_template: “{{trigger.c_type == video_s }}”
        sequence:
      • service: script.1692643378131
        data:
        media: “{{trigger.variables.m_type}}”
    • conditions:
      • condition: template
        value_template: “{{trigger.variables.c_type == video_p}}”
        sequence:
      • service: script.1692643035619
        data:
        media: “{{trigger.variables.m_type}}”
        playlist: “{{trigger.variables.pl_type}}”
        mode: single
        ``

I know the conditional logic is working and calling the right script via trace.

In the case of c_type equal to audio the following script is called:

``
alias: PlaySpotifyPL
sequence:

  • service: mass.queue_command
    data:
    command: play_media
    enqueue_mode: play
    uri: “{{playlist}}”
    player_id: media_player.wcast_tv_3
    mode: single
    ``

My understanding is the the portion of the automation that calls the script"
sequence: - service: script.1692643185235 data: playlist: "{{trigger.variables.pl_type}}"
should pass the value of the variable from the NFC card to the script but I’m getting null passed

Any ideas

Please format you code properly using 3 backticks above and below the code block so that we can see if there are any format issues.

trigger.c_type and trigger.variables.c_type are incorrect. Your trigger variables are not appended to the trigger state object, they are their own variables… just use c_type, pl_type, etc.

Also == video_p is incorrect unless you have another variable called video_p, otherwise you need to use the string value i.e. {{ c_type == 'video_p'}}

Not sure I am understanding

In the section of code that defines the variables associated with each NFC tag I have;

  - id: GranA
    platform: tag
    tag_id: 04-E4-A5-37-B8-2A-81
    variables:
      c_type: audio
      pl_type: Gran
      m_type: null
      l_type: Gran

In the condition section I have :slight_smile:

action:
  - service: persistent_notification.create
    data:
      title: Debug Notification
      message: "Content Type: {{ trigger.c_type }}"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{trigger.c_type == audio }}"
        sequence:
          - service: script.1692643185235
            data:
              playlist: pl_type
      - conditions:
          - condition: template
            value_template: "{{trigger.c_type == video_s }}"
        sequence:
          - service: script.1692643378131
            data:
              media: "{{trigger.variables.m_type}}"
      - conditions:
          - condition: template
            value_template: "{{trigger.variables.c_type == video_p}}"
        sequence:
          - service: script.1692643035619
            data:
              media: "{{trigger.variables.m_type}}"
              playlist: "{{trigger.variables.pl_type}}"
mode: single

The c.type variable is used in the switch and that part is working. In the test case the tag is called “Gran” and the assocated c_type is audio and the associated pl_type is “Gran”. When the above calls the script assocaited with audio I am trying to pass the value of the variable pl_type (ie Gran) to the script.

On the script side I have

alias: PlaySpotifyPL
sequence:
  - service: mass.queue_command
    data:
      command: play_media
      enqueue_mode: play
      uri: playlist
      player_id: media_player.wcast_tv_3
mode: single

the uri requires a string value for the playlist. When I use pl_type in the automation what is showing up is just the string “pl_type”. I’ve tried “{{playlist}}”, {{playlist}} and playlist on the script side and none of them seem to resolve to the value “Gran”.

You have not shared anything involving a switch, so I cannot comment on that. However, all of the attempted methods you are using in the shared portion of your automation are incorrect. You can use the following corrected Option definition as an example of how to correct the other Options and service calls:

...
      - conditions:
          - condition: template
            value_template: "{{ c_type == 'audio' }}"
        sequence:
          - service: script.1692643185235
            data:
              playlist: "{{pl_type}}"
....

Just like the service call above needs curly braces around pl_type to mark it as a variable, the receiving script needs to have the variable’s key inside curly braces so that the program knows it’s a variable not just a string of text.

alias: PlaySpotifyPL
sequence:
  - service: mass.queue_command
    data:
      command: play_media
      enqueue_mode: play
      uri: "{{playlist}}"
      player_id: media_player.wcast_tv_3
mode: single

“Hot damn”!!! that worked. Thank you very much. I’ve been struggling with this for days.