Template in script not behaving as expected

I have the following script, and I’m passing in my_entity_id The script calls a notification script and the title is showing the entity_id I passed in. So far so good. However message is incorrect. I want to send the same entity_id value for the message. The {% set entity = '{{my_entity_id}}' %} isn’t working as expected because the variable entity is blank. So the notification I’m getting has the correct title, but a blank body. Eventually, I want to do some string manipulation to entity but I can’t do that with a blank variable.

I’m stumped.

save_activity_state:
  sequence:
    - service: script.conditional_notify
      data_template: 
        boolean: binary_sensor.allow_automation
        title: "{{my_entity_id}}"
        message: >
          {% set entity = '{{my_entity_id}}' %}
           (manipulating entity code not shown)
          "{{entity}}"

message: >
          {% set entity = {{my_entity_id}} %}
           (manipulating entity code not shown)
          {{entity}}

Actually:

        message: >
          {% set entity = my_entity_id %}
           (manipulating entity code not shown)
          {{entity}}
1 Like
save_activity_state:
  sequence:
    - service: script.conditional_notify
      data_template: 
        boolean: binary_sensor.allow_automation
        title: "{{my_entity_id}}"
        message: >
                  {% set entity = {{my_entity_id}} %}
                  {{entity}}

Fails validation

Invalid config for [script]: invalid template (TemplateSyntaxError: expected token ‘:’, got ‘}’) for dictionary value @ data[‘script’][‘save_activity_state’][‘sequence’][0][‘data_template’][‘message’]. Got ‘{% set entity = {{my_entity_id}} %} {{entity}}\n’. (See /config/configuration.yaml, line 33). Please check the docs at Scripts - Home Assistant
Configuration reloading

This also has a blank value for entity. If I change that line to {% set entity = "foobar" %} then foobar will appear in the message. This is essentially the problem I’ve been fighting.

save_activity_state:
  sequence:
    - service: script.conditional_notify
      data_template: 
        boolean: binary_sensor.allow_automation
        title: "{{my_entity_id}}"
        message: >
          {% set entity = my_entity_id %}
          {{entity}}

ETA: I tried this a second time and deleted all the spaces and then used tabs. It’s working now!

save_activity_state:
  sequence:
    - service: script.conditional_notify
      data_template: 
        boolean: binary_sensor.allow_automation
        title: "{{my_entity_id}}"
        message: >
          {% set entity = my_entity_id %}
          {% set entity = (entity | replace("sensor.","input_text.")) %}
          {{entity}}

I think the problem is how you’re testing it, or maybe you haven’t restarted/reloaded properly after changing the script. If my_entity_id is defined in the title template, then it will also be defined in the message template.

It’s very strange. I’m going to do a reboot and see if that helps

Huh? NEVER use tabs in YAML files. You’re just asking for trouble.

I generally agree. But that was the only change I did. In fact, your code was basically identical to my original. I spent hours on this and still don’t know why it started working.

you can use tabs but you have to use the same tab protocol everywhere in the file. It doesn’t like the mix of tabs & spaces (as far as I know).

However, it’s more “standard” to use spaces. but you have to make sure the spacing is also using a regular protocol as well.

from the posts above it looked like your spacing was always off.

And in the first posts you had a template inside of a template {%…{{ …}}…%} and that’s not allowed.