Calling a Script that passes Variables in an Automation

I want to be able to call a script as a service within an automation - is this possible?

When I manually call the script under Developer Tools and enter the below, this works fine.

service: script.set_hvac_mode
data: 
  aircon_deviceId: !secret study_aircon_deviceId
  aircon_hvac_mode: "off" 
  aircon_state_sensor_entityId: sensor.study_aircon_state

So I thought I could just directly paste this into the YAML for the automation for this to work … but turns out it doesn’t.

If I do this, then I get an error that “result is not a dictionary”

Just for context, the overall automation YAML looks like this:

trigger:
  - [triggers]
condition:
  - [conditions]
action:
  - if:
      - [conditions]
    then:
      - repeat:
          sequence:
            - service: script.set_hvac_mode
              data: 
                aircon_deviceId: !secret study_aircon_deviceId 
                aircon_hvac_mode: "off" 
                aircon_state_sensor_entityId: sensor.study_aircon_state
          count: 5

!secret is a YAML function. As such, it can be used in purely YAML script configuration, but not in the UI Automation and Script editors (or their dependent yaml files). You can add your script directly to configuration.yaml or any other properly split file.

thanks didgeridrew

does this mean I should be able to use !include function in automations.yaml?

eg.

secret: !include secrets.yaml

trigger:
  - [triggers]
condition:
  - [conditions]
action:
  - if:
      - [conditions]
    then:
      - repeat:
          sequence:
            - service: script.set_hvac_mode
              data: 
                aircon_deviceId: secret.study_aircon_deviceId 
                aircon_hvac_mode: "off" 
                aircon_state_sensor_entityId: sensor.study_aircon_state
          count: 5

However if I do this, code editor is telling I’m missing an action after the !include statement

I haven’t tested it, but I don’t think so… the default automations.yaml file is “controlled” by the UI editor. You need to make a separate file.

ah ok i see what’s going on

!secret has problems when editing in YAML mode under UI Automation and Script editors but it does work properly if you edit it with a code editor (eg. Studio Code Server) - seems to be a quirk of HA. only thing is you forgo the ability to edit via the UI/Script editor.

thanks again

1 Like