Automation won't run due to the message

What’s wrong with the message in my first example?

The automation below won’t run, not even on a manual fire from the frontend.

  - alias: "Mobilnotis: Batteribyte behövs"
    trigger:
      platform: time
      at: '06:00:00'
      platform: time
      at: '12:00:00'
      platform: time
      at: '18:00:00'      
    action:
      - service: notify.ilias_galaxy_note_8
        data_template:
          title: "HEMMET {{ now().strftime('%H') + '.' + now().strftime('%M') }}"
          message: >
            Dags att byta batterier.
              {% for entity_id in states.group.battery_levels.attributes.entity_id %}
                {% set domain, device = entity_id.split('.') %}
                {% if states[domain][device].state | int < 20 and states[domain][device].state != 'unknown' %}
                  {{ states[domain][device].name }}: {{ states[domain][device].state }}%
                {% endif %}
              {% endfor %}

But this one does run on a manual fire:

  - alias: "Mobilnotis: Batteribyte behövs"
    trigger:
      platform: time
      at: '06:00:00'
      platform: time
      at: '12:00:00'
      platform: time
      at: '18:00:00'      
    action:
      - service: notify.ilias_galaxy_note_8
        data_template:
          title: "HEMMET {{ now().strftime('%H') + '.' + now().strftime('%M') }}"
          message: >
            Dags att byta batterier.

The only difference is the message.

I have verified the template in the Templates-section of my HA frontend, and I got the result i want.
The code is also correct.

Any thoughts?

are you missing a % sign at the beginning of this? You have one at the end of that line. I don’t know scripting well so this may not be a problem, but it just looked strange.

Close, I think that % is supposed to be a string in the message. Something like a list of , "battery domain : battery state % "

I’m pretty sure it needs to be escaped as a special character though. Just not sure how. maybe:

          message: >
            Dags att byta batterier.
              {% for entity_id in states.group.battery_levels.attributes.entity_id %}
                {% set domain, device = entity_id.split('.') %}
                {% if states[domain][device].state | int < 20 and states[domain][device].state != 'unknown' %}
                  {{ states[domain][device].name }}: {{ states[domain][device].state }}'%'

I actually don’t see anything that would prevent the first one from running when manually triggered. Are there no errors?

In any case, there is at least a problem in the trigger section. Without dashes you’ll just get the last one. Also you can make things simpler in the actions. I would write it as follows. Still, not sure it will help with the root cause.

  - alias: "Mobilnotis: Batteribyte behövs"
    trigger:
      - platform: time
        at: '06:00:00'
      - platform: time
        at: '12:00:00'
      - platform: time
        at: '18:00:00'      
    action:
      - service: notify.ilias_galaxy_note_8
        data_template:
          title: "HEMMET {{ now().strftime('%H.%M') }}"
          message: >
            Dags att byta batterier.
              {% for entity_id in state_attr('group.battery_levels', 'entity_id')
                   if states(entity_id)|int(20) < 20 %}
                {{ state_attr(entity_id, 'friendly_name') or entity_id }}: {{
                   states(entity_id) }}%
              {% endfor %}

You were all pointing at the right direction.
The percentage character % was the culprit.
After removing it, the script works as expected.

Thanks a lot!

I hope you implemented @pnbruckner’s suggestion about the trigger, without it, the only trigger that will occur is:

      platform: time
      at: '18:00:00'
1 Like

That is very strange. I wonder if the notify.ilias_galaxy_note_8 service itself has a problem with a message string that contains that character, because I would think it should not be causing problems with the template and automation. It would be interesting to see (with that character still there) in home-assistant.log if the automation was triggered and the service was called and what the parameters of the service call were, and if it was the service itself that didn’t work.

@petro yes of course. Sorry @pnbruckner for not mentioning that.

@pnbruckner I don’t know since I could never test if the %-character caused any problem. When I removed that character everything was working fine.

Did you try including it in single quotes as I suggested?

I tried several things but without any success.
The automation does not run if i have a % sign in any string:

Here is what i have tried:

{{ states[domain][device].name +': '+ states[domain][device].state + ' %' -}}

and

{{ states[domain][device].name +': '+ states[domain][device].state -}}
{{ ' %' }}

and also

{{ states[domain][device].name +': '+ states[domain][device].state  -}}
{% raw %}
%
{% endraw %}

But with no luck.

And what do you see in home-assistant.log? Is the automation running but the service is failing, or is it the automation that is failing?

There is no output in the log file.

I guess the notification service I am using (Join) might be the culprit here.
When adding the %-sign to another working automation that uses Join för the notification, that automation also stopped working.