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

As detailed here you may want to actually stop Kodi when you’re done watching and start it again later, instead of suspending it.
I did it because I had issues with the NAS waking up and I had to fiddle with the wake.py and standby.py scripts to perform operations (such as updating the Kodi library upon wake up).

So I managed to write a bash script that is executed with a remote button press (in my case, a Harmony 650). It will start/stop the mediacenter service as well as toggling the LED light and setting the CPU governor to ondemand/powersave for optimal power consumption.

You need to have basic Linux command line experience to connect to the Vero via SSH.

Create the file /home/osmc/scripts/mediacenter with the following contents:

#!/bin/bash

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

case "$1" in
    start)
        led_status=1
        cpu_governor="ondemand"
        ;;
    stop)
        led_status=0
        cpu_governor="powersave"
        ;;
    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’m not really an expert with Bash, so if someone has any suggestions please let me know.

Make the file executable with chmod +x /home/osmc/scripts/mediacenter
You should now be able to run:
/home/osmc/scripts/mediacenter start to start Kodi,
/home/osmc/scripts/mediacenter stop to stop Kodi,
/home/osmc/scripts/mediacenter status to display the current status (check if Kodi is running, if the LED is on and the current CPU governor).

You now need to map two different buttons to your remote. I mapped F3 and F4 to the yellow and blue buttons on the Harmony. I’m using Flirc with the Flirc mediacenter profile in MyHarmony. Add the yellow button to the startup sequence and the blue button to the shutdown sequence for your Vero activity.

You may use the builtin Vero IR receiver with a different profile and different keys: simply run evtest in a terminal and press the desired remote button to find out the key name. This is untested but should work.

Install triggerhappy, which is required to launch scripts with a keyboard press:
sudo apt update && sudo apt install triggerhappy

Create the file /etc/triggerhappy/triggers.d/osmc.conf with the following contents:

# start kodi on F3 - change to KEY_XX with the name of the key mapping you see with evtest
KEY_F3    1    /home/osmc/scripts/mediacenter start

# stop kodi on F4 - change to KEY_XX with the name of the key mapping you see with evtest
KEY_F4    1    /home/osmc/scripts/mediacenter stop

Restart triggerhappy:
sudo service triggerhappy restart

You should now be able to start/stop Kodi with your remote!

2 Likes

Thanks for this very useful guide! I’m trying to get this working with my Harmony remote that I have paired as a bluetooth keyboard device by setting the Vero up as a Microsoft Kodi device in the Harmony App. The pairing process in myOSMC was successful and the remote shows up as a “Harmony Keyboard”. It works very well and fast in Kodi. However I’m stuck with most likely a startup timing issue with triggerhappy and the bluetooth service. I have set up the osmc.conf file and when I log in via ssh and run

# evtest found harmony keyboard at /dev/input/event7
thd --triggers /etc/triggerhappy/triggers.d/ --deviceglob /dev/input/event7

it works and the scripts I have assigned to harmony buttons run as expected when I press the keys. However although the triggerhappy service ist enabled and automatically started when I restart the Vero it just doesn’t work by itself. I have tracked it down to the /dev/input/event7 harmony input device not being available yet when the service starts. I have already modified the triggerhappy service file at /lib/systemd/system/triggerhappy.service as follows:

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

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

[Install]
WantedBy=multi-user.target

ps -ef shows that the service is up and running, but the key presses are just not recognized. Subsequently, if I change the service command line to “…/dev/input/event7” in the service configuration file the service start fails because the harmony most likely isn’t available yet… I have also tried to start the service from rc.local but with no luck either.

Where and when do I have to start the triggerhappy service to make sure it connects to the bluetooth paired harmony? @sam_nazarko can you help? Thanks!!

After further testing I am now as far as that after a restart of the Vero I have to login with ssh once and run

sudo systemctl restart triggerhappy
and then everything works as expected. So oviously I have to restart triggerhappy “some short time” after kodi is up and running and the harmony is paired via bluetooth. How can I automate this? Is it possible to run a script somehow whenever the harmony keyboard device becomes available?

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: https://osmc.tv/wiki/general/connecting-a-bluetooth-device/ - 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 https://osmc.tv/wiki/general/usernames-and-passwords/) 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: https://c.gmx.net/@798273824461166834/65-WbHRsSri713SqiM15aA. Remember the /home/osmc/scripts/mediacenter script has to be executable, all other files have to be world readable.

Have fun! :slight_smile:

Thanks for the detailed write-up.

One thing I noticed is that you’ve modified the triggerhappy service file /lib/systemd/system/triggerhappy.service in situ. The downside of this approach iis that it might be overwritten (and therefore reset) during a regular system update. A safer approach is to create a separate file /etc/systemd/system/triggerhappy.service that contains the modified version of the file. This will take precedence over /lib/systemd/system/triggerhappy.service but won’t be changed during an update.

You can also use a systemd drop in which lets you only change the bits needed

True, but it’s easier using /etc/systemd/system. IMHO, of course. :wink:

I know this is not the priority here in this thread, but why edit the keys inside Kodi instead of remapping the buttons of your Harmony remote according to the default keyboard mappings of Kodi? It’s way more straightforward as there’s already a logic built into Kodi for those buttons and you simply have to pull up the list from the Kodi wiki and map standard keyboard keys to your Harmony’s buttons. :thinking:

Thanks a lot, I have modified the guide and the download to follow your advice. Of course this is the way to go!

1 Like

@Chillbo you are probably right. However with the Microsoft/Kodi setup some of the Harmony on-screen buttons like “Audio language” or “Subtitle” do nothing on the Vero version of Kodi. So that’s were I used Keymap to assign these on-screen buttons to switching audio language and subtitles. Because Keymap also remembers different key mappings for different “areas” in Kodi you could for instance map the Ch/Pg hardware buttons to “PageUp” and “PageDown” in movie list views where during movie play they used for chapter skip.