[HowTo] for auto quit/start kodi based on TV status

Hi, if you are like me, and want to turn kodi off when tv is off and turn back on when tv is on here is a bash script for it
(it works for me on rpi2+osmc, also worked earlier with rpi+raspbmc obiously wiht a minimal change)

PS: this is for LG tv, as you can see in the code.
you can see waht is your tv with the command:

/opt/vc/bin/tvservice -n

and then replace GSM-LG_TV in the script with your version of tv.


Put this script into a file and add to crontab like:

crontab -e

and:

@reboot /bin/bash /PATH/YOUR_SCRIPT_FILE.sh >/dev/null 2>&1 &

Here is the script:


#!/bin/bash

oldcmd=""
while :
do
        cmd=$(/opt/vc/bin/tvservice -n)
        if [[ "$cmd" == *"GSM-LG_TV"* && "$cmd" != "$oldcmd" ]]
        then
                echo starting xbmc
                $(sudo systemctl start mediacenter)
                oldcmd=$cmd
        elif [[ "$cmd" != "$oldcmd" ]] ; then


                echo stopping xbmc
                $(sudo systemctl stop mediacenter)
                oldcmd=$cmd
        fi


        echo $cmd
        sleep 10
done

after you have this script running, you can disable kodi from autostart after every boot:

sudo systemctl disable mediacenter

if you need later to autostart use this:

sudo systemctl enable mediacenter
6 Likes

just a single question: how did u install cron on osmc? I’m asking this because I’m using OSMC on a raspberry pi 2, tried crontab but the command line says command not found for crontab. I tried in two different installations of OSMC Alpha 4, both on different raspberry pi 2, with the same result.

Also, thank you for this extremely useful guide! Will try when I get home

as root

apt-get install cron

as osmc

sudo apt-get install cron

What’s the use of this, since it’s not powering off the RPi? Is it use significantly less energy when mediacenter is stopped?

Its not powering rpi2 down, it just quits kodi. thats all.
not everyone need this.

Basically you free resources. Kodi uses about 20% of the CPU on the new Rpi2, besides an important amount of memory.

With my samsung tv (UE32H5500), tvservice -n always return SAM-SAMSUNG even when tv is off… Any idea to replace this command?

@Zoltan
Are you sure this is working when TV is in Standby and not physically off? I get “device_name=GSM-LG_TV” even when the TV is in standby.

I was having a problem with this script that could be reproduced by the following steps:
Turn off TV (Kodi shutsdown)
Wait sometime, like 15 minutes
Turn on TV (Kodi process starts)
Screen stays black

I assumed that this was caused by some power management option and because of that I explicitly turn on and off the screen in the script

#!/bin/bash

oldcmd=" "
while :
do
        cmd=$(/opt/vc/bin/tvservice -n 2>&1)
        if [[ $cmd == *'No device present'* && $cmd != $oldcmd ]]
        then
                echo stopping Kodi
                #Turn off Kodi
                $(sudo systemctl stop mediacenter)
                #Turn off HDMI (tvservice -n still works)
                /opt/vc/bin/tvservice -o
                oldcmd=$cmd
        elif [[ $cmd != $oldcmd ]] ; 
	then
                echo starting Kodi
                #Turn on HDMI
                /opt/vc/bin/tvservice -p
                #Turn on Kodi
                $(sudo systemctl start mediacenter)
                oldcmd=$cmd
        fi

        echo $cmd
        sleep 10
done
1 Like

Cooool !!! I will share the script with my friends Coque housse Sony Xperia Z5 Compact,Ă©tui film pas cher

I think this is a brilliant Idea, I have been looking for a solution to the occassional lockup on my CEC and i think this might just do the trick. What I am looking to do is to actually issue a reboot command when the HDMI turns back on.
I don’t expect any issues since the mediacenter is already stopped when the tv is turned off.
My issue is that the tvservice -n command returns the same result whether I have the TV on or Off (standby). I don’t see any way around this. Does anyone have any suggestions for my situation. I attempted to use the tvservice -M command and it does identify a connection / disconnection but it only echos it to the screen and I am unsure how to trap this event.

I have an LG TV too and I can’t get this to work.

If I change source from RPi I get “No Device Present” but once I turn the TV off it goes back to GSM-LG_TV even if I fully power off by switch.

Kodi does shutdown in accordance with the script, but if you keep running a service check I find it’s back up and running again about 3min later.

I’ve now utilised a hardware solution for this.

I have a USB plug into the port on the TV (just a service port that’s only powered when the TV is on). That’s tied via a voltage divider to a GPIO pin.

I use a python script to detect the GPIO low/high to know if the TV is on or off … it then starts/stops mediacenter as appropriate.

If anyone wants it just let me know.

Did anyone get this to work with LG and Samsung TV? I have the same problem that when in standby there it still reports SAMSUNG_TV

I don’t think it’s the TVs, I think the HDMI TVservice process ghost tags the device. It stays even with TV unplugged.

As I said above Ive used a hardware solution tied to a USB port on the TV.

I want kodi to start when connecting the display but I don’t want it to stop when I disconnect it.

Can I then simply remove this part?

elif [[ "$cmd" != "$oldcmd" ]] ; then echo stopping xbmc $(sudo systemctl stop mediacenter) oldcmd=$cmd

or will this be problematic in case I turn on the monitor again when kodi is already running?

First of all, thank you OP for the script.

I personally installed it as a systemd unit file so that it was not necessary to install cron.

To address the ghost tagging issue, I noticed that if the HDMI is plugged in when the RPI (3 in my case) is powered on, then it will always detect the TV, regardless of its actual state. To make it work, physically plug in the HDMI after the RPI has booted!

Sorry for reviving an old thread… hope it’s helpful
Cheers
Matt

@mat231228, would you be willing to share your systemd unit file for managing this? I’m just looking into systemd unit files and it is an intriguing topic.

Here you go; I simply copied the original one that launches Kodi and modified it.

Steps:

  1. copy the script kindly provided above in /usr/bin/monitor_hdmi.sh

  2. copy the following as /lib/systemd/system/monitor_hdmi.service

[Unit]
Description = media center application
After = remote-fs.target network-online.target mysql.service
Wants = network-online.target

[Service]
Type = simple
TimeoutStopSec = 20
ExecStart = /usr/bin/monitor_hdmi.sh
Restart = on-abort

[Install]
WantedBy = multi-user.target

  1. execute

sudo systemctl disable mediacenter

to disable automatic launch of Kodi.

  1. execute

sudo systemctl enable monitor_hdmi

to enable automatic launch of the script.

Cheers,
Matt

Thank you, Matt!