How to pass an entity_id as an argument to a script

Hi. Newbie here, (my second post). I have been configuring HA (Hassio on Ubuntu) for about a month and so far it has been great. I’ve now run into a problem I can’t seem to figure out.

I’d like to refactor a few scripts that I reuse down to one by passing an entity_id.

I set up a test so that I can get the syntax right before I make changes to my prod environment however I can’t pass an entity_id to a script from an automation.

I am getting the following error “Error executing script. Invalid data for call_service at pos 1: extra keys not allowed @ data[‘entity’]”. I’ve looked through the forum, the question has been asked before:

I’ve tried several options but nothing has worked; at my wits end now so any suggestions are welcomed (sorry for the long post). My code is:

Automation -

- id: '7'
  alias: New Automation
  description: ''
  trigger:
  - entity_id: switch.lamp_tv
    platform: state
    to: 'on'
  condition: []
  action:
  - service: script.test_arguments
    data: 
      entity: "{{ switch.lamp_tv }}"

Script -

test_arguments:
  alias: test_arg
  sequence:
  - entity_id: "{{ entity }}"
    service: homeassistant.turn_off

You will need a data template in your script. Variables can’t be used outside of the data template

This should work I believe:

- id: '7'
  alias: New Automation
  description: ''
  trigger:
  - entity_id: switch.lamp_tv
    platform: state
    to: 'on'
  condition: []
  action:
  - service: script.test_arguments
    data: 
      entity: switch.lamp_tv
test_arguments:
  alias: test_arg
  sequence:
  - data_template:
      entity_id: "{{ entity }}"
1 Like

Thanks for the suggestion Pippyn. I tried your code and unfortunately got the same error message. I also tried curly braces and quotes around the entity data variable in the automation and same result. It seems to me that I am missing some really simple rule or syntax, I’ll keep reading and trying. :thinking:
cheers
Nick

I’d advise to read the HA documentation first - have you checked this?

Your issue is the script - all data/data_template make sense with a service call but you don’t have one. That’s how it should be:

- id: '7'
  alias: New Automation
  description: ''
  trigger:
  - entity_id: switch.lamp_tv
    platform: state
    to: 'on'
  action:
  - service: script.test_arguments
    data: 
      entity: switch.lamp_tv

and the script

test_arguments:
  alias: test_arg
  sequence:
    - service: homeassistant.turn_off
      data_template:
        entity_id: "{{ entity }}"
    
3 Likes

Hi Ahmad

Thanks very much for the advice. I’ve read the page on scripts. I am using the “calling the script as a service directly” method. I had a service call in the script but in a different sequence to your post. I’ve modified my code accordingly. However, I got the same error message on execution:

Logger: homeassistant.components.automation
Source: helpers/script.py:693
Integration: Automation (documentation, issues)
First occurred: 3:36:52 PM (16 occurrences)
Last logged: 8:53:39 PM

New Automation: Error executing script. Invalid data for call_service at pos 1: extra keys not allowed @ data[‘entity’]

Reading the docs, your code should work so I thought that there is something else going on. I loaded my code into the VS Code editor to see if I received any errors. I also rewrote the code to ensure that there were no hidden chars. Still the same result. So I then rebooted Ubuntu/Hassio and tried again, and it worked!!! My code is below for reference.

Thanks for the advice once again.

Nick

Automation

- id: '7'
  alias: New Automation
  description: ''
  trigger:
  - entity_id: switch.lamp_tv
    platform: state
    to: 'on'
  action:
  - service: script.test_arguments
    data: 
      entity: switch.lamp_tv

Script

test_arguments:
  alias: test_arg
  sequence:
    - service: homeassistant.turn_off
      data_template:
        entity_id: "{{ entity }}"

well, after changing your automation and script did you reload them by going to Configuration → Server controls and clicking on “Reload automations” and “Reload scripts”?
you should do that so HA pick up the changes.
you only need to restart HA (just HA) if you change something with your sensors and other non-reloadable stuff (as HA devs keep adding .reload services to integrations).

please mark the solution post in this topic so others can easily find it later.

Solution post is marked. I didn’t know that automations and scripts needed a restart :slightly_frowning_face: I do now, thanks.

cheers
Nick

I though my post was a solution… but as a topic starter you are a decision maker :wink:

I’m not fussed Ahmad. Changed it to your post.

1 Like

me, too :wink: