Need help with getting this script to work

Tags: #<Tag:0x00007fc3ff2016e0>

I’m trying to make a script that the house can use to track whos turn it is to cut the grass. The script worked fine the first time it ran, but when it got to the last person it just stopped. I tried to use one choose condition, but the script failed right off the bat. I get the fowling when I try to run it now. All other script are working.

Failed to call service script/1626657398968. sequence item 0: expected str instance, Optional found

alias: Finished Mowing
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_text.whos_mowing
            state: Jakov
        sequence:
          - service: input_text.set_value
            data:
              value: Maddi
    default: []
  - choose:
      - conditions:
          - condition: state
            entity_id: input_text.whos_mowing
            state: Maddi
        sequence:
          - service: input_text.set_value
            data:
              value: David
            entity_id: input_text.whos_mowing
    default: []
  - choose:
      - conditions:
          - condition: state
            entity_id: input_text.whos_mowing
            state: David
        sequence:
          - service: input_text.set_value
            data:
              value: Andrew
            entity_id: input_text.whos_mowing
    default: []
  - choose:
      - conditions:
          - condition: state
            entity_id: input_text.whos_mowing
            state: Bryan
        sequence:
          - service: input_text.set_value
            data:
              value: Jakov
            entity_id: input_text.whos_mowing
    default: []
  - choose:
      - conditions:
          - condition: state
            entity_id: input_text.whos_mowing
            state: Bryan
        sequence:
          - service: input_text.set_value
            data:
              value: Jakov
            entity_id: input_text.whos_mowing
    default: []
mode: single

Try this version:

finished_mowing:
  alias: Finished Mowing
  sequence:
    - variables:
        roster: {'Jakov': 'Maddi', 'Maddi': 'David', 'David': 'Andrew', 'Bryan': 'Jakov'}
        mower: "{{ states('input_text.whos_mowing') }}"
    - service: input_text.set_value
      target:
        entity_id: input_text.whos_mowing
      data:
        value: "{{ roster[mower] }}"

BTW, are you using Home Assistant’s Script Editor to create the script or a text editor?

This could be a whole lot simpler by using an input_number as an index and just iterating:

alias: Finished Mowing
variables:
  mow_names: ['Maddi', 'David', 'Andrew', 'Bryan', 'Jakov']
  mow_index: "{{ states('input_number.mow_index')|float }}"
sequence:
  - service: input_text.whos_mowing
    data:
      value: "{{ names[mow_index] }}"
  - service: input_number.set_value
    data:
      entity_id: input_number.mow_index
      value: "{{ (mow_index + 1) % mow_names|count }}"
mode: single

Each time you call it it should set input_text.whos_mowing to the name and increment the index input_number.mow_index for next time.

Edit: Ha, was typing at the same time. The docs make it look like variables: should be a peer to sequence:, but I haven’t tested anything. Could certainly use the value of the input_text as an index, as Taras mentioned. Coming from C, my brain doesn’t immediately go that way.

Yes, as a peer if defined prior to sequence but variables can also be defined after sequence (and then take the form I used). In my simple example, I doubt the location of its declaration makes any difference in the variable’s scope. However, you can declare variables in repeat, or in one of the choices of a choose, and then they definitely have a narrower scope.

I’m using the UI Editor. YAML confuses me. Thanks for replying though.

I’ll try this tomorrow when I have time. Thanks!

If you placed the names in an input_select you can just call input_select.select_next which by default will cycle the list. No need for any script.

2 Likes

I encourage you to become familiar with it, otherwise you will find it difficult to use Home Assistant’s more advanced concepts. Case in point is the example I posted. It’s a fraction of the size of the one employing a chain of choose statements.

The reason why I asked if you were using the visual editor is because the YAML it presents is slightly different from how it appears when manually created in a file containing other scripts. For example, the first line in my example (finished_mowing:) is not needed if you are using the visual editor.

BTW, the last two choose statements in your example are duplicates.

Another excellent suggestion!

Excellent idea! :+1:

Theovit should remember to specify the cycle option when using select_next to ensure it wraps around from last to first. Nope, it’s the default mode.

1 Like

So if I make scripts via the text editor should they show up in the script section of the UI? I’m having difficulting figuring out where to put the script. I can’t switch from UI to YAML it won’t save. When I try to replace the code in the file it said only scripts crated with the UI are assessable.

Yes, provided you put them in the same place as where the Script and Automation editors store their information. I believe the default nowadays is automations are stored in automations.yaml and scripts are in scripts.yaml.

It won’t let you save if whatever you created in the Automation Editor in YAML mode contains errors.

I think you might be using the word “scripts” interchangeably with “automations”. They’re different concepts and there are slightly different requirements for creating automations manually that can also be edited via the Automation Editor (but there’s no such restriction for scripts). Here’s what I mean:

  • If I create a script via the Script Editor, it is saved in scripts.yaml. If I edit that file and add another script, after executing Configuration > Server Controls > Reload Scripts, the added script will also be accessible to the Script Editor.

  • In the case of automations, if I manually add a new automation to automations.yaml, that new automation must have an id option with a unique value if I want it to also be edited via the Automation Editor. The id can be as simple as this:

id: abc123_light_schedule

After executing Configuration > Server Controls > Reload automations, the new automation becomes visible in the list of automations and can be edited via the Automation Editor. Without the id, it is visible in the list but cannot be edited via the UI.