How To Control Kodi with Google Assistant

How To Control Kodi with Google Assistant

With voice automation rapidly rising, many are looking to integrate their daily activities with voice commands. We’ve received a few requests on how to achieve this with Kodi, and after looking into it, we’ve found a pretty good tool.

This tool was created by OmerTu, and you can find it on his page. The steps involved are a little advanced for a newbie, but the ReadMe file is fairly detailed and can still be accomplished if you carefully follow them step by step.

Once done you’ll be able to use commands like “Hey Google, kodi play movie [movie name]” or “Hey Google, pause Kodi”

Below is a copy of the instructions from the ReadMe file, and HERE is a link to the download.

How to setup

Disclaimer: Use on your own risk and choose complex username & password in the below steps.

A) Enable webserver access in kodi

  1. In Kodi, go to Settings > Services > Web server
  2. Set Allow remote contorl via HTTP to On
  3. Choose a port number (e.g. 8080). We will refer to that port as YOUR_KODI_PORT
  4. Choose a username and password (Important!). We will refer to those values as YOUR_KODI_USER_NAME and YOUR_KODI_PASSWORD

B) Set up a nodejs-webserver to control your kodi

We currently support three methods of how this app can be hosted.

  1. Hosting it in Glitch, a 3rd-party web-hosting service
  2. Hosting it yourself
  3. Hosting it yourself with Docker
  4. Hosting it with Google Cloud Run

The first method is very easy to set up and to maintain and also free of charge. The second method is for advanced users. You have to setup and maintain the nodejs environment yourself. But it supports multiple Kodi instances, greatly reduces latency and does not expose your kodi-webservice to the internet directly. The third method is also for advanced users. After installing docker, you can simply run our app as a production ready docker container. This is the recommended method for LibreELEC users.

B.1 Set up a webserver in Glitch

  1. Configure your router to forward YOUR_KODI_PORT.
    Note: This is needed, so your kodi can be contacted from the internet.
  2. Find your external IP address (i.e. google for ‘what’s my ip?’). We will refer to that as YOUR_EXTERNAL_IP_ADDRESS later.
    Hint: It is strongly recommended to setup a dynDNS service of your choice. (i.e. selfhost.me)
  3. Go to Glitch.com and sign in with your github user
  4. Create a new Glitch project and under advance settings choose Import from GitHub
  5. Enter this project OmerTu/GoogleHomeKodi
  6. Change Glitch project settings to private (under share > Make private)
  7. Edit the .env file in your Glitch project with the following settings:
KODI_PROTOCOL="http"
KODI_IP="YOUR_EXTERNAL_IP_ADDRESS"
KODI_PORT="YOUR_KODI_PORT"
KODI_USER="YOUR_KODI_USER_NAME"
KODI_PASSWORD="YOUR_KODI_PASSWORD"
AUTH_TOKEN="YOUR_CONNECTION_PASSWORD"

B.2 Set up local webserver

  1. Install the Node.js application server on your target computer
    Note: Minimum required Version is 6.10 (but 10 or higher for the broker feature)
  2. Choose a location, where your app will live (i.e C:\node\)
  3. Clone this repo with git or simply download and unzip the sourcecode (green button on the top-right)
  4. You now should have a folder with a bunch of files in it (i.e. here C:\node\GoogleHomeKodi)
  5. Install this app
cd C:\node\GoogleHomeKodi
npm install
  1. Create a copy of the kodi-hosts.config.js.dist file and name it kodi-hosts.config.js.
  2. Edit the file and make sure the kodiConfig and globalConfig sections match your environment.
  3. Set up your router to forward the port you just configured.
    Default: globalConfig.listenerPort: ‘8099’
  4. You should now be able to start the node server by running: node server.js.
  5. Determine the internal IP-Address or the hostname of the machine running this node server.
    (i.e. by executing ipconfig/ifconfig) We refer to that as YOUR_INTERNAL_NODE_IP.
  6. Configure your router to forward the port from step 8 to YOUR_INTERNAL_NODE_IP.
    Note: This is needed, so your kodi can be contacted from the internet.
  7. Find your external IP address (i.e. Google ‘what’s my ip?’)
    Hint: It is strongly recommended to setup a dynDNS service of your choice. (i.e. selfhost.me)
  8. The address of your self hosted node server now consists of the port of step 8 and the ip/host of step 12.
    We will refer to this address later as YOUR_NODE_SERVER. (i.e. http://omertu.selfhost.me:8099)

For Linux-Users only: Here is a systemd init config. To run it as a daemon. On a debian dist save it as /etc/systemd/system/kodiassistant.service. Don’t forget to run: sudo systemctl enable sudo systemctl enable kodiassistant.service to start the deamon on startup.

[Unit]
Description=Node.js Google Home Kodi Interface

[Service]
ExecStart=/usr/bin/node /opt/GoogleHomeKodi/server.js
# Required on some systems
WorkingDirectory=/opt/GoogleHomeKodi
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10
# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-example

[Install]
WantedBy=multi-user.target

B.3 Set up local webserver using Docker

As an alternative to (B.2), it’s possible to use a Docker container to run a local instance.

You can configure your instance simply through environment variables or a kodi-hosts.config.js inside a folder mapped to the container-file-path /config.

  1. Install the Docker engine
    • If you want to run it on a LibreELEC system
      You can simply install the offical service addon Docker
      Note: Enable the feature Wait for network before starting kodi under Settings > LibreELEC > Network
    • For all other systems please see the offical documentation
      Docker Installation
  2. Get the latest GoogleHomeKodi docker imagedocker pull omertu/googlehomekodi
  3. Now just run the production-ready docker image
    • with the use of environment variables:docker run \ –detach \ –publish 8099:8099 \ –restart always \ -e KODI_PROTOCOL=”http” \ -e KODI_IP=”YOUR_INTERNAL_KODI_IP_ADDRESS” \ -e KODI_PORT=”YOUR_KODI_PORT” \ -e KODI_USER=”YOUR_KODI_USER_NAME” \ -e KODI_PASSWORD=”YOUR_KODI_PASSWORD” \ -e AUTH_TOKEN=”YOUR_CONNECTION_PASSWORD” \ –name googlehomekodi \ omertu/googlehomekodi
    • with the use of docker-compose:
      create a file named docker-compose.yml with the following contents: version: '3' services: googlehomekodi: image: omertu/googlehomekodi ports: - "8099:8099" environment: - KODI_PROTOCOL=http - KODI_IP=YOUR_INTERNAL_KODI_IP_ADDRESS - KODI_PORT=YOUR_KODI_PORT - KODI_USER=YOUR_KODI_USER_NAME - KODI_PASSWORD=YOUR_KODI_PASSWORD - AUTH_TOKEN=YOUR_CONNECTION_PASSWORD restart: always and then just fire it up withdocker-compose up –detach
    • or with the use of the config file:
      • Create a copy of the kodi-hosts.config.js.dist file and name it kodi-hosts.config.js.
      • Edit the file and make sure the kodiConfig and globalConfig sections match your environment.
      • Run itdocker run \ –detach \ –publish 8099:8099 \ –restart always \ -v YOUR_CONFIG_DIR:/config \ –name googlehomekodi \ omertu/googlehomekodi
  4. Determine the internal IP-Address or the hostname of the machine running the docker container.
    We refer to that as YOUR_INTERNAL_NODE_IP.
    Hint: When running the docker container on the same LibreELEC-host just use the hostname libreelec as the IP-Address of your node server.
  5. Configure your router to forward the port from the step 3 (8099 is the default) to YOUR_INTERNAL_NODE_IP.
    Note: This is needed, so your kodi can be contacted from the internet.
  6. Find your external IP address (i.e. Google ‘what’s my ip?’)
    Hint: It is strongly recommended to setup a dynDNS service of your choice. (i.e. selfhost.me)
  7. The address of your self hosted node server now consists of the port of step 5 and the ip/host of step 6.
    We will refer to this address later as YOUR_NODE_SERVER. (i.e. http://omertu.selfhost.me:8099)
  8. In case you need to update to a newer version of this app later, just repeat steps 2 and 3 after executing:docker rmi --force omertu/googlehomekodi

B.4 Google Cloud Run

  1. Just click here:

C) [OPTIONAL] controlling multiple kodi instances

There are two ways of implementing the scenario of multiple kodi instances (i.e one in the living room and another in the bedroom). For both methods you will need two IFTTT-Applets with different phrases, due to the text-parameter limitation of IFTTT.

  1. Having two node servers. Each targeting one instance. (Recommended when hosting with glitch)
    i.e. the phrase living room kodi play... could target https://mylivingroom.glitch.com/playpause
    and the phrase bedroom kodi play... could target https://bedroom.glitch.com/playpause.
    • Note: Behind a single external IP the kodi instances need still to be distinguishable.
      This can be achieved in two ways:
      • Configuring both kodi instances with different ports
        (i.e. 8080 and 8090)
        or
      • Mapping with your routers port forwarding two different ports to the same port on different hosts
        (i.e. 8080 -> 192.168.100.1:8080 and 8090 -> 192.168.100.2:8080)
  2. Having one node server targeting both kodi instances. (Recommended when hosting yourself)
    The utilization of the kodi-hosts.config.js allows you to name your instances and target them individually.
    i.e. the phrase living room kodi play... could target http://192.168.100.1:8080/playpause
    and the phrase bedroom kodi play... could target http://192.168.100.2:8080/playpause).

C.2 utilize the kodi-hosts.config.js

D) Set up IFTTT with your Google Home

  1. Go to IFTTT
  2. Create a new applet: if This then That
  3. For This choose: Google Assistance
    1. Choose Say a phrase with a text ingredient
    2. In What do you want to say? enter something like:
    Kodi play movie $
    1. In What do you want the Assistant to say in response? enter something like:
    ok playing $ movie
  4. For That choose: Maker Webhooks
    1. Choose Make a web request
    2. In URL enter:
    YOUR_NODE_SERVER/playmovie?q={{TextField}}For example, if your node server address is ‘green-icecream.glitch.me’, your should enter:https://green-icecream.glitch.me/playmovie?q={{TextField}}
    1. Method: POST
    2. Content Type: application/json
    3. Body:
    {“token”:”YOUR_CONNECTION_PASSWORD“}

Check out this example to make sure your settings are correct

Now every time you say “Hey Google, Kodi play movie bla bla”, it should play bla bla on your kodi.
Note: If your external IP changes, this will stop working (consider getting a static IP address)
Tip: If you don’t want define action for every phrase separatelly, you can use Phrase broker

Setting up other actions:

For Tv show support – Next unwatched episode, follow all the steps in D, except these changes:

  • Choose a different phrase (e.g. “Kodi play an episode of $”)
  • Use this URL:YOUR_NODE_SERVER/playtvshow?q={{TextField}}

For Tv show support – Specific episode, follow all the steps in D, except these changes:

  • Choose “Say a phrase with both a number and a text ingredient” in step 3
  • Choose a different phrase (e.g. “Kodi play $ episode #”).

For this to work, when you talk to your GoogleHome, the $ part must be in the format of “[TV_SHOW_NAME] season [SEASON_NUMBER]”. Meaning the word “Season” has to be said, the tv show name must be said before it and the season number must be said after it (i.e. “okay google kodi play bla season 4 episode 1”)

  • Use this URL:YOUR_NODE_SERVER/playepisode?q={{TextField}}&e={{NumberField}}

To pause or resume kodi follow the instructions in D, except these changes:

  • Choose “Say a simple phrase” in step 3
  • Use this URL:YOUR_NODE_SERVER/playpause

To Stop kodi, follow the same instructions as pause but use this URL:

YOUR_NODE_SERVER/stop

To Mute kodi, follow the same instructions as pause but use this URL:

YOUR_NODE_SERVER/mute

To Set Volume on kodi use “Say a phrase with a number” and the URL:

YOUR_NODE_SERVER/volume?q={{NumberField}}

To Seek forward by x minutes use “Say a phrase with a number” and the URL:

YOUR_NODE_SERVER/seekforwardminutes?q={{NumberField}}

For PVR TV support – Set channel by name, follow all the steps in D, except these changes:

  • Choose a different phrase (e.g. “switch kodi to $ channel”)
  • Use this URL:YOUR_NODE_SERVER/playpvrchannelbyname?q={{TextField}}

For PVR TV support – Set channel by number, use “Say a phrase with a number” and the URL:

YOUR_NODE_SERVER/playpvrchannelbynumber?q={{NumberField}}

Phrase broker:

Instead of defining each phrase separately on IFTTT, you can use built-in phrase broker, which will parse phrase on node server. It uses regular expression for matching phrases, so it is more powerful than IFTTT’s $ and #. For setup phrase broker you need to define on IFTTT one single action (for example “Kodi $”) and direct that action to the broker URL:

YOUR_NODE_SERVER/broker?phrase={{TextField}}

If you want to use other language than english, just lang parameter to that URL:

YOUR_NODE_SERVER/broker?phrase={{TextField}}&lang=<lang_code>

where <lang_code> is name of json file in app’s broker folder without json extension (so for example “lang=en”)

If there is not your language in broker folder yet, it is easy to add a new language. Just copy en.json and name it with your language’s code. Then edit that new file and change language text in second column to your language. You can also create your variant of default language, just copy required json file, name it with your name (f.e. “en.json” -> “en_my.json”) and change texts on your own. Don’t forget to set new language code in your action (so for our example it would be “YOUR_NODE_SERVER/broker?phrase={{TextField}}&lang=en_my”). Format of one lanuage’s json file record is simple:

“<HelperHandlerName>”: “<regular expression to match and extract parameters>”

For more variants of single handler just use suffix “:<whatever_unique>”, so in case of second variant of kodiPlayMovie handler it would be “kodiPlayMovie:1”, for third “kodiPlayMovie:2” and so on. Don’t forget, that records on the top of file have greater priority than on the bottom. If broker finds match on the top, it does not continue to the bottom.

Full table with available actions>> Found HERE

To Start/resume a movie/tv episode with a delay The commands marked with *delay (above) take an optional delay parameter to delay startup of the media by the specified number of seconds.

For example:
YOUR_NODE_SERVER_/playmovie?q=Deadpool&delay=10

To Turn on/off the TV and switch Kodi’s HDMI input

  • This requires Kodi 17 (Krypton) and above
  • This also requires that both your kodi device and TV support CEC commands
  • You need to install this script.json-cec add-on: Download and move it to your kodi device and then in Kodi choose Settings>Add-ons>Install from zip file > choose the zip your just downloaded.
  • Finally you can use this in 2 ways:
    • Turn on: Add another command: follow the same instructions as pause but use this URL:
    YOUR_NODE_SERVER/activatetv
    • If host your server on Glitch, add the following line to your .env file (see step B.1 – (5) above) and kodi will automaticly turn on the tv and switch the HDMI input everytime your trigger the main playing actions (play a move / tv show / episode / pvr channel)
    ACTIVATE_TV=”true”
    • If you host your server yourself (see step B.2 – (7) above), then add the following line at the end of your kodi-hosts.config.js file instead
    process.env[‘ACTIVATE_TV’] = ‘true’;
    • Turn off: Add another command: follow the same instructions as pause but use this URL:
    YOUR_NODE_SERVER/standbytv

How to update to the latest version

  1. Go to Glitch.com and sign in with your github user
  2. Select your Glitch project and under advance settings choose Import from GitHub
  3. Enter this project OmerTu/GoogleHomeKodi

For troubleshooting steps, you can find a details also on the github page, found here

The app and full detailed instructions were taken from Omer Tu’s github homepage. Here is the link.