Get latest video ID from Youtube channel and play Youtube video on Kodi

I was looking for a way to play the latest video from a Youtube channel on Kodi just with one press in Home Assitant. I couldn’t find anything so i put something together. I am not a coder so it is possible this can be done much easier. But it works for me so i’m happy and i like to share it!
For me it is even a mirracle it works!:slight_smile:

In short what happens is; we retrieve the latest video id from a youtube channel trough the youtube data api and save this id together with a command to a text file. Then we run the command in the text file using the shell_command in Home Assitant.

Here are the instructions to set it up:

The final result is something like this:

If there are any questions i will try to answer them.

3 Likes

Neat idea and well explained in your readme on the git. Nice work, man!

Thank you @rpitera !

I think it would be awesome to somehow combine the code. So you can get the latest id and send the video with one command.
Then it should also be possible to use something as an input_select to set the host. So you can choose which Kodi instance (if you have multiple) plays the video.

And then there is no need to setup cronjobs. But using cronjobs can be an advantage

But i think that goes above my head :cry:

So i dived into this and apparently it wasn’t that hard to accomplish.
I updated the code a little bit. For who is interested you can find the new files here:


I added a small explanation on how to use this code, maybe i will add more detail later.

The advantages of this version are:

  1. You now only need 1 youtube_to_kodi.php file (not one for every channel);
  2. You can execute the code with one command;
  3. The ip adress for the Kodi host now needs to be given in the command. (also port number)
  4. This makes it also possible that you can use Input_select.
  5. No need for cronjobs and text files anymore, because you send the channel id and it will retrieve the latest video ID when you activate the script.
  6. In the command you also need to give a number to choose if you want the latest video of a channel or one of the four before the latest video.

Notice that the kodi username and password still are harcoded in kodi-cli2 so you may have to change that.

This is how it looks now in Home Assistant:

3 Likes

thanks mister-espria,

Can you place also the home assistant .yaml config?
Better for my idea how it works.

yes of course here you go:

shell_command:
  play_roman: 'php -f /home/pi/youtube_to_kodi.php UC-SV8-bUJfXjrRMnp7F8Wzw {{states.input_select.kodi_instance.state}} 8080 {{states.input_select.yt_offset.state}}'

  
input_select:
  kodi_instance:
    name: kodiselect
    options:
     - 192.168.1.25
     - 192.168.1.8
    initial: 192.168.1.25
    icon: mdi:kodi
  yt_offset:
    name: Video Offset
    options:
     - 0
     - 1
     - 2
     - 3 
     - 4
    initial: 0
    icon: mdi:code-equal

Then i have also a script for each channel like this:

watch_romanatwoodvlogs:
  sequence:
   - service: shell_command.play_roman

And in the group.yaml i have:

Youtube:
- input_select.kodi_instance
- input_select.yt_offset
- script.watch_romanatwoodvlogs

and in the customization.yaml i have:

script.watch_romanatwoodvlogs:
    icon: "mdi:youtube-play"
    friendly_name: Watch Roman At Wood Vlogs
1 Like

Thanks! :grinning: That helps me a lot!

Oké nice to hear.
I am just making a small change to the youtube_to_kodi.php which make it possible to chain videos after each other using another input_select.
So you can choose to watch it directly or after the video ends.

Edit:
I got it working!
The new youtube_to_kodi.php file is here.

Now you can use this command:
php -f /home/pi/youtube_to_kodi.php UCOmcA3f_RrH6b9NmcNa4tdg 192.168.1.8 8080 0 y

New is that the command has an y at the end which stands for play video directly on kodi.
Instead of an y you can use an q to queue the video. It will then start directly after the currently playing video.

.

2 Likes