Kodi on-off based on Tv status (created script, need some help)

#1

Hi, i’m new here and i would like to give my experience.

I created two scripts to start-stop kodi, each of them based on TV status. If TV is on, then kodi starts, if TV is off then Kodi stops itself.

First script is for people with a TV compatible with /sys/class/amhdmitx/amhdmitx0/hpd_state.
Second script is for those with a TV not compatible.

With my LG TV, if i give cat /sys/class/amhdmitx/amhdmitx0/hpd_state in SSH, result is 0 (tv off) or 1 (tv on).
Based on this, i made this script:

#!/bin/bash

oldcmd=""
while :
do
cmd=$(cat /sys/class/amhdmitx/amhdmitx0/hpd_state)
if [[ “$cmd” == “1” && “$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 3

done

If you are lucky and hpd_state works as expected, Kodi start-stop based on tv status(on-off) with this script. All you have to do is disable mediacenter via ssh ( sudo stop disable mediacenter ) install crontab, and add a cron with this script.

Now the second script, for those unlucky which hdp_state results always in a “1”… like me. I’ve now a Samsung TV, latest model, cat /sys/class/amhdmitx/amhdmitx0/hpd_state results always as 1, so my script can’t work.

First of all we have to enable in cec adapter the option to CLOSE kodi on tv standby.
Second, disable mediacenter (as seen before), then my second script:

#!/bin/bash
while true
do
KODI_OFF=$(ps x |grep -v grep |grep -c “kodi.bin”)

if [[ "$KODI_OFF" == "0" ]]
then
    # Kodi is off
    echo Kodi is off
          CEC_OUT=$( echo pow 0 | cec-client -d 1 -s)
    
    if [[ $CEC_OUT == *"power status: on"* ]]
    then
        # TV is on
        echo TV is on
        
        # start Kodi
        sudo systemctl start mediacenter
    fi
  
fi
sleep 5

done

For this script to work i need some help. It works as expected only first time. The cause is that when Kodi exit (forced or with cec control), it restarts after 10 seconds, and the script cannot work anymore as expected.

So, is there a way to completely disable auto restart of Kodi when exit?

I hope this helped someone, and someone could help me.

#2

There will probably be some other kind of sign that the TV has been powered down.
Check disp_cap. Does it differ when TV is on and when it’s off?

#3

I tried everything under amhdmitx0. No difference when tv off on, i suspect because the TV doesn’t real power off but only standby, as many modern tv do.

That’s why i tried to created second script, but i need to disable kodi restart on exit. I cant simply stop mediacenter with cec status, because if i use cec to control tv status, cec remote in kodi doesn’t work properly.

The only workaround i’ve found should be disable automatic restart of kodi when exit. Is there a way?

#4

If Kodi is exiting when the TV is turned off; then it’s possible to tell when the TV is off.

You need to call sudo systemctl stop mediacenter to stop Kodi from restarting.

#5

I know this. But the problem is, i can exit Kodi with the internal kodi cec (after modifying peripherals.xml). So: tv off, kodi-cec commands kodi to exit, but then it restart itself again and i have no chance to automate sudo systemctl stop mediacenter command in this way.

Only way to automatic execute that command is in my FIRST script, and it needs the Vero to recognize tv status (on off), not working with my tv.
That’s why i need to know how to prevent kodi to restart when regular exit. I see the mediacenter service has a restart script, so i tried to comment it, but it continue to restart.

For now what i can do is exit Kodi (with kodi internal cec modified), and it restarts. If i’d use cec (as in second script) to control tv status (on-off) it breaks internal cec, and i shouldn’t use my tv remote.

#6

I want to share, i solved second case.

i created another script that run together with the other.

One script is the last on first post and it does this:

IF KODI == OFF:
IF TV == ON:
THEN --> START Kodi

second script needed does this:
IF KODI == OFF: (closed with internal kodi cec)
(before it restarts by itself)
THEN – STOP Kodi service

second script is:

#!/bin/bash
while :
do
KODI_ON=$(ps x |grep -v grep |grep -c “kodi.bin”)

if [[ "$KODI_ON" == "1" ]]
then
    # Kodi is on
    echo Kodi is on
          
    
            elif [[ "$KODI_ON" != "1" ]] ; then

                # Kodi is off
        echo Kodi is off
        
        # stop Kodi
        sudo systemctl stop mediacenter
    fi

		sleep 5

done

I hope this should be helpful for other users.

#7

Take a look at /usr/bin/mediacenter, the script that controls the restart.

There is a while loop starting in line 116, so Kodi will endlessly restart itself after exiting (except for exit code 64 or 66)

I put my modification in there, calling another script that does my cec stuff and parses the output to determine in my case if Kodi should be active. If not it sleeps for some time. If conditions are met then this script just exists and the mediacenter script continues with the restart. Kodi itself exists on screensaver kicking in, so I can keep it active just in case.

Basically I am delaying the start of the Kodi binary until certain conditions are met.

Currently trying to figure out how to process a keypress on the Vero RF remote here (never played with LIRC). So I can turn on everything and switch to Kodi with a double tap of the home button. Also wonder if I can switch the power LED of the vero to red somehow programmatically…

#8

By the way, i think i made an useless double script…

I correct initial script and it should work as expected. TV off --> Kodi Off ; Tv on --> Kodi On
This is possible only after modify peripherals.xml file as suggested in other topic here in the forum, and add option to close Kodi on tv stanbdy via CEC.

#!/bin/bash
while true
do
KODI_OFF=$(ps x |grep -v grep |grep -c “kodi.bin”)

if [[ “$KODI_OFF” == “0” ]]
then
# Kodi is off
echo Kodi is off
$(sudo systemctl stop mediacenter)
CEC_OUT=$( echo pow 0 | cec-client -d 1 -s)

if [[ $CEC_OUT == *"power status: on"* ]]
then
    # TV is on
    echo TV is on
    
    # start Kodi
    sudo systemctl start mediacenter
fi

fi
sleep 5
done

#9

If you edit the watchdog it will be overwritten on updates.

You can do this with systemd.

If you clarify your use case I can give you some more pointers.
/sys/class/leds/led-sys/brightness will let you do that.

Sam

#10

Yeah, I am aware of this. I am just not comfy with systemd to be honest, therefore I chose the quick and dirty way here. Need to read up on how to do stuff ‘properly’ here.

Regarding the LED: Basically I just want to have a visual cue when Kodi is not running and maybe some other conditions are not met (e.g. no Internet access). Need to play with it and check what is possible here. Again need to read up on this topic as I never dealt with LEDs on Linux.

#11

You can make it red when kodi is off and blue when it should be running

#12

Hi all,

I just noticed this thread and thought I could provide some help.

If you are trying to use cec-client commands while kodi is running, then your tv remote will stop working in kodi as only one resource can access libcec at a time. Using cec-client will kill the process within kodi.

To workaround this, the following useful plugin for kodi exists, which exposes libcec functionality via JSON, so you can send a web request to turn your tv on or off:

In my case, I can turn my tv off or on from my phone. However, instead you could use this functionality to replace some of the cec-client script commands above using a curl cmd such as:

curl --header 'Content-Type: application/json' --data-binary '{"jsonrpc":"2.0","method":"Addons.ExecuteAddon","params":{"addonid":"script.json-cec","params":{"command":"activate"}},"id":1}' http://kodi/jsonrpc

Thanks,

Matt

#13

I’ll give it a look.
Can this script check tv status inside Kodi with CEC, without interfer with internal kodi-cec ?

If one should cec control status of tv when Kodi is started, without interfer with internal kodi cec, we can make a script that tells kodi to stop mediacenter when tv is off (instead close Kodi, that automatic restarts). And when kodi is closed, standard cec control to view status to run Kodi when tv is on.

#14

I made some tests:
with my tv, activate commands works, standby doesn’t work. By the way i don’t need these two commands. I only need to check if tv is on or off, with cec, when Kodi is active, without interfer with internal kodi cec.

#15

i dont have amhdmitx folder in /sys/class

#16

It’s a two year old thread. If you have any problem with OSMC please create new post with debug logs.

1 Like