Skip to content

Kodi volume control

Sam Turner edited this page Mar 9, 2017 · 5 revisions

Overview

Using ha-bridge we will add a device that allows us to set the software volume level with commands like:

🗣️ "Alexa, set Kodi Sound to 50 percent”

🗣️ "Alexa, turn on Kodi Sound"


For those with no prior experience of HTTP requests:

  • Kodi (formerly XBMC) is a free and open-source media player software application available on most operating systems. Though it is assumed you already know what Kodi is and have installed it on a box within your local network.

  • JSON ("jay-son") is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

  • JSON-RPC an interface to communicate with Kodi using JSON. Whilst this guide focuses on the volume control, you can use the same technique for many of the methods available. See the official Kodi Wiki for more info


Kodi: Enable the web server and remote control

(off by default)

In Kodi navigate to Settings/Services/Control

💬 - you may need to change the settings level to see the available options (screenshot below is set to expert)

Kodi Services Control

Change the settings to:

  • Allow remote control via HTTP
    • Change the username to something unique
    • Set a strong password
    • Change the port number if needed
  • Allow remote control from applications on this system
  • Allow remote control from applications on other systems

💬 - you might find it easier to type the details out into a text file then paste into Kodi


Controlling Kodi using a browser

Before we set up ha-bridge to adjust the volume, we are going to change the volume using a URL and a browser to make sure that side of things is working correctly.

💬 - this next section might seem a little long winded and if you know what you are doing then feel free to jump ahead

1. Can we access the web server on Kodi

Open a browser in an incognito window and type in the ip address of the Kodi box and the port number you noted in the previous step (making sure Kodi is running).

If you are opening the browser on the same box you are running Kodi the URL would be http://localhost:8080

If Kodi is running on another computer you will need the ip address http://http://192.168.1.123:8080

You should see a box pop up saying "Authentication Required"

Click cancel

2. Including our login details in a URL

We need to log into the Kodi web server without having to fill in the popup each time. There is a way you can do this by putting the username and password in the URL.

⚠️ - It is generally not a good idea to use this method as it does put your password in clear view, but for your local network it is fine. If you are accessing your Kodi box from outside the local network it is assumed you already know what you are doing.

3. Can we access JSON-RPC on Kodi

Use the example below replacing the relevant sections with the details that you defined in the Kodi settings screen in the first step.

http://KODI_USERNAME:KODI_PASSWORD@192.168.1.123:8080/jsonrpc

Paste in your constructed URL to the browser.

If all is working as it should you see a page like this:

JSON in the browser

If you don’t see something that looks like the code above, then go back and double check your settings.

If it doesn't work in the browser it won't work in ha-bridge

The URL is what connects you to Kodi, JSON is what is used to communicate with it.

4. The JSON command

The JSON that is used to set the volume level is:

{"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":100},"id":1}

5. Sending the JSON using a browser

Join the two together by adding ?request= to the end of the URL and then add the JSON to the end of the request. You will end up with something like:

http://KODI_USERNAME:KODI_PASSWORD@192.168.1.123:8080/jsonrpc?request={"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":100},"id":1}

Make sure something is playing in Kodi then test the combined URL/JSON in a browser changing 100 to whatever level you want to set.

Kodi should adjust the volume accordingly, try a few different levels to be sure it is working correctly.

💬 - The browser will reformat the URL each time you press return so don’t build the URL in the browser bar without making a copy first. Ideally build the URL in a text document which you can easily edit and then copy/paste each time

You should now be able to control the volume of Kodi using the structured URL you built above in a browser.

If you can’t get it to work in a browser then you won’t be able to get it to work in ha-bridge.


ha-bridge: Setting up the custom device

Open your ha-bridge configuration in your browser and open the Add/Edit tab.

Name

Give the device a unique name that doesn’t include “volume” as it will cause conflicts with the Echo’s built in volume controls. A device name of “kodi sound” works well, if you are having trouble getting Alexa to hear "kodi" correctly, you can also try "cody", though "kodi" does work.

On Items

Type

Select HTTP Device from the dropdown

Target Item

Use the same URL you used to see the big page of JSON, removing the ?request= bit from the end e.g.

http://KODI_USERNAME:KODI_PASSWORD@192.168.1.123:8080/jsonrpc

HTTP Verb

Select POST from the dropdown

HTTP Body (body args)

Expand the text field if needed and paste in:

{"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":100},"id":1}

Content type

Select application/json from the dropdown

Manage

Click Add

Dim Items

Enter exactly the same information as the On Item for all the fields

The only difference is the HTTP Body (body args) where instead of a number we are going to put ${intensity.percent} which will allow ha-bridge to replace it with the spoken value it hears.

{"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":${intensity.percent}},"id":1}

Don't forget to set the content type and Click Add

Off Items

Enter exactly the same information as the prevous two and for HTTP Body (body args) we use the same as the On Item but instead of {"volume":100} we want {"volume":0} which will mute the sound.

{"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":0},"id":1}

Add the new device

Click the BLUE button at the top of the page Add Bridge Device

You should have something that looks like this:

Link to fullsize screenshot

ha-bridge: test the device

Open the Bridge Devices and click the Test On Test Dim Test Off buttons with Kodi playing something.


Controlling the device

You can use the commands as listed in the README

🗣️ "Set Kodi Sound to 50 percent"

🗣️ "Dim Kodi Sound to 70 percent"

🗣️ "Turn on Kodi Sound"

🗣️ "Turn off Kodi Sound"

🗣️ "Turn up Kodi Sound"

🗣️ "Turn down Kodi Sound"

Enjoy

🎉


Examples of other devices you can setup

Toggle Fullscreen

Using Input.ExecuteAction you can send button presses to Kodi.

{"jsonrpc":"2.0","method":"Input.ExecuteAction","params":{"action":"togglefullscreen"},"id":1}

With a device name of "Kodi fullscreen" you would say:

🗣️ "Alexa, turn on Kodi fullscreen"

There are 200 "Actions" you can use, See the official Kodi Wiki for more info

Switching user profiles

If you have multiple user profiles in Kodi you can turn them on or off

Device Name "Tina's Profile"

💬 - It is OK to include apostrophes in possessive (“Tinas’s” and “Mother's”) names

On Items

{"jsonrpc": "2.0","method": "Profiles.LoadProfile","params": {"profile": "Tina"},"id": 1}

Off Items

To set it back to Master profile

{"jsonrpc": "2.0","method": "Profiles.LoadProfile","params": {"profile": "Master User"},"id": 1}

🗣️ "Alexa, turn on Tina's Profile"

🗣️ "Alexa, turn off Tina's Profile"


The Kodi Alexa Custom Skill

Because ha-bridge uses the Smart Home Skill Kit it can feel a little awkward saying "turn on Kodi fullscreen"

There is an excellent Skill available that allows you to use commands like:

🗣️ "Alexa, ask Kodi to play a random episode of King of the Hill"

🗣️ "Alexa, ask Kodi to pause"

🗣️ "Alexa, ask Kodi to play the movie Spirited Away"

🗣️ "Alexa, ask Kodi to skip this"

🗣️ "Alexa, ask Kodi to play season 5 episode 7 of Bob's Burgers"

🗣️ "Alexa, ask Kodi if there are any new shows to watch"

🗣️ "Alexa, ask Kodi to play a random horror movie"

🗣️ "Alexa, ask Kodi are there any new episodes of The Simpsons"

and 70+ more

kodi-alexa on GitHub

⚠️ It is fairly complicated to setup requiring developer accounts on Amazon and an AWS account. Also it requires you to forward a port using your router exposing your local network. This is perfectly safe if you know what you are doing, which means you know that port forwarding is very risky, so have setup HTTPS for a secure connection to your local network, and are using a reverse proxy on a non standard secure port.