Output single quotes from template in automation

I am trying to get an automation with a template running. I have a hardcoded, working version that does not use a template. I am struggling to reproduce one line that contains single quotes in my Action (everything else works).

The goal is to get this line, with single quotes around the number:

- region_id: '3'

Assume that trigger.payload is set to β€˜room_one’.

When using "{{ iif( trigger.payload == 'room_one', '3', '999') }}", I get no quotes around my 3:

- region_id: 3

When I escape like this region_id: "{{ iif( trigger.payload == 'room_one', '\\'3\\'', '999') }}", I get triple quotes:

- region_id: '''3'''

I didn’t manage to get just single quotes. Thanks for any hints.

This should be enough, or do you get some error with this?

In yaml single quotes are escaped with single quotes, like this:

region_id: "{{ iif( trigger.payload == 'room_one', ''3'', '999') }}"

However if you are trying to match the whole string then you probably need to do this:

region_id: >
  {{ iif( trigger.payload == "'room_one', '3', '999'") }}
1 Like

I think it is not enough. With your suggestion, the autromation is executed, my device (a roomba) received the command and beeps but does not execute the action. I guess it is the quotes, because the hardcoded version with the quotes works.

The first suggestion gives me a parse error when saving the automation. I think this is not a case of yaml escaping, as for yaml everything is inside double quotes anyway. It seems to be a case of jinja excaping, hence my try with the backslashes.

The second suggestion is not what I need. I need an output with singke quotes, but no matter what I try, in the trace I see only either no single quotes or triple single quotes, as described in my original post.

Try it. It has single quotes around each item with the whole string enclosed in quotes.

I did. I don’t want trigger.payload to match "'room_one', '3', '999'", I want to get a β€˜3’ if trigger.payload matches room_one, elser a β€˜999’.

I tried barious combinations of escaping, using > and a separate line, all to no avail. I either get no single quotes around my 3, or triple single quotes, or even 5 single quotes, but not β€˜3’.

I have the same issue, I get 0 or 3 quotes. I think I tried all combos to get 1 but never succeeded. Did you figure out how to get the 1 quote here?

What are you trying to execute with the automation?

image

{{ "'3'" if is_state( 'your.entity', 'on') else "'99'" }}

Paste your automation as it stands. I’m not clear where you are wanting the single quotes to appear.

this is the blueprint:

#Home assistant script blueprint for starting a vacuum cleaner
blueprint:
  name: Vacuum
  description: Start a vacuum cleaner
  domain: script
  input:
    vacuum:
      name: Vacuum
      description: The vacuum to start
      selector:
        entity:
          domain: vacuum
    room:
      name: Room
      description: The room
      selector:
        entity:
          domain: input_text

variables:
  room: !input 'room'
  room_text: "{{ states(room) }}"
  room_number: "{{ states(room).split(',')[1] | int}}"

sequence:
 
  - service: vacuum.send_command
    target:
      entity_id: !input 'vacuum'
    data:
      command: start
      params:
        pmap_id: OV68p3N5TcS3vGlZS_R2bg
        regions:
          - region_id: '{{  room_number }}'
            type: rid
      - region_id: '{{  room_number }}' 

should result in:
- region_id: β€˜13’

The actual result is
- region_id: 13

When I do this:
- region_id: β€œβ€˜{{ room_number }}’”
result is:
- region_id: β€˜β€™β€˜13β€™β€˜β€™

I tried a lot of way on adding the single ', however it seems to be removed they become 3 quotes

Try:

        regions:
          - region_id: '{{  "\x27" ~ room_number ~ "\x27" }}'

I tried it in a UI-generated script:

alias: quote test
sequence:
  - service: notify.admin
    metadata: {}
    data:
      message: |
        {% set x = 3 %} {{ "- region_id: \x27" ~ x ~ "\x27" }}
mode: single

and my phone notification has the quotes.

Hex 27, decimal 39 is the ASCII code point for the single quote.

1 Like

I tested your code in actual automation and recieved the quotes as well

The same result:

adding the | on the - region_id: | line results in:

  • region_id: β€˜β€™β€˜β€™β€˜13β€™β€˜β€™β€˜β€™

{% set x = 3 %} {{ '- region_id: \x27' ~ x ~ '\x27' }} produce the correct outcome?

Not when I try to use it in a blueprint script, in the template editor it looks all as I would expect.

Long shot, defining as JSON rather than YAML:

        regions: >
          {{ [ { "region_id": "\x27" ~ room_number ~ "\x27",
                 "type": "rid" } ] }}

it does insert the extra quotes this way as well

when I add a 0 for the number it seems that the quote is added correctly:

The vacuum works this way. It seems a big hack, if I give the number as a int or string it fails. When a 0 is added before the number, it seems to see that it needs β€˜β€™ to keep it the same

My gut tells me this is the source of the additional quotes

test without the quotes

room_number: {{ states(room).split(',')[1] | int}}