Please help me with variables in a script!

I have the same script over and over, so I am trying to simplify it by rewriting the script to use variables. However, I just can’t get it to work. Can a kind soul please tell me what stupid mistake I am making?

This is the script without variables, which does work:

roomba_clean:
  alias: Roomba - Clean
  mode: single
  sequence:
    - service: vacuum.send_command
      data:
          entity_id: vacuum.roomba
          command: start
          params:
            pmap_id: XXXYYYZZZ
            regions:
              - region_id: '9'
                type: rid
            user_pmapv_id: AAABBBCCC

This is the same script but with variables. It does not work:

roomba_clean:
  alias: Roomba - Clean
  mode: single
  sequence:
    - variables:
        region_id: 9
        type: rid 
        pmap_id: XXXYYYZZZ 
        user_pmapv_id: AAABBBCCC 

    - service: vacuum.send_command
      data:
        entity_id: vacuum.roomba
        command: start
        params:
          pmap_id: "{{pmap_id}}"
          regions:
            - region_id: "{{region_id}}"
              type: "{{type}}"
          user_pmapv_id: "{{user_pmapv_id}}"

I think it needs to look more like this.

roomba_clean:
  alias: Roomba - Clean
  mode: single
  variables:
    region_id: 9
    type: rid 
    pmap_id: XXXYYYZZZ 
    user_pmapv_id: AAABBBCCC 
  sequence:
    - service: vacuum.send_command
      data:
        entity_id: vacuum.roomba
        command: start
        params:
          pmap_id: "{{pmap_id}}"
          regions:
            - region_id: "{{region_id}}"
              type: "{{type}}"
          user_pmapv_id: "{{user_pmapv_id}}"

Thanks for the suggestion @SgtBatten ! But the way I coded it is actually what HA shows in their example: Script Syntax - Home Assistant

I think I have narrowed it down further, but I still do not know how to fix it. My variable version of the script now works with all parameters except region_id. I think the Roomba expects region_id to be a string. My hard coded example works because the 9 is within single quotes:

region_id: '9'

I tried forcing the value to be a string using both methods below but neither worked:

region_id: "{{ region_id | string }}"

region_id: '"{{ region_id }}"'

Thoughts?

I’m no expert here but the description for that page refers to automations and amazon alexa integration. I referenced the script page when i edited your example. Not sure exactly how you are using this though. I’m now assuming both are valid.

If that is indeed the issue i would have expected your solutions to resolve it. Sorry I can’t help further with that.

I see what you’re saying. I swapped it around so that variables is at the same level as sequence, but unfortunately I still have the same issue. I’ll keep plugging away at this. I appreciate your help!

Anyone else have any thoughts on this? I think the issue comes down to this:

Can we step back?

What is the end goal? Are you looking to pass parameters to the script to action them? or are you simply trying to have variables in the top of the script?

What are you actually trying to accomplish?

Hi @calisro. That’s a fair question. I am pretty certain I formatted my initial script incorrectly, which I can fix. But the ultimate goal is to have one single script to which I pass parameters.

In order to pass parameters, you include a section like this:

fields:
  message:
    description: Message to send
    example: This is a message
  target:
    description: The group/entity to broadcast to
    example: group.downstairs_speakers
  media_content_type:
    description: The type of media
    example: audio/mp3
  stop_display:
    description: Stop the display at the end
    example: true
  volume:
    description: Specify target volume
    example: 0.5
  delay_to_stop:
    description: The Delay to stop the Display
    example: 15

Then you can pass them in. I believe the reason your script above though isn’t working with that 9 is because you defined that variable like this:

</s> <s> - variables:</s> <s> region_id: 9</s> <s> type: rid </s> <s> pmap_id: XXXYYYZZZ </s> <s> user_pmapv_id: AAABBBCCC </s> <s>

instead of
</s> <s> - variables:</s> <s> region_id: '9'</s> <s> type: rid </s> <s> pmap_id: XXXYYYZZZ </s> <s> user_pmapv_id: AAABBBCCC </s> <s>

EDIT: This is a good read. THere’s a hacky solution in there…

Thanks @calisro. I will give this a try.