Passing variable to a script

Hello, I have one automation which has a trigger with some event from esphome integration. When that event occurs, in my action part Im calling a script which is setting up the tv and gets as input arg a string should open netflix, yt, or some other app.
Here is my automation:

- alias: Bedroom TV toggle via encoder
  trigger:
    platform: event
    event_type: esphome.bedroom_entrance
    event_data:
      type: single_short_click_3
  action:
    - service: script.turn_on
      entity_id: script.bedroom_tv
      data:
        tvsource: "Netflix"

and the script:

alias: Prepare Bedroom TV
icon: "mdi:party-popper"
description: 'Turn on TV and set source'
fields:
  tvsource:
    description: Particular application (eon, yt, netflix..)
    example: Netflix
sequence:
  - service: media_player.turn_on
    entity_id: media_player.bedroom_tv

  - service: media_player.select_source
    data_template:
      entity_id: media_player.bedroom_tv
      source: "{{ tvsource }}"

Im using webos component.

but for some reason Im getting the “standard” error that extra keys are not allowed @data[‘tvsource’]. Why?

Something is confused there because the script isn’t the one being called from the automation. You’re calling script.bedroom_tv, but the second one will be called script.prepare_bedroom_tv.

Also, you should change data_template: to data:.

nope, the naming is correct. I agree for data_template => data… I just was testing with data_template in desperate manner.

There’s nothing in the script that would cause it to be named script.bedroom_tv.

Yes it is :slight_smile:

so, script naming comes from name of the each file in my scripts folder. Its ok, believe me :slight_smile:

Ah, that was the missing piece. And for your issue, you need a variables: block, as shown in the docs: https://www.home-assistant.io/integrations/script/#passing-variables-to-scripts. This works fine:

  test:
    sequence:
      - service: script.turn_on
        entity_id: script.notify_iphone
        data:
          variables:
            message: test
            title: test

thats it. I was missing the variables part :slight_smile: thanks!