I am trying to integrate Kodi in some automations in Home Assistant (HA 0.53.1), but I can’t get Kodi to play any media.
I have two Kodi players as follows:
Both of them are visible in hass.io and I can see what is playing, change volume/mute, skip/stop - the basic stuff. However, when I add an automation and a script to play pvr or an mp3-file from my server, I get a spinning wheel with pvr then nothing, while my attempt to play an mp3-file does nothing at all.
My script looks like this:
tv_kodi_kok:
alias: Turn on the silly box
sequence:
- alias: TV on
service: media_player.turn_on
# data:
entity_id: media_player.kodi_kok
- alias: Play TV channel
service: media_player.play_media
entity_id: media_player.kodi_kok
data:
media_content_type: "CHANNEL"
media_content_id: 7
# media_content_id: smb://homeserver/music/Min%20musik/ABBA/abba%20-%201974%20-%20waterloo%20%5B2001%20remaster%5D%20-%20%5B224%20kbps%5D
# media_content_type: "audio/mp4"
media_player.kodi_kok (LibreElec on a RPi3) is always running, so I guess I don’t need the first alias in the sequence. I also installed “script.json-cec.zip” in LibreElec, but it made no difference.
Could somebody point me in the right direction on how to get this to work? I read somewhere that there is a bug causing HA to not show media_content_type while playing pvr, and I have noticed that too. But while playing mp3, the media_content_type is visible.
It works for me (but I’m not in HASSio), I have a script to turn on the TV which looks like yours:
script:
play_kodi_pvr:
alias: Enciende la caja tonta
sequence:
- alias: TV on
service: media_player.turn_on
data:
entity_id: media_player.kodi
- alias: Play TV channel
service: media_player.play_media
data_template:
entity_id: media_player.kodi
media_content_type: "CHANNEL"
media_content_id: >
{% if (now().hour < 14) or ((now().hour == 14) and (now().minute < 50)) %}
10
{% elif (now().hour < 16) %}
15
{% elif (now().hour < 20) %}
2
{% elif (now().hour == 20) and (now().minute < 50) %}
10
{% elif (now().hour == 20) or ((now().hour == 21) and (now().minute < 15)) %}
15
{% else %}
10
{% endif %}
I think the commented line turning on Kodi is the error you’re looking for
I also work with the CEC addon (in my case Kodi runs over OSMC), if you want to see my config: tv_control.yaml
**Edit: Also make sure you can call the kodi services without problem from the dev tools, it’s the quicker way to adjust the parameters for complex service calls before writing them to the yaml files, because there is no need to reload anything, and you can see the HA log to debug the errors from bad service calls)
You need to define the kodi turn_on action before trying to use it. My config, using the CEC addon to turn ON /OFF the TV & Home Cinema attached to the OSMC device is this:
My Kodi is “always on” as is the screen it is showing on, so I didn’t think I needed any turn_on_action, or am I wrong? I can see the spinning wheel in Kodi, but then it stops without playing anything. I have installed the CEC module for LibreElec, but I don’t know how it is supposed to be used.
I have another Kodi client in my network too, which is also “always on” in Ubuntu 16.04 running on an Intel NUC. For that one I need to turn the TV and receiver on though.
I have looked at the documentation about the Kodi component in Home Assistant, but I am afraid I don’t fully understand how to set it up properly for automations. I can see my Kodi machines and what they are playing in the GUI, pause, stop etc. However, conrolling it with a script seems to be an entirely different animal and that’s where I am lost.
play_kodi_pvr:
alias: Kodi spela TV
sequence:
- alias: TV on
service: media_player.turn_on
data:
entity_id: media_player.kodi_kok
- alias: Play TV channel
service: media_player.play_media
data_template:
entity_id: media_player.kodi_kok
media_content_type: "CHANNEL"
media_content_id: 7
I have tried calling the media_player play_media service in the dev tools, but I haven’t been able to figure out how to enter the service data in JSON.
The JSON is: {"entity_id": "media_player.kodi_kok", "media_content_type": "CHANNEL", "media_content_id": 7}, and it’s a valid call (works for me in OSMC).
You need to configure the addon in Kodi, and make sure your TV is CEC-compatible. And, to control it from HA, you need to activate some variables in the Kodi config (sorry, don’t remember more)
And divide the problem: one is the TV activation in the Kodi turn on action, the other is the call to the play_media service. Which is not working as expected?
Hm, I thought it sounded a bit odd that I would ned a CEC addon to control Kodi from hass.io. My kitchen Kodi is not connected to a TV, but an official 7" Raspberry Pi screen.
I tried the play_media service with the JSON arguments above, and I get the same spinning wheel as with my script.
Now, this is really weird. I found out that the channel I am calling does not correspond with the channel shown in media_player.kodi_kok. By trial and error I have now found the channels I am subsribing to.
Sorry for the misunderstanding. The CEC protocol is to control TVs. I thought you had 2 problems: to turn on the TV, and to play some media. Now I see your problem is only with the OpenElec PVR channels…
Well, the JSON RPC API for Kodi could be a little complex… but it has good docs. It’s not really a problem of HA or hassio. You could play with the Kodi API from within a Jupyter notebook, or an ipython console, or with some CURL commands in the CLI, before using it from Home Assistant, your life would be easier
I actually managed to play PVR when I finally called the right channels. I am using two Kodi machines as stated above and the channels are unfortunately not the same for both of them. Also, I have not been able to play a specific album or artist from my mp3 music collection on my Linux server (with Samba share).
If you have any tips or examples on how to get HA to play these in Kodi, it would be very helpful. Also, is there a way to extract which channels correspond to which media_content_id? I also have Radio enabled in Kodi.
You’re welcome.
If you have these mp3 albums added to your Kodi media library, you only need to know their identifiers to play them. As I said, The Kodi API is a complex, but most complete one. It requires to play a lot with it to get things done.
Like with the PVR channel ids, there are API commands to query the Kodi DB in order to obtain the necessary info to do what you want.
Also, to play internet video or audio streams, take a look to the media_extractor component in Home Assistant.
Well, in the link to my TV control yaml config you can see some. But if you’re doing complex orders, you’ll need a powerful tool to make the automations. I use the awesome appdaemon to do that.
As an example, I have an input_select and some scripts to dynamically populate it: for loading and then selecting items from the ‘recent TV shows’ and ‘recent movies’ playlists (and for the PVR channels). To receive the API call results and populate the input_select, I have this appdaemon app.
Thanks @azogue, quite advanced stuff and so far a little bit over my head, although I am trying to learn
What I am after to begin with is this:
Let HA tell Kodi to play TV or radio at the press of a button or switch or e.g. a certain channel on the kitchen tv every workday morning
Let HA tell Kodi to play a predefined playlist of mp3’s, such as e.g. “Dinner music” or random songs from a music genre with a button press or switch
Same as 2), but get music from Spotify or Tidal through Kodi addons
However, I guess I still need to learn how to extract the content_id from Kodi. I tried to call Kodi with the parameters album_id and artist_id, but couldn’t get it to find anything in my Kodi music db.
Later on I will probably like to do some of the advanced stuff with input_select etc., but to begin with pretty basic stuff.
Also, I am running a Logitech Media Server on my Ubuntu Server in order to try and accomplish multi-room functionality with Squeezelite or Squeezebox and Kodi. I have also seen examples using HA to play a random playlist from LMS.
I will look at the documentation you suggest, but as many of us unwilling to “reinvent the wheel” I was also looking for some basic examples that I could apply to my setup.