Calling scripts with data template

Following this guide, I am trying to call a script and pass it variables.

I’m calling it with:

    - service: script.notify
      data:
        speaker: 'media_player.kitchen_speaker'
        message: 'Xmas lights have been turned off'

The script is as below:

 notify:
    alias: "Notifications"
    fields:
      speaker:
      message:
    sequence:
      - service: tts.google_say
        data_template:
          entity_id: "{{ speaker }}"
          message: "{{ message }}"
      - service: notify.lounge_tv
        data_template:
          message:  "{{ message }}"
      - service: notify.mobile_app_iphone
        data_template:
          title:  "{{ message }}"

With the fields property in the script I get the following config error:

Invalid config for [script]: [fields] is an invalid option for [script]. Check: script->script->notifications->fields. (See /Users/ashscott/.homeassistant/configuration.yaml, line 245). Please check the docs at https://home-assistant.io/components/script/

I’m stumped as to why it won’t work.

Any suggestions anyone?

1 Like
  notify:
    alias: "Notifications"
    sequence:
      - service: tts.google_say
        data_template:
          entity_id: "{{ speaker }}"
          message: "{{ message }}"
      - service: notify.lounge_tv
        data_template:
          message:  "{{ message }}"
      - service: notify.mobile_app_iphone
        data_template:
          title:  "{{ message }}"
1 Like

Thank you, Marc.

That’s what I started with but the docs call for the fields and when I include them I get the config error.

Either way it doesn’t work as expected.

Turns out that ‘reload scripts’ and ‘reload automations’ wasn’t enough to have this work. A full restart was required.

In summary, the correct code is as below, if it helps anyone else.

Automation:

    - service: script.notify
      data:
        speaker: 'media_player.kitchen_speaker'
        message: 'Xmas lights have been turned off'

Script

notify:
    alias: "Notifications"
    sequence:
      - service: tts.google_say
        data_template:
          entity_id: "{{ speaker }}"
          message: "{{ message }}"
      - service: notify.lounge_tv
        data_template:
          message:  "{{ message }}"
      - service: notify.mobile_app_iphone
        data_template:
          title:  "{{ message }}"
3 Likes

I’m trying to do something similar, but for integer. Can anyone say what is wrong in this code?

trigger:
- platform: state
  entity_id: sensor.vallox_outdoor_air
condition:
action:
  - choose:
    - conditions:
      - condition: numeric_state
        entity_id: sensor.vallox_outdoor_air
        below: '-20'
      sequence:
      - service: script.turn_on
        entity_id: script.set_fan_speed
        data:
          power_percent: '50'

I have verified that the conditions above are met by toggling state of my Hue lamps.

And the script:

set_fan_speed:
  alias: Setting fan speed...
  sequence:
  - service: vallox.set_profile_fan_speed_home
    data: #tried also data_template:
      fan_speed: '{{ power_percent }}'
  mode: single

Running following script seems to work, so the problem is that delivering the parameter does not work.

set_fan_speed:
  alias: Setting fan speed...
  sequence:
  - service: vallox.set_profile_fan_speed_home
    data:
      fan_speed: '50'
  mode: single

Thanks!

Tried casting too, still no luck:

set_fan_speed:
  alias: Setting fan speed...
  sequence:
  - service: vallox.set_profile_fan_speed_home
    data_template:
      fan_speed: '{{ power_percent|int }}'
  mode: single

Anyone able to help?

Ok I solved it. Just in case someone else is wondering the same thing, the problem was how I called the script. The correct way (well, working at least) is as follows:

trigger:
- platform: state
  entity_id: sensor.vallox_outdoor_air
condition:
action:
  - choose:
    - conditions:
      - condition: numeric_state
        entity_id: sensor.vallox_outdoor_air
        below: '-20'
      sequence:
      - service: script.set_fan_speed
        entity_id: script.set_fan_speed
        data:
          power_percent: '50'

The difference is that I don’t call

script.turn_on

but

script.set_fan_speed

The script that works with this is:

set_fan_speed:
  alias: Setting fan speed...
  sequence:
  - service: vallox.set_profile_fan_speed_home
    data_template:
      fan_speed: '{{ power_percent|int }}'
  mode: single