5

I am trying to set an script for autostarting kodi when the TV is on (not in standby mode) or when I change to the raspberry corresponding HDMI.

I found a script that should do this, but for what I've read it only works when the tv is switched off and not in standby mode.

#!/bin/bash

oldcmd=""
while :
do
    cmd=$(/opt/vc/bin/tvservice -n)
    if [[ "$cmd" == *"SAM-SAMSUNG"* && "$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

Is there any way to change the script to only start kodi when the tv is not in standby or I select the corresponding HDMI output from the TV?

Update: I've made a change in the script to make it ping the tv (as pointed by Gotschi). This is the script if anyone wants it.

#!/bin/bash

status="off"
while :
do
        count=$( ping -c 1 $yourip | grep '64 bytes' | wc -l )

        if [[ $count -eq 0 && $status == "on" ]] ; then

                echo stoping xbmc
                $(sudo systemctl stop mediacenter)
                status="off"
        elif [[ $status == "off" ]] ; then


                echo starting xbmc
                $(sudo systemctl start mediacenter)
                status="on"
        fi


        echo $status
        sleep 10
done
4
  • well, I tried several methods utilising the HDMI port - until I realised my Smart Tv has a Ethernet port. Just plug in the cable, give it a static IP and ping for the TV in your script / check some port depending on your device. Not an answer to your question, but a alternative solution...
    – Gotschi
    May 19, 2015 at 18:55
  • you can check certain ports / upnp services or even make a arp lookup. my sony also took about 10 minutes to not respond when turned off, I didn't care...
    – Gotschi
    May 19, 2015 at 19:10
  • 1
    Yes, I have made some changed and it works great! Thanks! I will add the new script to my question just in case someone needs it
    – Eylen
    May 19, 2015 at 19:31
  • Nice! I just remember fiddling around with the tvservice (hdmi-) port for a long time, but with no result...
    – Gotschi
    May 20, 2015 at 8:40

1 Answer 1

1

I come to a very similiar solution (pinging the TV static ip), and I integrated it into systemd;

this is the bash script in /home/osmc/checkHdmi.sh to be launched by the service:

#!/bin/bash

ping -c 1 -W 1 192.168.1.7 > /dev/null

if [ $? -eq 0 ]; then

    systemctl -q status mediacenter > /dev/null

    if [ $? -ne 0 ]; then
            sudo systemctl start mediacenter
    fi

else

    systemctl -q status mediacenter > /dev/null

    if [ $? -eq 0 ]; then
            sudo systemctl stop mediacenter
    fi

fi

exit 0

This is the service file in /etc/systemd/system/checkHdmi.service:

[Unit]
Description=Check if TV is on

[Service]
Type=simple
ExecStart=/home/osmc/checkHdmi.sh

and this is the timer file in /etc/systemd/system/checkHdmi.timer which launches the service every minute:

[Unit]
Description=Check if TV is on

[Timer]
OnBootSec=60
OnUnitActiveSec=60
Unit=checkHdmi.service

[Install]
WantedBy=timers.target

Remember to enable and start the timer with the following commands:

systemctl enable checkHdmi.timer

systemctl start checkHdmi.timer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.