Preserving Leading and Trailing Spaces in Automation Variables

Hello Home Assistant Community,

I’m encountering an issue with an script where I want to set a variable with text, including leading and trailing spaces, and then pass this string to the text.set_value service while preserving all spaces. Here’s the code snippet:

alias: TextSetValueTest
sequence:
  - variables:
      text: "          HOME"
  - service: text.set_value
    data:
      value: "{{text}}"
      target:
        entity_id: text.ff_office_switch_door_statustext1
mode: restart

The problem I’m facing is that when I pass the string to the text.set_value service, it removes all the spaces.

I’ve tried several workarounds such as using the intend filter, replace filter, and more, but unfortunately, none of them have been successful. Whenever I output the Jinja variable or use a filter, it always returns the string without spaces.

Interestingly, this behavior only occurs within the context of an automation. When I replicate the same logic in the DevTools → Template Editor, it works as expected.


I would appreciate any support or guidance on how to preserve the spaces.

Is the issue limited to the use of text.set_value or does it also occur for other service calls such as input_text.set_value?

I tried other services as well but the problem is the same:

alias: TextSetValueTest2
sequence:
  - variables:
      text: "          HOME"
  - service: browser_mod.console
    data:
      message: "{{text}}"
    target:
      entity_id: binary_sensor.pw_macbook_chrome
mode: restart


Your test results imply that the value of a script variable is subjected to a trim operation where leading/trailing whitespace is removed.

In the Template Editor, you’re defining a Jinja2 variable (used within a template) whereas in the automation you’re defining a script variable (used within the entire automation/script).

I suspect that the variable is being returned as a YAML scalar value, which is always trimmed, as indicated by the debugger (value without quotes). Unfortunately, a basic string {{value|string}} filter doesn’t resolve the problem.


— vs —

When quotes are introduced, the string is correctly passed, but the quotes are also considered part of the string.

Unfortunately, escaping doesn’t help in this case.

Looks like you’ll have to pad it with leading spaces in the message option.

alias: TextSetValueTest2
sequence:
  - variables:
      text: "HOME"
  - service: browser_mod.console
    data:
      message: "          {{text}}"
    target:
      entity_id: binary_sensor.pw_macbook_chrome
mode: restart

I ventured into this rabbit hole with no success.

So what you’re saying is that the service call also performs an implicit trim on its value?

In that case, looks like there’s no recourse.

I can reproduce the behavior independently of a service call within a “variables” block as well. I have summarized the problem and my attempted solutions in a copy-and-paste script here:

alias: Try to Perserve Spaces
sequence:
  - variables:
      text: "        HOME"
      opt1: "{{text}}"
      opt2: "{{text|string}}"
      opt3a: "\"{{text}}\""
      opt3b: "{{opt3a | replace('\"', '')}}"
      opt4: \"{{text}}\"
      opt5: "{{ ' '| safe * 8 }}{{text}}"
      opt6: "{{text[0:12]}}"
  - stop: ""
mode: restart

None of the options returns the expected result. But I suppose there are some other options that I am not aware of.

Did you ever solve this?

Not yet. But still interested to solve it.