[HowTo] Start/Stop Kodi with your remote (headless mode)

Ok so I got this finally working and would like to share my solution. My goal was to control the Vero 4K+ with my Logitech Harmony Elite remote, connected via bluetooth for best responsiveness. I also wanted to put the Vero into some kind of “suspend mode” when it is not used by any Harmony activity so I can safely let my Windows Home Server shut down when I’m done watching movies (the server hosts the Kodi mySQL movie/music database and media files).

First thing of course is you have to add the Vero to your list of devices in the MyHarmony app and set up the bluetooth connection. To do so, add Entertainment Device and type Microsoft in the manufacturer field and Kodi in the model field (I’m guessing the english terms here because I am german and use the german version of the app). When it asks to try to turn on the device answer yes and yes again (this is just to complete this step). When it asks to add an activity answer no. Now go into operation settings and choose the first option (turn off when not used), tap next (>) and choose the two buttons option. In the next step, choose the “Lock PC” button for turning the device on and “Win+4” for turning off (I verified these buttons are not used otherwise in Kodi on the Vero).

Next open the myOSMC app on your Vero and start Bluetooth discovery as described here: Connecting A Bluetooth Device - General - OSMC - click on Enable Bluetooth, click on Start/Stop Discovery (the 4K+ has built in bluetooth so no additional hardware is needed). Leave this screen open. Now add the Kodi device to an activity. MyHarmony will now try to establish a bluetooth connection. You will have to wait a little but eventually “Harmony Keyboard” should show up under available devices in myOSMC. Select it and click Ok. Choose “Pair with PIN” and shortly after that the Harmony should be listed under paired devices. I know pair with PIN sounds wrong and actually no PIN is shown or requested anywhere in the process but “Pair without PIN” didn’t work for me. Anyway you should now already be able to control Kodi with your Harmony, the basic keys should be working. I highly recommend to install the Keymap Editor Add-on to customize the Harmony buttons for specific Kodi commands.

IMPORTANT: You now have to edit the activity you just created in MyHarmony. Edit start sequence and using “add step” add an 1 second delay and then add the “Lock PC” button from the Kodi device after that. The delay and the Lock PC commands should now be at the end of the start sequence. I’ll explain below.

Next we have to install some additional packages on the Vero and create/modify some scripts and configuration files. Some of these files can only be modified as root so to be frank I just enabled root (see Usernames and passwords - General - OSMC) and installed the FTP server from the myOSMC app. This way I could create/modify all the files on my Windows system and upload them to the Vero via FTP client logged in as root. Note: you have to connect via SSH and run sudo nano /etc/ftpusers and remove root from this file for this. Also if on Windows, make sure you use an editor that can create UNIX style text files (I use NoteTab Pro, Notepad++ works too). Windows Notepad will not create the correct line break format and your scripts will not run!

Next log in to the Vero via SSH and run the following commands to install packages

sudo apt update
sudo apt install triggerhappy
sudo apt install at
sudo apt install wakeonlan

Next we have to create the triggerhappy service configuration in /etc/systemd/system/triggerhappy.service like this:

[Unit]
Description=triggerhappy global hotkey daemon
After=local-fs.target

[Service]
Type=notify
ExecStart=/usr/sbin/thd --triggers /etc/triggerhappy/triggers.d/ --deviceglob /dev/input/event*

[Install]
WantedBy=multi-user.target

Next create the /etc/triggerhappy/triggers.d/osmc.conf file:

# Start Kodi via "Lock PC"
KEY_L+KEY_LEFTMETA    1    /home/osmc/scripts/mediacenter start
# Stop Kodi via "Win+4"
KEY_4+KEY_LEFTMETA    1    /home/osmc/scripts/mediacenter stop

Note: if you want to use different keys on your remote for the start/stop commands you can run thd --dump /dev/input/event* over SSH and then press the desired key on your Harmony and it will print out something like the above almost ready to use (the part after the #, also read the triggerhappy documentation). For multi-key commands like in our script however the key codes have to be reversed! That is, where the --dump parameter prints out KEY_LEFTMETA+KEY_4 the script must read KEY_4+KEY_LEFTMETA just as we did here.

As you can see we reference the mediacenter script at /home/osmc/scripts/mediacenter. I have modified @koichirose’s intitial version to read like this:

#!/bin/bash

service="mediacenter"
action=$1
declare -A led_statuses
led_statuses=([0]=red [1]=off)

case "$1" in
    start)
        grep -oP "(.*)" /home/osmc/.kodi/userdata/wakeonlan.xml | cut -d ">" -f 2 | cut -d "<" -f 1 | while read -r mac; do
          wakeonlan $mac
        done
        led_status=1
        cpu_governor="ondemand"
        sudo echo "0" >  /proc/sys/net/ipv4/icmp_echo_ignore_all
        ;;
    stop)
        led_status=0
        cpu_governor="powersave"
        sudo echo "1" >  /proc/sys/net/ipv4/icmp_echo_ignore_all
        ;;
    status)
        echo "$service is $(sudo systemctl is-active $service)"
        echo "LED status is ${led_statuses[$(sudo cat /sys/class/leds/led-sys/brightness)]}"

        for i in {0..3}
        do
           echo "CPU$i governor is $(sudo cat /sys/devices/system/cpu/cpu$i/cpufreq/scaling_governor)"
        done
        exit 0
        ;;
    *)
        echo $"Usage: $0 {start|stop|status}"
        exit 1
esac

echo "Action is $action"

echo "Setting LED to ${led_statuses[$led_status]}"
sudo echo $led_status > /sys/class/leds/led-sys/brightness

echo "Setting CPU Governor to $cpu_governor"
for i in {0..3}
do
    sudo echo $cpu_governor > /sys/devices/system/cpu/cpu$i/cpufreq/scaling_governor
done

echo "Running 'sudo systemctl $action $service'"
sudo systemctl $action $service

I have basically added sending a WOL magic packet to any network servers configured in Kodi before starting Kodi, hopefully shortening the wait time by a few seconds. Also in the Stop/Start sections I disable/enable the Vero’s reply to ping requests. This should be enough to basically “hide” the device on the network and use that to run scripts/actions on other devices based on that. In my case it is necessary for the “Lights Out” Windows Home Server addon that monitors the network and shuts down the server based on several conditions, one of which is that the Vero is “down”.

Don’t forget to make the script executable by chmod +x /home/osmc/scripts/mediacenter or use your FTP client to change the permissions.

Almost done. We now have to create a so called UDEV rule for the Harmony. Create the file /etc/udev/rules.d/50-harmony.rules with the following contents:

KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{address}=="ha:rm:on:ym:ac:00", RUN+="/usr/bin/at -M -f /home/osmc/scripts/runthd.sh now"

You have to modify the "ha:rm:on:ym:ac:00" bit to match your Harmony Hub MAC address. The MAC address is printed at the bottom of your harmony hub, there’s a “WIFI MAC address” label where the MAC address is printed like AABBCC112233 (yes the WIFI and Bluetooth MAC address is the same). So you would modify that line to read

KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{address}=="aa:bb:cc:11:22:33", RUN+="/usr/bin/at -M -f /home/osmc/scripts/runthd.sh now"

IMPORTANT: the MAC address has to be in lower case!

You can also find the MAC address over SSH on the Vero. Make sure your Harmony is paired with the Vero and run udevadm info -a -n /dev/hidrawX where X can be 1, 2, 3 and so on. On my Vero it was hidraw2. Look for a text block like this:

  looking at parent device '/devices/c11084c0.serial/tty/ttyS1/hci0/hci0:12':
    KERNELS=="hci0:12"
    SUBSYSTEMS=="bluetooth"
    DRIVERS==""
    ATTRS{address}=="c8:db:26:0d:86:e5"
    ATTRS{type}=="ACL"

The part afterATTRS{address}== is the MAC address and you can just copy and paste that.

At last, we have to create the script referenced above, /home/osmc/scripts/runthd.sh:

sleep 0.5
systemctl restart triggerhappy

Note: this script does not have to be made exeutable.

At last, restart your Vero from over SSH with reboot or from the Kodi shutdown menu.

OK, that’s it! Now when you start/stop the Kodi activity on your Harmony, Kodi on the Vero should start/shutdown, the LED should turn off/on and the device should start/stop responding to ping requests.

DISCLAIMER: I’m by no means a Linux expert and all of this “knowledge” was acquired over the holidays mostly by googling and reading over at Stackexchange. Kudos to @koichirose for the original idea. All of the above runs as root on the Vero so be careful.

Technical background: The “turning off part” is actually easy. The Harmony is connected and the triggerhappy service is monitoring key strokes. When the “Off” button is pressed the mediacenter stop command is executed and all is well. The problem is that now the Harmony “unplugs” the “Harmony Keyboard”. When you start the activity with the Vero again it “plugs back” the “Harmony keyboard” but because of the unplug/plug the triggerhappy service is no longer intercepting the Harmony key strokes. That’s why I created the UDEV rule, it just restarts the triggerhappy service every time there’s a plug/unplug event for the Harmony with a slight delay. Now the modification to the activity start sequence above makes sure the triggerhappy service receives the “On” key stroke again after it is restarted. YMMV, it works perfectly in my setup.

The scripts/files with proper file structure for easy FTP upload to the Vero can be downloaded here: Cloud. Remember the /home/osmc/scripts/mediacenter script has to be executable, all other files have to be world readable.

Have fun! :slight_smile: