Control Kodi audio output and more

I have 2 raspberrys. One is with home assistant, and the other is with Kodi libreELEC.
They both work fine and now I am looking to do some cool automations with them.
First thing I want to do is have my bluetooth speaker wake me up in the morning by using a Kodi playlist I am gonna add later.
My setup is this:
The kodi is connected to my TV with HDMI so all sounds are coming from it.
I also have a bluetooth speaker I want to use.
Kodi is connected to this too and works perfectly when I select is as audio output.

-My goal is to have home assistant execute this automation:

  1. when the time is 08:00
  2. turn on Kodi
  3. set the volume to 0.1
  4. change the audio output from HDMI to Bluetooth
  5. play a song/playlist
  6. delay 00:20:00
  7. stop the song/playlist
  8. change the audio output from Bluetooth to HDMI
  9. turn off kodi

I guess this can be done by executing a script instead of an automation but that is something I will decide later.
For now, I want to know:
what kind of communication is homeassistant using to talk to kodi? Is it tcp? Are the commands by JSON format? Where can I find a complete command list to use with kodi? My first step is to create an alarm automation but later on, I will think of more uses for kodi.

This is a good place to start:

I reckon the Bluetooth speaker is the only thing in your list that could be fussy.

Did you achieve your goal? I’m looking for a simular code…

No, I haven’t.
I have managed to send other types of commands in Kodi, and they work pretty fine. For example play or stop media, navigate through the menus etc but nothing on audio output.

Here is the link with the command list I found. It may be useful to you. If you figure out what command should be used to send, please share it.

https://kodi.wiki/view/List_of_built-in_functions

To help even further, the file with the audio settings can be found in
\\yourkodiIP\Userdata\guisettings.xml
And the setting is here:

<settings>
...
...
...
...
    <audiooutput>
...
...
...
        <audiodevice>PULSE:Default</audiodevice> <<<you may have "analogue" or something else here.
...
...
...
    </audiooutput>

So if you find a way to trigger the audio output using a command from the raspberry, you are awesome!

Thank, I’m gonna take a look at it :+1:

I found the solution to this!

I googled around some more, and came to find the command needed to be sent from a shell command:

curl -v -H "Content-type: application/json" -X POST -d '{"jsonrpc":"2.0","method":"Settings.SetSettingValue", "params":{"setting":"audiooutput.audiodevice","value":"TYPETHEDEVICEHERE"},"id":1}' http://kodiIP:kodiWEBPORT/jsonrpc -u kodiuser:kodipass

This is my shell.yaml:

kodi_audio_analogue: >
  curl -v -H "Content-type: application/json" -X POST -d '{"jsonrpc":"2.0","method":"Settings.SetSettingValue", "params":{"setting":"audiooutput.audiodevice","value":"PI:Analogue"},"id":1}' http://192.168.1.5:8080/jsonrpc -u user:pass'

While the above command is working to change the audio source, I have another problem. If kodi restarts (happens a lot, for numerous reasons), then the bluetooth is disconnected and never automatically reconnects. Other devices are reconnecting. This is only happening in KODI. I know this isnt HA related, but I thought you guys might know something. I have already searched on google and while there are some workarounds, none of them worked for me.
I have a thought on this:
If I find the correct command, I can make a script to make kodi reconnect with the bluetooth device.