Parsing automation with template

I set up my first automation within homeassistant and I am having trouble with how to enter the yaml part which includes a template. The problem ist just with the template part with the action, the rest already works.

When I enter the following action with everything hardcoded (no template), that works:

service: vacuum.send_command
data:
  entity_id: vacuum.rob
  command: start
  params:
    pmap_id: 12345
    regions:
      - region_id: "3"
        type: rid
    user_pmapv_id: 12345
enabled: true

When I look at the trace of that action, I see the following:

params:
  domain: vacuum
  service: send_command
  service_data:
    entity_id: vacuum.rob
    command: start
    params:
      pmap_id: 12345
      regions:
        - region_id: '3'
          type: rid
      user_pmapv_id: 12345
  target: {}
running_script: false
limit: 10

So that is the goal I want to reach, but with a template and the region_id and type dependent on trigger.payload. Using that variable from the trigger already works, but my problem lies in the formating of my yaml/template.

When I enter the following action as yaml, my action is executed, but the region part is not constructed proberly and the command is not understood by the entity:

service: vacuum.send_command
data_template:
  entity_id: vacuum.rob
  command: start
  params:
    pmap_id: 12345
    regions: >
      {% if trigger.payload == 'room_one' %}
       - region_id: '3'
      {% else %}
       - region_id: '999'
      {%- endif -%}
       type: {{ 'zid' if trigger.payload == 'zone_not_room' else 'rid' }}
    user_pmapv_id: 12345
enabled: true

In the trace I see (when trigering with trigger.payload == room_one):

params:
  domain: vacuum
  service: send_command
  service_data:
    entity_id: vacuum.rob
    command: start
    params:
      pmap_id: 12345
      regions: |-
        - region_id: '3'
        type: rid
      user_pmapv_id: 12345
  target: {}
running_script: false
limit: 10

So that > gets turned into |- and I am not sure why and why I need the > at all (saw it in some examples). This is probably just something small but I can’t figure it out. Thanks fort helping.

For most purposed in HA the > and |- multiline quote symbols are equivalent. The symbols indicate to the yaml parser that what follows should be read as if they are surrounded in quotation marks. All templates must be surrounded in quotes or preceded by a multiline quote symbol.

Your examples were missing quotes around the type template.

You cannot build the keys of yaml configuration with templates, you can only use them to render the values.

service: vacuum.send_command
data_template:
  entity_id: vacuum.rob
  command: start
  params:
    pmap_id: 12345
    regions: 
      - region_id: "{{ iif( trigger.payload == 'room_one', '3', '999') }}"
        type: "{{ 'zid' if trigger.payload == 'zone_not_room' else 'rid' }}"
    user_pmapv_id: 12345
enabled: true
1 Like

Thanks for the quick reply and the explanations, much appreciated! I still didn’t get it to work properly yet.

The outputs of the working, hardcoded command (as per trace screen) and the last example from your post differ in single quotes around the number 3 (line region_id). The hardcoded command has the quotes in the output, the example you provided with the template does not have them.

Not sure if that is the actual problem, but that is the only difference in the output I can see.

Anyway, I guess I can fix this one by myself. Thanks again!

Hmm, I either get zero quotes when using "{{ iif( trigger.payload == 'room_one', '3', '999') }}":

- region_id: 3

or three quotes when I use region_id: "{{ iif( trigger.payload == 'room_one', '\\'3\\'', '999') }}":

- region_id: '''3'''

Didn’t manage to get just

- region_id: '3'