Using RSS event data in template sensor

I have set up a Feedreader integration which pulls from a BBC News RSS and sends new entries as events. I’m trying to figure out how to create a template sensor that includes the latest mp3 URL so that I can have Home Assistant send it to my speakers, but am getting stuck with the configuration of the template sensor.

Listening for the Feedreader event gives this result:

event_type: feedreader
data:
  id: urn:bbc:podcast:w172z2qqmccqkxv
  guidislink: false
  title: 29/03/2023 15:01 GMT
  title_detail:
    type: text/plain
    language: null
    base: >-
      https://podcast.voice.api.bbci.co.uk/rss/audio/p002vsmz?api_key=Wbek5zSqxz0Hk1blo5IBqbd9SCWIfNbT
    value: 29/03/2023 15:01 GMT
  summary: The latest five minute news bulletin from BBC World Service.
  summary_detail:
    type: text/html
    language: null
    base: >-
      https://podcast.voice.api.bbci.co.uk/rss/audio/p002vsmz?api_key=Wbek5zSqxz0Hk1blo5IBqbd9SCWIfNbT
    value: The latest five minute news bulletin from BBC World Service.
  subtitle: The latest five minute news bulletin from BBC World Service.
  subtitle_detail:
    type: text/plain
    language: null
    base: >-
      https://podcast.voice.api.bbci.co.uk/rss/audio/p002vsmz?api_key=Wbek5zSqxz0Hk1blo5IBqbd9SCWIfNbT
    value: The latest five minute news bulletin from BBC World Service.
  content:
    - type: text/plain
      language: null
      base: >-
        https://podcast.voice.api.bbci.co.uk/rss/audio/p002vsmz?api_key=Wbek5zSqxz0Hk1blo5IBqbd9SCWIfNbT
      value: The latest five minute news bulletin from BBC World Service.
  itunes_explicit: false
  authors:
    - name: BBC World Service
  author: BBC World Service
  author_detail:
    name: BBC World Service
  links:
    - rel: alternate
      type: text/html
      href: http://www.bbc.co.uk/programmes/w172z2qqmccqkxv
    - length: "5000000"
      type: audio/mpeg
      href: >-
        https://open.live.bbc.co.uk/mediaselector/6/redir/version/2.0/mediaset/audio-nondrm-download/proto/https/vpid/w1mskdkwnqxb7rr.mp3
      rel: enclosure
  link: http://www.bbc.co.uk/programmes/w172z2qqmccqkxv
  itunes_duration: "300"
  published: Wed, 29 Mar 2023 15:06:00 GMT
  published_parsed:
    - 2023
    - 3
    - 29
    - 15
    - 6
    - 0
    - 2
    - 88
    - 0
  feed_url: >-
    https://podcast.voice.api.bbci.co.uk/rss/audio/p002vsmz?api_key=Wbek5zSqxz0Hk1blo5IBqbd9SCWIfNbT
origin: LOCAL
time_fired: "2023-03-29T15:37:05.297344+00:00"
context:
  id: 01GWQ07YGHVK492A5PAPMTAA7F
  parent_id: null
  user_id: null

I have set up my template.yaml configuration as follows but the value of the sensor just shows as Unknown. Where am I going wrong?

- trigger:
    platform: event
    event_type: feedreader
    event_data:
      feed_url: >-
        https://podcast.voice.api.bbci.co.uk/rss/audio/p002vsmz?api_key=Wbek5zSqxz0Hk1blo5IBqbd9SCWIfNbT
  sensor:
    - name: "BBC News URL"
      state: '{{ trigger.event.data.links.href }}'

links is a list. If you are trying to get the audio/mpeg link, try this (using the safer bracket notation and note the change to the quotes):

state: "{{ trigger.event['data']['links']|selectattr('type','eq','audio/mpeg')|map(attribute='href')|first }}"

That’s a safer way than using e.g. links[1] as you can’t guarantee that the HTML and audio items will be in that order. If it’s not always audio/mpeg, then perhaps select on rel being enclosure instead.

1 Like

Thank you, this worked perfectly! I’m pretty new to Home Assistant and don’t have any coding background, so I appreciate the help.