Passing variables to a script from an automation

Hey folks, thanks in advance for your help. :pray:t2::beer:

I’ve read through the script syntax here, but I can’t see where I’m going wrong.

I’m trying to send a notification that will appear on my TV.
To do it, I’m using most of this script … this bit works great.
I see the notification on the TV without a problem.

# ===============================================================
# Send a Notification to the Lounge TV
# ===============================================================
 
 notification_to_lounge_tv:
   alias: "Send a Notification to the Lounge TV"
   mode: restart
   sequence:

    - choose:
        - conditions:
            - condition: state
              entity_id: media_player.lounge_tv
              state: "on"
          sequence:
            - service: notify.lounge_tv
              data:
                message: "This is a test message"
                title: "TITLE"

However, what I really want to do is pass the message to it as a variable when its called from an automation. So in this case, I’m using this script which is almost the same …

# ===============================================================
# Send a Notification to the Lounge TV
# ===============================================================
 
 notification_to_lounge_tv:
   alias: "Send a Notification to the Lounge TV"
   mode: restart
   sequence:

    - choose:
        - conditions:
            - condition: state
              entity_id: media_player.lounge_tv
              state: "on"
          sequence:
            - service: notify.lounge_tv
              data:
                message: "{{ message }}"
                title: "{{ title }}"

and for the automation …

# ===============================================================
# Person detected in the Front Garden
# ===============================================================

- id: '5C43859D-5396-41E2-A93D-71F1020FD0BF'
  mode: single
  alias: "Person detected"

  trigger:
  - platform: state
    entity_id: sensor.front_garden_camera_detected_object
    to: person

  action:
  - service: script.notification_to_lounge_tv
    data: 
      variables:
        title: "TITLE"
        message: "Person Detected"
  - service: notify.mobile_app_iphone
    data:
      message: "Person Detected"

In this case, I get the notification on the iPhone, so I know the automation is firing, but I’m not getting the notification on the TV.

Clearly I’m missing a trick, but I just can’t see it. There are no errors in my logs.

Thanks in advance!

If you call the script as a service directly, instead of calling the script.turn_on service on the script, you do not need to use the variables configuration…

- id: '5C43859D-5396-41E2-A93D-71F1020FD0BF'
  mode: single
  alias: "Person detected"
  trigger:
  - platform: state
    entity_id: sensor.front_garden_camera_detected_object
    to: person
  action:
  - service: script.notification_to_lounge_tv
    data: 
      title: "TITLE"
      message: "Person Detected"
  - service: notify.mobile_app_iphone
    data:
      message: "Person Detected"
1 Like

Ohhhhhhhhhhhh. Knew it would be something simple that I overlooked.
Thank you very much @Didgeridrew :beer:

This proposed solution fine for this specific use-case, but I would still like to know how to pass a variable to a script, which is called from a service.

For example, I can call this script, but attempt to pass the message field don’t work:

service: notify.alexa_one
data:
  message: Message from H.A. "{{message}}"

What you have posted is a service call to what looks like a custom notifier, not a complete script… if you want help, it’s much easier for us if you post actual, complete scripts or automations, including custom component configuration where applicable.

Using the following works fine for me:

#script
alias: Test script variable
sequence:
  - service: notify.alexa_media_kitchen
    data:
      message: |
        Message from Home Assistant  {{ message }}
      data:
        type: tts
mode: single
# service call to use in another script or automation
service: script.test_script_variable
data:
  message: This is a test

#OR

service: script.turn_on
data:
  variables:
    message: Just testing stuff
target:
  entity_id: script.test_script_variable

OP’s issue was that they had made a mash-up of the two service call types, using the variable configuration from the script.turn_on (i.e. indirect) service while using the direct service call method.

2 Likes

I am trying to do something similar to those above and, if I pass in a string to the script then it does in fact work. My problem is that when instead I try to pass a trigger variable from the automation to the script the script executes with a null value in the field.
In the automation the triggers are all calendar events. This works:

service: script.send_template_notification
data:
  notification_message: meeting

This also works:

service: script.send_template_notification
data:
  notification_message: "{{ states('input_text.somerandomtexthelper') }}"

This also does execute the notification service call but the message displayed is empty:

service: script.send_template_notification
data:
  notification_message: "{{ trigger.calendar_event.summary }}"

The script looks like this:

device_id: 593a9732a29993e7a593f65200c30bae
domain: mobile_app
type: notify
message: "{{ notification_message }}"
title: "{{ notification_type }}"

That is not a script… it may be part of a script, but you aren’t doing yourself any favors by posting partial configurations.

From the looks of it you are trying to use templates in a Device action, which is not supported. Instead you should use a Call Service action.

Thanks for the feedback and your tip solved my problem!

i am also trying to figure out how to do this…
my scirpt looks like this

alias: onkyo_command
sequence:
  - service: shell_command.onkyo_command
    data:
      cmd: {{onkyo_cmd}}

and i have a basic button to test it:

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: script.onkyo_command
  data:
    onkyo_cmd: listening-mode=direct
name: test

but when i click on the button nothing happens…

if i hardcode this in the script then it works just fine:

alias: onkyo_command
sequence:
  - service: shell_command.onkyo_command
    data:
      cmd: listening-mode=direct

You have a typo in your code. :slight_smile:

Shouldn’t that be onkyo_cmd?

i actually only had the typo in the post here sorry, i was editing things around, i have it without typo in home assistant and it doesn’t work.

edited my initial post to have actual copy/paste now

EDIT: i think i figured it out, my problem is with the “=” in the value… i am guessing it needs to be escaped somehow? removing that everything works fine