Get alerted when someone subscribes to your Youtube channel or likes a video

Hi guys, are there any integrations or config out there that can alert you when someone subscribes to your Youtube channel or even likes one of your videos? I would like the ability to create an automation to play a message through my Google mini’s when someone subscribes to my channel.

I have tried searching for such integration but was unsuccessful in finding anything.

Use the Youtube API, Subscriptions: list | YouTube Data API | Google Developers.
Go watch any of the videos with a live physical display that shows likes or subscribers and you will see the basics of it.
Then it’s just a matter of adapting it to HA.

Edit wrong API.
This is the one: Channels: list | YouTube Data API | Google Developers

Just fill it out like this:

and you should get:

I masked it because I don’t know if it’s sensitive or not. But most likely not since it’s part of the open API.

If you click on the show code you get the CURL command that you can use in HA, but you need to create an Auth code.

Auth code can be found here:
Obtaining authorization credentials | YouTube Data API (google.com)

1 Like

Thank you I will have a look at this. It is a bit over my head but will at least try to see what I can do with this. I will be sure to share here should I make headway with it. In the meantime, if there is anyone out there that can assist, please feel free to add your comments.

Something like this:
But you still need to get the API key and Auth token from the link above.

sensor:
  - platform: rest
    name: youtube_api
    resource_template: "https://youtube.googleapis.com/youtube/v3/channels?part=statistics&forUsername=[YOUTUBE_USERNAME]&key=[YOUR_API_KEY]'"
    method: GET
    headers:
      authorization: 'Bearer [YOUR_ACCESS_TOKEN]'
      content-type: 'application/json'
    value_template: '1' # fake value, we only need the attributes
    json_attributes:
      - "items"
    scan_interval: 3600 # one hour

And from this then you can use the attribute subscriber_count in an automation

1 Like

You are an amazing help thank you so much! I will give this a try as soon as I can and post back the results.

Hi again, I have managed to generate an API through the developer console but I am a bit confused on what to do with the auth token? I went to create a new Oauth client ID but not sure what to type into the “Authorised redirect URIs” field. should it just be youtube.com?

Thanks

Not sure actually.

I’m a bit busy to test it myself, but this is what the docs say:

Ok…

I think I got it.
Use the key you masked like this: notice the &key= and replace the rest with your key

(the API key is already deleted)

https://youtube.googleapis.com/youtube/v3/channels?part=statistics&forUsername=TheHellis&key=AIzaSyDbQODRIFUndevn--WB-PxesGQU-ap38ek

And I get:

Opening the weblink that is partially masked gives this:

After enabling: BOOM! :slight_smile:

That should mean we can simplify the sensor to:

And using the link we just tried in the browser as resource_template

sensor:
  - platform: rest
    name: youtube_api
    resource_template: "https://youtube.googleapis.com/youtube/v3/channels?part=statistics&forUsername=[YOUTUBE_USERNAME]&key=[YOUR_API_KEY]'"
    method: GET
    headers:
      content-type: 'application/json'
    value_template: '1' # fake value, we only need the attributes
    json_attributes:
      - "items"
    scan_interval: 3600 # one hour

with removing the auth token.

Perfect, thank you. It seems mine is working but not giving me subscriber count. I only have 1 subscriber at the moment but I thought that it should still show that info.

So what does it give you?
What does it look like if you paste it in the browser? Do you see the json as pictured above or is “items” missing?

I just noticed if I try this on “Ozzy Man Reviews” I get this

image

“OzzyManReviews” however:

I got that result you pasted first. My channel name is ResetResto so not sure what I am doing wrong.

Instead of &forUsername=…

use:

&id=UCOSx30ko605VZI4f0mdMlYw

You are absolute legend!!! it worked. Thank you thank you thank you!!!

1 Like

Last question I promise. I added the sensor to my configuration.yaml file and I can add the sensor as an entity in Lovelace. It obviously only shows the fake value “1”. How would I display the attributes in the entity card? I am guessing this is some more yaml config right?

Yes isn’t there always a need for more yaml and configuration? :wink:

  - platform: template
    sensors:
      youtube_subscriber_count:
        friendly_name: "Subscriber count"
        value_template: >-
                        {% if state_attr('sensor.youtube_api', 'items') != None %}
                        {{ state_attr('sensor.youtube_api', 'items').statistics.subscriberCount }}
                        {%- endif %}

  - platform: template
    sensors:
      youtube_view_count:
        friendly_name: "View count"
        value_template: >-
                        {% if state_attr('sensor.youtube_api', 'items') != None %}
                        {{ state_attr('sensor.youtube_api', 'items').statistics.viewCount }}
                        {%- endif %}

Should give you two sensors with the views and subscribers.

You can try the template in developer tools first to make sure they work as expected.

Thanks again for the help. I am obviously doing something wrong but I took your first template and pasted into the developer template window and it just outputs the code not the result. I checked that the sensor.youtube_api is available under the states section but it only shows friendly_name as an attribute. see screenshots below:

This seems to show the sensor.youtube_api as empty.

hmm…
I just tried the json in the template tool

The thing is that the sensor should get the “items”.
But apparently it doesn’t. I’m lost now.

But your count and view sensor needs the [0] also as you can see above. But that won’t solve the issue since sensor.youtube_api is empty

Just a wild guess here to see if anything at all works.
Replace the fake value with "{{ value_json.["items"][0].statistics.subscriberCount }}" and restart.

It does not like the format with the quotes. I tried single quotes but that also gave an error.

Sorry…
"{{ value_json.['items'][0].statistics.subscriberCount }}"

Jeeze I feel like I am so waisting your time now but that too gave an error.