Trigger.entity_id please help with template

Hi everybody,

I am trying to automate sending notifications when certain sensors state their data. What is wrong with my template? Nothing happens at the moment:

automation:
  - id: 'not_yt_all'
    alias: "[Notify] YouTube Abos"
    trigger:
      - platform: state
        entity_id: sensor.youtuber1
      - platform: state
        entity_id: sensor.youtuber2
    action:
      - service: notify.wir_alle
        data_template:
          title: "{{ state_attr('trigger.entity_id', 'friendly_name') }}"
          message: >
            📺 Neues Video! {{ state_attr('trigger.entity_id', 'friendly_name') }} ➡ {{ states('trigger.entity_id') }} ({{ state_attr('trigger.entity_id', 'published')[11:16]  }} Uhr am {{ state_attr('trigger.entity_id', 'published')[:10] }})

Let’s say youtuber1 (friendly_name YouT) releases “my new video”. The sensor picks this up and I should get the following message: 📺 Neues Video! YouT ➡ my new video (12:15 Uhr am 2019-10-01).

I had a hard-coded version of this written for each youtuber, which worked fine; but now that I am trying to just add any additional subscription to the trigger entity_ids, it won’t work any longer.

This is my previous code

automation:
  - id: 'not_yt_rtg'
    alias: "[Notify] YouTube YouT"
    trigger:
      - platform: state
        entity_id: sensor.youtuber
    action:
      - service: notify.wir_alle
        data_template:
          title: YouT
          message: >
            📺 Neues Video! {{ state_attr('sensor.youtuber1', 'friendly_name') }} ➡ {{ states('sensor.youtuber1') }} ({{ state_attr('sensor.youtuber1', 'published')[11:16]  }} Uhr am {{ state_attr('sensor.youtuber1', 'published')[:10] }})
# trying there to also add a photo to the mssage, but this does not work
          # data:
            # photo:
            #   - url: {{ state_attr.('sensor.youtuber1', 'entity_picture') }}
            #     caption: "Something"

What am I doing wrong? I suggest there is something wrong with trigger.entity_id, but I cannot seem to figure out what it is.

Thank you for your ideas :slight_smile:

Try this:

"{{ state_attr('trigger.to_state', 'friendly_name') }}"

Remove the single-quotes delimiting trigger.entity in your template. In other words, change from this:

'trigger.entity_id'

to this:

trigger.entity_id

Here’s an example (that works):

- alias: 'Demo 44'
  trigger:
    - platform: state
      entity_id: input_boolean.master
    - platform: state
      entity_id: input_boolean.toggler
  action:
    - service: persistent_notification.create
      data_template:
        message: "Demo triggered by: {{state_attr(trigger.entity_id, 'friendly_name')}}"
        title: Demo triggered

Screenshot%20from%202019-10-01%2009-26-43

Screenshot%20from%202019-10-01%2009-23-56

3 Likes

D’oh! That is so obvious, yet I didn’t think of it. Thank you for your help :slight_smile:

EDIT: oh, and by any chance, do you know what’s wrong about my the part that I commented out? It’s supposed to attach the preview picture to the message as well, but will not work. It did not even work with the hard-coded version, so this had nothing to do with removing the single-quotes.

I had tried data_template instead of data (even though data was already nested in data_template), but neither made a difference. I have a similar automation looking like this that works

action:
  - service. notify.j
    message: "You need to hydrate."
    data:
      photo: 
        - file: /config/www/images/glass_of_water.png
           caption: "H2theO!"

Sorry, I don’t have any experience with photos in notifications. My only suggestions (more like guesses) would be:

  1. Ensure this template returns a valid URL:
    {{ state_attr.('sensor.youtuber1', 'entity_picture') }}
  2. Although it may not make a difference, try removing the leading hyphen for url and delimiting the template with double-quotes.
    action:
      - service: notify.wir_alle
        data_template:
          title: YouT
          message: >
            đź“ş Neues Video! etc
          data:
            photo:
              url: "{{ state_attr.('sensor.youtuber1', 'entity_picture') }}"
              caption: "Something"

If that doesn’t fix it, someone with more experience will have to help you with it. Good luck!

1 Like

The double quotes were missing, now all notifications looks as expected :slight_smile: