Beginners question about line continuation in HA yaml

This code in an automation yaml snippet runs fine:

alias: test automation 2
description: Test automation for learning trigger variables even better
trigger:
  - entity_id:
      - sensor.random_sensor
    above: 5
    trigger: numeric_state
    below: 10
conditions: []
actions:
  - action: persistent_notification.create
    metadata: {}
    data:
      title: Value between thresholds
      notification_id: value_between
      message: >
        {{ trigger.entity_id }} is above {{ trigger.above }} and below {{ trigger.below }}( {{ states("sensor.random_sensor") }} )
mode: single

but when removing the > so that the message is on one line in the editor I get a red line on the left in the editor and the following error when pressing the Save button: Error in parsing YAML: bad indentation of a mapping entry (line: 16, column: 40)

How come?

If you want to remove the > then you need:

      message: "{{ trigger.entity_id }} is above {{ trigger.above }} and below {{ trigger.below }}( {{ states('sensor.random_sensor') }} )"

So swapped the " for single in the syntax and a " as outer

1 Like

Thanks for the speedy reply!

Your suggestion gave and error message when saving:
Message malformed: template value should be a string for dictionary value @ data[‘actions’][0][‘data’]

But the following worked

message: ´{{ trigger.entity_id }} isabove {{ trigger.above }} and below {{ trigger.below }}( {{ states("sensor.random_sensor") }} )´

The version with the “>” is more readable, but I was curious why the other would not parse.

In python and in this case in yaml whitespace infront of the first command really matters. It is a weird design concept of the language. Some love it, some hate it. But you absolutely need the right amount of spaces.

You didn’t use the syntax I posted.
Just copy paste it and it will work