start / stop kodi based on HDMI monitor status

Max2Play Home Forums Max2Play on Raspberry PI start / stop kodi based on HDMI monitor status

Viewing 8 posts - 1 through 8 (of 8 total)
  • 19. Januar 2016 at 9:30 #17995

    I want to have kodi start when I plug the hdmi-monitor and to stop when I unplug the monitor. I’m running the raspberry pi 24/7 because I have my lms server on it. I’m only using kodi about once a week and don’t want have kodi running all of the time. Still I want it to start in a kids friendly way (like just plugging the monitor)

    Apparently this can be done in OSMC using this method

    Would this be possible in max2play as well? What kind of packages do I need to install?

    It would be nice if this could be set with the web interface

    19. Januar 2016 at 10:35 #17996

    I also wonder how to change the script so that it turns on with display but that kodi doesn’t shutdown when I turn off the display.

    Can I simply remove this part?

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

    Or would I get into problem when I turn on the screen again and kodi is already started?

    20. Januar 2016 at 19:41 #18029

    Apparently systemctl start mediacenter and systemctl stop mediacenter doesn’t work with max2play. What command should I use to start and stop kodi so that I can adjust this script

    oldcmd=" "
    while :
            cmd=$(/opt/vc/bin/tvservice -n 2>&1)
            if [[ $cmd == *'No device present'* && $cmd != $oldcmd ]]
                    echo stopping Kodi
                    #Turn off Kodi
                    $(sudo systemctl stop mediacenter)
                    #Turn off HDMI (tvservice -n still works)
                    /opt/vc/bin/tvservice -o
            elif [[ $cmd != $oldcmd ]] ; 
                    echo starting Kodi
                    #Turn on HDMI
                    /opt/vc/bin/tvservice -p
                    #Turn on Kodi
                    $(sudo systemctl start mediacenter)
            echo $cmd
            sleep 10
    21. Januar 2016 at 16:48 #18045

    Based on your code I wrote this (in perl) it works.

    $| = 1;
    $debug = 1;
    if ($debug == 0) { open(STDERR, ">/dev/null")}
    # This will start kodi on startup if a device is on hdmi
    # or will stop kodi processes if no device is on hdmi
    $oldstat="X";    # force a change on startup
    while(1) {
        $stat=<code>/opt/vc/bin/tvservice -n</code>;
        # if there is something plugged in and there was a change then Start KODI
        if (($stat ne "") && ($stat ne $oldstat)) {
            print "starting Kodi\n";
            #Turn on HDMI
            system("/opt/vc/bin/tvservice -p");
            #Turn on Kodi
            system("sudo -u pi -H -s /opt/max2play/ >> /dev/null 2>&1 &");
        # if there is nothing plugged in and there was a change then Stop KODI
        if (($stat eq "") && ($stat ne $oldstat)) {
            print "stopping Kodi\n";
            #Turn off Kodi
            system("pkill -9 -f \"kodi.*standalone\"");
            #Turn off HDMI
            system("/opt/vc/bin/tvservice -o");
    sleep 10;
    22. Januar 2016 at 18:41 #18093

    Hi asplundj and noop,

    thank you for your posts.

    This feature is a great idea and we will include it in future versions of Max2Play.

    : Thank you for your script, your support is appreciated and will be the used for improving the Kodi interface of Max2Play.

    Maximilian from Max2Play

    12. Februar 2016 at 11:14 #18468

    Hi noop and asplundj,

    thanks for sharing your scripts!

    If you like to add this feature to Max2Play you could contribute to the Github project of Max2Play.

    There is one thing, that could be improved to use the capabilities of the Max2Play-Framework: Max2Play automatically runs the script „/opt/max2play/“ as cronjob every minute. This script looks for autostart parameters and can be used to run your script too (instead of doing the loop every 10 sec and permanently running the script).

    In your scenario it would be easy to add a checkbox on the Kodi page of the max2play web interface that sets a parameter (e.g. AUTOSTART_STOP_KODI_BY_HDMI_STATUS) in /opt/max2play/autostart.conf to make the script run optional and to activate / deactivate it with the web interface.

    To add a checkbox to the kodi page you should have a look at this 2 pages:

    view file to add checkbox in HTML:

    Autostart/Stop Kodi by HDMI Status: <input type="checkbox" <?php if($sp->view->AUTOSTART_STOP_KODI_BY_HDMI_STATUS) echo "checked"; ?> value="1" name="AUTOSTART_STOP_KODI_BY_HDMI_STATUS" />

    controller file to set parameter AUTOSTART_STOP_KODI_BY_HDMI_STATUS in /opt/max2play/autostart.conf

    there is an easy function for setting a Parameter in a file (add this to the existing „save“ action in Xbmc.php):

    if($_GET['action'] == 'save'){							
        $this->selectAutostart(isset($_GET['autostart']) ? 1 : 0);
        // Set selected Status
        $this->saveConfigFileParameter('/opt/max2play/autostart.conf', 'AUTOSTART_STOP_KODI_BY_HDMI_STATUS', isset($_REQUEST['AUTOSTART_STOP_KODI_BY_HDMI_STATUS']);
    // Get Status for Checkbox in view file (add this at the End of the function __construct)
    $this->view->AUTOSTART_STOP_KODI_BY_HDMI_STATUS = $this->getConfigFileParameter('/opt/max2play/autostart.conf', 'AUTOSTART_STOP_KODI_BY_HDMI_STATUS');

    The parameter for the autostart can be checked in the script „/opt/max2play/“ and run your script if set to 1.

    # Add this to /opt/max2play/ 
    AUTOSTART_STOP_KODI_BY_HDMI_STATUS=$(cat /opt/max2play/autostart.conf | grep AUTOSTART_STOP_KODI_BY_HDMI_STATUS=1 | wc -l)
         /opt/max2play/ &
    12. Februar 2016 at 11:49 #18470

    Thanks for all help. I don’t know how to achieve this and would be great if someone who’s more clever than me could get this into bitbucket.however, I don’t understand why the start audioplayer script is needed.

    14. Februar 2016 at 16:20 #18482

    I have two questions regarding this.

    (1) what should I do to get this working before it’s implemented into max2play?

    (2) tvservice -n still reports that my asus monitor is plugged in when I turn off or unplug the monitor. Is there a way to update the tvservice status. As a workaround i think I just reboot the rpi to reset tvservice -n and thereby turn off kodi (once I understand how to do with the script). However, it would be much better if the status could be updated some how. Turning off and hdmi with tvservice -o and -p flags doesn’t help.

    Any suggestions?

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

Register here