Struggling to build json payload in yaml

Being new to HA I’m trying to strike up a friendship with the yaml files, but I don’t think I left a very good first impression, in particular when trying to template a payload for a restful command.

I started with a very simple json payload string with a single variable. It worked correctly, but had no null check. I’m trying to rectify this and also base other parts of the payload on variables.

payload: >
      {"Message":"
      {% if message is not none %}
      {{ message }}
      {% else %}
      No message set
      {% endif %}
      ","Color":"
      {% if font_color is not none %}
      {{ font_color }}
      {% else %}
      \#00fbff
      {% endif %}
      ","Font":"DejaVuSans","FontSize":12,"Position":"Center","PixelsPerSecond":19,"AntiAlias":false,"AutoEnable":true}

When I converted message into the if/else it worked great. I then proceeded with color and here I ran into problems.

The API just gives me error 500 no matter if I supply a color or not. AFAIK there’s no built-in way to inspect the outbound API call in HA(?), so I’d like to know if anyone can spot what I’m doing wrong?

  • Am I missing a tilde somewhere? Its use is a little unclear to me.
  • Is it the escaping of the #, that might be the issue? But then again, it doesn’t work when I do supply a color, so this smells of wrongly concatenated text…
  • Is there an better way to build a json payload than the approach I’ve taken here?

Thanks!

If it is a concatenation issue this might fix it:

payload: >
  {"Message":"
  {% if message is not none %}
    {{ ~ message ~ }}
  {% else %}
    {{ ~ "No message set" ~ }}
  {% endif %}
    ","Color":"
  {% if font_color is not none %}
    {{ ~ font_color ~ }}
  {% else %}
    {{ ~ "#00fbff" ~ }}
  {% endif %}
 
","Font":"DejaVuSans","FontSize":12,"Position":"Center","PixelsPerSecond":19,"AntiAlias":false,"AutoEnable":true}

Ok, I’ll try that approach.

On the question of seeing outbound requests, is that something that has been addressed, either natively in HA or by some external project? I test my requests before moving them to yaml, but once work is being done there, it’s a bit black box…

Not that I know of.

Also just realised this. change is not none to is defined for both variables.

I unfortunately got Unexpected Tilde on the above and removed them all one by one until it none were left, and only then did it pass parsing.

API still not happy with my request, very frustrating.

What about this?

payload: >
  {"Message":{{ message if message is defined else '"No message set"'}},"Color":{{ font_color if font_color is defined else '"#00fbff"' }},"Font":"DejaVuSans","FontSize":12,"Position":"Center","PixelsPerSecond":19,"AntiAlias":false,"AutoEnable":true}

Thanks I’ll check right away.

Btw,I tested removing the color if/else statement in your previous example and then it worked again. So it seems to be loosely related to how the second argument gets written to the playload string.

Ok, so I tested… and tinkered. I realized there were double quotes missing around the payload value of message and color. This works:

payload: >
  {"Message":"{{ message if message is defined else 'No message set'}}","Color":"{{ font_color if font_color is defined else '#00fbff' }}","Font":"DejaVuSans","FontSize":12,"Position":"Center","PixelsPerSecond":19,"AntiAlias":false,"AutoEnable":true}

Based on my tests, it seems like replacing none with defined somewhat hits the mark. I found that if data attribute message is missing completely, defined would be false, and “No message set” would correctly be returned. But if data attribute message is defined, but the value is not set (null), then it would return true nevertheless.

Would this be correct syntax for checking for both?

message if message is defined and message is not none

I’m a little unsure how verbose one needs to be (can the last message reference be excluded?)

Also, it’s the first time I’ve experienced syntax “message if message is defined”, love it!

Yes you need the verbose version.

message if message is defined and message is not none