Extract data from a sensor and pass it to a message

The YouTube sensor produces the following when a new video is uploaded to a specified channel :

url: https://www.youtube.com/watch?v=JWFlSp2SXox
published: 2020-07-28T08:09:04+00:00
live: false
friendly_name: channelname
icon: mdi:youtube
entity_picture: https://i3.ytimg.com/vi/JWFlSp2SXox/hqdefault.jpg

I want to use that URL in an automation and am testing by simply forwarding it as a message. I realise I’m getting the formatting of the URL wrong and wondered if anyone has any pointers. There’s not a lot of documentation or importantly examples of this type of sensor.

Below is what I’ve blindly guessed by reading other similar scripts :

alias: ytnotification
description: ‘’
trigger:

  • entity_id: sensor.ytchannel
    platform: state
    condition: []
    action:
  • service: notify.HAnotification
    data_template:
    title: “YouTube”
    message: {{states.sensor.ytchannel.url}}

Please see point 11 here then edit your post accordingly: How to help us help you - or How to ask a good question

message: "{{ states('sensor.ytchannel.url') }}"

That’s sending the message “unknown”.

I’ve tried adding attributes also:

message: “{{ states(‘sensor.ytchannel.attributes.url’) }}”

What do the sensor’s attributes look like in the developer tools states menu?

If url is an attribute (including the correct lower/upper letter case) then the correct format is:

message: "{{ state_attr('sensor.ytchannel', 'url') }}"
1 Like

Thanks, that format worked, you’re a star!. HA syntax is such headache, I’ve been using it for 3 weeks and still havent got my head around it and often cheat a little by buillding partly using the automation builder.

1 Like

Practice, practice, practice. You’ll pick it up.

Bookmarking a few examples for reference is a good idea too.

Yeah, my hass bookmarks folder is growing daily!