My script is not found but I cannot see any error messages

I have two simple scripts configured YAML files. One appears in list of Services, the other does not. The system does not report any error messages. What have I done wrong?

Script that works:

swimmingpool_package_1:

  script:

    start_swimmingpool_cleaning:
      alias: Start Swimming Pool Cleaning
      sequence:
        service: script.swimmingpool_cleaning
        data:
          cycle_count: 5
#        mode: single 

Script that does not work:

swimmingpool_package_2:

  script:

    swimmingpool_cleaning:
      alias: Swimming Pool Cleaning
      sequence:
        repeat:
          while: "{{ cycle_count > 0 }}"
          sequence:
            cycle_count: "{{ cycle_count - 1 }}"
            service: notify.persistent_notification
            data:
              message: "Pool Cleaning"
              title: "Pool Cleaning Notification"
#      mode: single 

Example trying to CALL SERVICE:

service: script.start_swimmingpool_cleaning
data: {}

Service script.swimmingpool_cleaning not found.

Thanks in anticipation.

Are you using the UI? Where did you put these scripts?

Hi Stephen,
No, I am not using the UI to create the scripts - I am using the File editor.
Each script is in a separate YAML package named as:
/homeassistant/packages/swimmingpool/swimmingpool_package_1.yaml
and
/homeassistant/packages/swimmingpool/swimmingpool_package_2.yaml

My confg YAML (/homeassistant/configuration.yaml) is:

# Loads default set of integrations. Do not remove.
default_config:

homeassistant:
  packages: !include_dir_merge_named packages/  

switch:
  - platform: template
    switches:
      ewelink_virtual_switch:
        turn_on:
          service: switch.turn_on
        turn_off:
          service: switch.turn_off

My Packages folder contains:

I guess that my usage of “cycle_count” is wrong!

Any reason you aren’t using the UI? I am YAML-challenged and the UI has made my Home Experience much easier.

Hi Stephen,
I do not know how to create a Package YAML using the UI!
I do not even know how to use the UI to create a Service like my two examples!
I am UI challenged and YAML challenged.
In this day-and-age I would hope to be using an object-oriented language like C#.
I think that YAML has been an unfortunate language choice for configuration - but that is what it is.
I would appreciate any pointers on using the UI for Scripts, particularly with Scripts in Packages if that is possible.

I am surprised either works. Based on the example in the documentation, you should repeat the folder and file name in the file itself, separated by an underscore.

So either:

/homeassistant/packages/swimmingpool/swimmingpool_package_1.yaml

swimmingpool_swimmingpool_package_1:
  script:
    start_swimmingpool_cleaning:

or

/homeassistant/packages/swimmingpool/package_1.yaml

swimmingpool_package_1:
  script:
    start_swimmingpool_cleaning:

But as a caveat, I don’t use this method so this is just a guess. I use the !include_dir_named method for packages which allows me to name the files however I please, and the first heading in the file is the same as if it were in configuration.yaml

Thanks Ric, I tried your suggestion but it had no affect.
I would love to see an article discussing best practice for naming things when working with Packages!

Your script is malformed. Variables have to be defined with the variables key, and once you add more than one item in a sequence it must be a list. It’s good habit to make it a list when it’s only 1 item as well.

alias: Swimming Pool Cleaning
sequence:
  - repeat:
      while: "{{ cycle_count > 0 }}"
      sequence:
        - variables:
            cycle_count: "{{ cycle_count - 1 }}"
        - service: notify.persistent_notification
          data:
            message: Pool Cleaning
            title: Pool Cleaning Notification
1 Like

Thanks Rick,
I have made the changes you suggested, and the Script is now found!
But I am having troubles with “Variables”. You stated “Variables have to be defined” but the documentation at “counted repeat” seems to indicate otherwise.
Two related questions? When I test this script it causes an infinite loop which creates thousands of notifications!

  1. Is there a way to dismiss ALL notifications?
  2. Is there a way to halt a runaway script, other than rebooting the HA computer?

A counted repeat is different from a “while” loop. In a counted repeat, count is a special key.

You can use a notification_id so that notifications get overwritten rather than duplicated. To fix the problem you already have, you can use the service call to dismiss all:

action:
  service: persistent_notification.dismiss_all

YAML is not a programming language, but it is replacing XML as a “more human readable” serialization language like XML and JSON. After a year of writing YAML files for my Home Assistant, I still don’t get it. Someday it will grok.

You probably shouldn’t use packages if you don’t understand YAML.

I find the UI easier to use. Go to Settings → Automations and Scenes → scripts → Add Script, then follow the prompts. Don’t forget to click on “save script”.

You can test/run the script from inside the UI. You can also see or edit the YAML code that the UI generates.

You’re doing many things wrong here.

  1. You’re overwriting a supplied variable. Automations & variables do not allow this.
  2. You made up a field outside variable action and just added it to a random service call. That’s not allowed. Yaml fields are rigid, there will be errors in your logs about extra fields not allowed cycle_count
  3. You should be using count, not while.
swimmingpool_package_2:

  script:

    swimmingpool_cleaning:
      alias: Swimming Pool Cleaning
      sequence:
        repeat:
          count: "{{ cycle_count }}"
          sequence:
            service: notify.persistent_notification
            data:
              message: "Pool Cleaning"
              title: "Pool Cleaning Notification"

But keep in mind that this will just hammer out cycle_count notifications. Not sure why you’d want this.

Hi Rick,

Thanks for two useful items of information, particularly:

Hi Stephen,
Thanks. Nice to know how manage Scripts from the UI.
Unfortunately for me, it seems to not understand Packages which I am using. I get the following error:

Visual editor is not supported for this configuration
Templates not supported in visual editor
You can still edit your config in YAML.

Hi Petro, Thanks. Your three comments are very helpful. I tried “Count” and it does what I want.
The useless notifications in the script are my way of debugging. Now I have the script being found I will put some useful actions in there.

1 Like