Multi-line template for custom roomba commands

I’m trying to create a custom script for sending commands to my Roomba. I have access to my specific room/zone IDs but it would be a lot easier if I don’t have to remember the ID numbers and just select a more friendly name. However, I think I’m doing something slightly wrong with my script code.

So as an example, the below command will work to send the vacuum to the Living Room (Room ID: 1) and to the Entryway (Zone ID: 11).

service: vacuum.send_command
data:
  command: start
  params:
    pmap_id: 0hVqO6_1RA-XDwpkLrv3Kw
    regions:
      - region_id: "1"
        type: rid
      - region_id: "11"
        type: zid
target:
  entity_id: vacuum.the_help

My non-functioning script YAML is:

sequence:
  - service: vacuum.send_command
    metadata: {}
    data:
      command: start
      params:
        pmap_id: 0hVqO6_1RA-XDwpkLrv3Kw
        regions: >
          {% for item in rooms %} 
          - region_id: {{ item }}
            type: rid
          {% endfor %}
          {% for item in zones %}
          - region_id: {{ item }}
            type: zid
          {% endfor %}   
    target:
      entity_id: vacuum.main_floor
mode: single
fields:
  rooms:
    selector:
      select:
        multiple: true
        options:
          - label: Living Room
            value: '1'
          - label: Office
            value: '4'
          - label: Hallway
            value: '8'
          - label: Dining Room
            value: '10'
          - label: Kitchen
            value: '5'
    name: Rooms
    required: false
  zones:
    selector:
      select:
        multiple: true
        options:
          - label: Entryway
            value: '11'
          - label: Garage Door
            value: '0'
          - label: Front Door
            value: '1'
          - label: Kitchen Counter
            value: '3'
          - label: Back Door
            value: '6'
    name: Zones
    required: false

I know this issue is somewhere with how I templated this bit:

        regions: >
          {% for item in rooms %} 
          - region_id: {{ item }}
            type: rid
          {% endfor %}
          {% for item in zones %}
          - region_id: {{ item }}
            type: zid
          {% endfor %}

Can anyone correct what I did wrong or point me in the right direction?

You need to use namespace so that your loops output a real list instead of a list-shaped string.

        regions: >
          {% set ns = namespace(regions=[]) %}
          {% for item in rooms %} 
          {% set ns.regions = ns.regions + [{"region_id": item, "type": "rid"}]
          {% endfor %}
          {% for item in zones %}
          {% set ns.regions = ns.regions + [{"region_id": item, "type": "zid"}]
          {% endfor %}
          {{ ns.regions }}

EDIT: Added missing set

Thank you! When I try to use that code, I’m getting this error

Message malformed: template value should be a string for dictionary value @ data[‘sequence’][0][‘data’]

Any thoughts on why that would be?

        regions: >
          {% set ns = namespace(regions=[]) %}
          {% for item in rooms %} 
          {% set ns.regions = ns.regions + [{"region_id": item, "type": "rid"}] %}
          {% endfor %}
          {% for item in zones %}
          {%  set ns.regions = ns.regions + [{"region_id": item, "type": "zid"}] %}
          {% endfor %}
          {{ ns.regions }}

Try that.

1 Like

Oh duh, yeah that was it.

1 Like