Failing automation using event data template

Hi,

using this automation:

  - alias: 'GitHub repo update'
    trigger:
      platform: event
      event_type: state_changed
    condition:
      - condition: template
        value_template: >
          {{ trigger.event.data.entity_id.startswith('sensor.github')}}
      - condition: template
        value_template: >
          {{ 'old_state' in trigger.event.data and 'new_state' in trigger.event.data and
             trigger.event.data.new_state.attributes.latest_release_url !=
             trigger.event.data.old_state.attributes.latest_release_url }}
    action:
      service: notify.system
      data_template:
        title: Github repo update
        message: >
          {{trigger.event.data.entity_id}} was updated to {{trigger.event.data.entity_id.attributes.latest_release_url.split('tag/')[1]}}

I can see the automation being triggered, but the message isn’t received (probably isn’t even sent, but I dont see any logs indication an issue)

As far as I have ben able to test, it must be the template, because all other lines have worked fine: the service is ok, and, since the automation is triggered, everything before that, eg the trigger and conditions, must be fine too.

to give you an idea of the entity:


What could be wrong in the template, please have a look?

this does work:

{% set id = states.sensor.github_multiple_entity_row %}

{{id.attributes.name}} was updated to {{id.attributes.latest_release_url.split('tag/')[1]}}


thanks!

Yeah, if the last_triggered time gets updated, it passed all of the conditions (pretty sure).

What happens if you trigger the automation manually in dev tools? (Not sure how it handles trigger.event when you bypassed the entire trigger honestly)

This one doesn’t make much sense:
{{trigger.event.data.entity_id.attributes.latest_release_url.split('tag/')[1]}

trigger.event.data.entity_id is a string. Won’t have attributes…

You would think though that an error here would show up in the logs.

Either way, either use trigger.event.data.new_state.attributes… or use state_attr with the entity_id.

        title: Github repo update
        message: >
            {{trigger.event.data.entity_id}} was updated to {{ state_attr(trigger.event.data.entity_id, 'latest_release_url').split('tag/')[1]}}

2 Likes

If you bypass the trigger, you’ll produce errors referencing the trigger not existing. So that won’t work.

I agree. it needs to be what you have or using the trigger. And

It should have this error in the logs:

AttributeError: 'str' object has no attribute 'attributes'

of course!, thanks you for that, re-opening my eyes…

using a slightly adapted version with name:

         {{state_attr(trigger.event.data.entity_id, 'name')}} was updated to {{ state_attr(trigger.event.data.entity_id, 'latest_release_url').split('tag/')[1]}}

did see that in the template editor, but can’t find anything in home assistant.log. Will control_f once more.

thanks and will report back!

this works perfectly! just got reminded , mini-graph-card was updated :+1:

let me ask an additional question Ive been forgetting over and over: Id like to create a link in the notification, so the underlined text holds an external link to the assets:

  - alias: 'GitHub repo update'
    trigger:
      platform: event
      event_type: state_changed
    condition:
      - condition: template
        value_template: >
          {{ trigger.event.data.entity_id.startswith('sensor.github')}}
      - condition: template
        value_template: >
          {{ 'old_state' in trigger.event.data and 'new_state' in trigger.event.data and
             trigger.event.data.new_state.attributes.latest_release_url !=
             trigger.event.data.old_state.attributes.latest_release_url }}
    action:
      service: notify.system
      data_template:
        title: Github repo update
        message: >
         {{state_attr(trigger.event.data.entity_id, 'name')}} was updated to {{ state_attr(trigger.event.data.entity_id, 'latest_release_url').split('tag/')[1]}}.
         Visit the release page {{ state_attr(trigger.event.data.entity_id, 'latest_release_url')}} to download.

shows the link in full, but as you understand I would like it to show:

      Visit the [release page](http:// {{ state_attr(trigger.event.data.entity_id, 'latest_release_url')}}) to download.

showing like:

Visit the release page to download.

of course above doesn’t work, but you get my drift…

That should work in the markdown card… or

"<a href=\"{{ state_attr(trigger.event.data.entity_id, 'latest_release_url')}}\">Visit the release page to download.</a>"

Ya, in a markdown ok. But I am looking for the syntax in a notification …
Is the code you suggest capable of doing so in the message field ?

I doubt anything will work in a message field. You’d have to look up what your message field supports.