Home Automation With XBMC

Posted: February 5th, 2012 | 19 Comments »

At my home, I use a Mi Casa Verde Vera 3 home automation controller to control lights in my house, and I wanted a way to have XBMC automatically control lights for me. By adding a couple Python scripts to XBMC, whenever I start playing a video the lights in my living room turn off, if I pause the video the lights in my living room, kitchen, hallway, and bathroom go to 25% brightness, and when video stops playing, the lights in the living room turn on to full brightness. It also turns on and off my deck lights – no need to waste power when I’m inside watching something. You could modify the script to do the same thing with X10 or INSTEON, pause or resume your torrent or SABnzbd downloads, set a status on your Twitter or Facebook that you’re watching something, set your Google Voice account to Do Not Disturb, or anything else you can think of.

All of the below scripts go in the ~/.xbmc/scripts directory. If you don’t have that directory, create it. If you’re on Windows XP, that directory is under C:\Documents and Settings\[user]\Application Data\XBMC\scripts and on Windows Vista or 7, C:\Users\[user]\AppData\Roaming\XBMC\scripts

The first script watches XBMC for the state of the video player and launches actions when it changes. It doesn’t do anything if you’re playing music or watching a slide show. Feel free to modify it to cover both Videos and Music by removing if xbmc.Player().isPlayingVideo():

Save it as playeraction.py in the scripts folder mentioned above. You might be asking why I’m using os.system("wget")? I wanted to test the commands on my shell to make sure they were the right URLs, and I didn’t want to clutter up the script with using urllib2 and implementing error handling if someone was going to use the script with local shell commands.

The next script launches actions when XBMC is idle for more than 15 minutes. I have it set to turn off my TV and Receiver to save power – it immediately powers them back on if I touch any button on the remote.

Change IDLE_TIME_MIN if you want more or less idle time. Save this script as idletime.py in the scripts directory.

Finally, the scripts will need to be launched by XBMC by including them in autoexec.py which is under ~/.xbmc/userdata on Mac or Linux, Windows XP is C:\Documents and Settings\[user]\Application Data\XBMC\userdata and on Windows Vista or 7, C:\Users\[user]\AppData\Roaming\XBMC\userdata. You probably already have an autoexec.py but if you don’t, create one.

What this does is launch the two scripts from the default home directory of your master profile. If you’re using multiple profiles you’ll have to modify the special:// directory to match the other profile.

If you find the above scripts useful, leave a comment with how you’re using them, it might inspire someone!


  • http://twitter.com/ryanrcraig Ryan Craig

    Awesome guide. Can you update it for eden? They removed the autoexec.py, so I'm clueless how to integrate this script in to xbmc

  • http://JordanHackworth.com Jordan Hackworth

    According to this developer, http://forum.xbmc.org/showpostautoexec.py is still supported, it's just depreciated as a form of launching plugins (as it should be)

    You should be able to create an autoexec.py under the userdata folder (there won't be one by default) 

    If you look at debug logs, http://forum.xbmc.org/showpost… You can see that it's still looking for an autoexec.py

  • Shanbax

    I'm trying this on a windows 7 machine and it seems that the wget command is not using the entire URL
    I am getting returned “'ouput_format' is not recognised as a internal or external command, operable program or batch file”
    and I get the same for “serviceId”. “action”. and “SceneNum”

  • http://JordanHackworth.com Jordan Hackworth

    Try downloading wget.exe from here http://users.ugent.be/~bpuype/… and saving it to C:Windows, then modifying the code to 

    wget –spider “http://10.10.0.6:49451/data_request?id=lu_action&output_format=xml&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=10″

  • Shanbax

    ok that now works from the command prompt but the script failed when loaded into xbmc…maybe because of the nested quotes?

  • Shanbax

    ok figured that out by swapping the single and double quotes around…now I just need to suppress the output of the command so that xbmc isn,t forced to the background.

  • Therealabdo

    Hello

    I have done exactly what is written above
    unfortunately nothing happened
    I am using Vera2 and when i just copy the http URL on my browser the scene is triggered but from XBMC i cant!

    Will you help

  • chixxi

    First off all, thank you for this! It is working on the latest nightly openelec fusion build on a zotac zbox ad04! Great work.

    I am working on a plugin for the vera to control xbmc via upnp. I would very much like to provide some variables in this plugin so that your script can set a “PlayerStateVariable”. The plugin also provides events, which can then be used to trigger scenes.

    I am new to xbmc and to python. I was wondering if could collect more information in this way? For example a python script which recognizes the audio state, the current volume, or the current menu point, or which app is opened. Do you know where I can find more information like this? Or could we maybe work together to make a useful plugin? I have quiet some experience with the vera, but not so much with xbmc.

  • chixxi

    forget, this runs on eden as well!

  • Seb

    Very cool !
    Works perfectly under Mac OS with a Vera lite.

  • http://twitter.com/StuTro Stu

    any reason why this script takes about 50% CPU and and also causes XBMC to hang when exiting?

  • http://JordanHackworth.com Jordan Hackworth

    To anyone else concerned – I got this resolved. I was missing two spaces in the idletime.py script which left xbmc.sleep(5000) outside of the loop so it ran constantly. 

  • http://www.facebook.com/mathias.neerup Mathias Neerup

    Thank you sir :)

  • Raginglynx

    Is it possible to also add a feature where I get to choose between projector and lcd-tv when I want to play a movie. And when choosing i.e the projector, shades go down, lights dim and the projector screen goes down? I plan on using insteon/knx as automation solution.

  • Andy Penty

    Hi, I am using Vera2 but with PLEX for media player and wondering if the same logic will work…has anyone done similar thing with Plex..does plex also have autoexec.py etc?

  • Andrew Grady

     Hi Andy, Did you get anywhere with Plex and Vera? I am looking to do the same thing so no point reinventing the wheel if you created something that your willing to share :-)

  • Meiky76

    There’s a problem: The events are fired more than once. For example, when you start a video it fires up to three/four times the command… Do you know a solution for that?

  • Raginglynx

    I’m using raspberry pi’s with rapsmbc (XBMC). I’ve realized the easiest way of doing that is to control them by XMBC remote and have one Raspberry for the LCD and one for the Projector. Then make different scenes for the KNX-system when playing something on the projector and the LCD. Don’t you think?

  • M Hula

    Love to get this working!
    But I get:

    ImportError: No module named xbmc

    I thought I googled and it said xbmc functions aren’t available externally????? i.e. must be an addon!!!
    HELP! :-)