YAML Multiline value_template spaces added

Hi!

This is driving me crazy…

value_template: >
          {% if states('input_number.car_heater_hour')|round(0)|string|length == 1 %}0{% endif %}
          {{ states('input_number.car_heater_hour')|round(0)|string }}:
          {% if states('input_number.car_heater_minutes')|round(0)|string|length == 1 %}0{% endif %}
          {{ states('input_number.car_heater_minutes')|round(0)|string }}

I’m getting a space when a “0” or “:” are added.

Ex.
When input_number.car_heater_hour = 1.0
and input_number.car_heater_minutes = 5.0

I should get “01:05” but I get “0 1: 0 5”

When I put everything in one row without newlines, no spaces are added.

Please help! I have banged my head against the wall for a couple of hours now…

1 Like
value_template: >
          {% if states('input_number.car_heater_hour')|round(0)|string|length == 1 %}0{% endif -%}
          {{- states('input_number.car_heater_hour')|round(0)|string -}}:
          {%- if states('input_number.car_heater_minutes')|round(0)|string|length == 1 %}0{% endif -%}
          {{- states('input_number.car_heater_minutes')|round(0)|string }}

Try:

value_template: >-
1 Like

Thanks Phil!

However, I don’t understand why the extra “-” characters are needed… :frowning:

Could you please explain?

1 Like

See https://jinja.palletsprojects.com/en/master/templates/#whitespace-control.

1 Like

Yes, I think I understand now but it’s a little bit confusing what is jinja and what is yaml.

Is everything after “value_template: >” parsed as jinja and the rest is parsed as yaml?

sensor:
  - platform: template
    sensors:
      car_heater_departure_time:
        friendly_name: 'Time of departure'
        value_template: >
          {% if states('input_number.car_heater_hour')|int|string|length == 1 %}0{% endif -%}
          {{- states('input_number.car_heater_hour')|int|string -}}:
          {%- if states('input_number.car_heater_minutes')|int|string|length == 1 %}0{% endif -%}
          {{- states('input_number.car_heater_minutes')|int|string }}

The value_template: > tells yaml to add spaces at new lines and the dashes in jinja removes them?

First the file is parsed as YAML. Then the value of value_template is parsed/evaluated as a Jinja template. The > actually replaces new lines with spaces. See https://yaml-multiline.info/.

Thanks! :smile: