Home Automation With XBMC

Posted: February 5th, 2012 | 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\ and on Windows Vista or 7, C:\Users\[user]\AppData\Roaming\XBMC\

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():


import xbmc,xbmcgui
import subprocess,os

class MyPlayer(xbmc.Player) :

        def __init__ (self):
            xbmc.Player.__init__(self)

        def onPlayBackStarted(self):
            if xbmc.Player().isPlayingVideo():
                os.system("wget -b -O /dev/null -o /dev/null 'http://10.10.0.6:49451/data_request?id=lu_action&output_format=xml&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=19'")

        def onPlayBackEnded(self):
            if (VIDEO == 1):
                os.system("wget -b -O /dev/null -o /dev/null 'http://10.10.0.6:49451/data_request?id=lu_action&output_format=xml&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=2'")

        def onPlayBackStopped(self):
            if (VIDEO == 1):
                os.system("wget -b -O /dev/null -o /dev/null 'http://10.10.0.6:49451/data_request?id=lu_action&output_format=xml&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=2'")

        def onPlayBackPaused(self):
            if xbmc.Player().isPlayingVideo():
                os.system("wget -b -O /dev/null -o /dev/null 'http://10.10.0.6:49451/data_request?id=lu_action&output_format=xml&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=18'")

        def onPlayBackResumed(self):
            if xbmc.Player().isPlayingVideo():
                os.system("wget -b -O /dev/null -o /dev/null 'http://10.10.0.6:49451/data_request?id=lu_action&output_format=xml&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=19'")
player=MyPlayer()

VIDEO = 0

while(1):
    if xbmc.Player().isPlaying():
      if xbmc.Player().isPlayingVideo():
        VIDEO = 1

      else:
        VIDEO = 0

    xbmc.sleep(1000)

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.


import xbmc,xbmcgui
import subprocess,os

ILT = 0
IDLE_TIME_MIN = 15
s = 0

while(1):
  it = xbmc.getGlobalIdleTime()
  s = ((IDLE_TIME_MIN * 60) - it )
  if (s > 0):
    if (ILT == 1):
      if xbmc.Player().isPlayingVideo():
        pass
      else:
        os.system("wget -b -O /dev/null -o /dev/null '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'")
        ILT = 0

  elif (s < 0):
    if (ILT == 0 and xbmc.Player().isPlayingVideo() == False):
      os.system("wget -b -O /dev/null -o /dev/null 'http://10.10.0.6:49451/data_request?id=lu_action&output_format=xml&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=10'")
      ILT = 1
xbmc.sleep(5000)

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 or the above mentioned Windows directories. You probably already have an autoexec.py but if you don’t, create one.


import os
import xbmc

xbmc.executescript('special://home/scripts/playeractions.py')
xbmc.executescript('special://home/scripts/idletime.py')

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!


Image Positioning in Markdown

Posted: September 19th, 2011 | Comments

With Markdown, an exclamation mark preceding a link, such as ![Example](/example.jpg) will insert an image into your body of text. Unfortunately, Markdown’s syntax doesn’t support positioning images, for that you’ll need to fall back on HTML.
Read the rest of this post »


Essential iPhone 4 Accessories

Posted: September 18th, 2011 | Comments

After having owned an iPhone 4 for more than a year, I’ve found a few accessores that I consider essential.
Read the rest of this post »


BASH Profile

Posted: March 19th, 2009 | Comments

Do you spend a lot of time in the terminal? Do you use BASH? Did you know you could change your default BASH prompt easily?

This is my prompt: bash

As you can see, it has the current time, the username, the computer name, and the current working directory. If you’d like to use this prompt, you can download my bash_profile here and start using it by renaming it .bash_profile and putting it in your home directory. Running mv bash_profile ~/.bash_profile will do it.


Recalibraing Your Roomba Battery

Posted: March 14th, 2009 | Comments

If you’ve owned a Roomba for more then a year you have no doubt noticed that the battery life is drastically reduced from when you bought it. Recalibrating, also known as reconditioning, your battery is a simple procedure that may help you reclaim some of that life. Read the rest of this post »


WiFi Internet Anywhere with Sprint EVDO

Posted: March 14th, 2009 | Comments

I recently picked up a Compass 597 EVDO Rev. A card from Sprint, allowing me to access the internet nearly anywhere. I also picked up a Cradlepoint PHS-300 wireless router. The PHS-300 is a very small, battery powered, WiFi router designed specifically for USB EVDO cards or supported phones. By plugging the C597 into the router I have WiFi for about two hours. Read the rest of this post »


Keeping Your Inbox Empty

Posted: March 14th, 2009 | Comments

I want to outline a few different ways I keep my inbox free of email.

An inbox, in any form, isn’t meant to hold things forever, it’s designed to be the first place an item goes before getting sorted, or taking action against; Like deleting or throwing out the item. This same approach is the most effective and efficient way of using email.

First off, the way I handle email is only going to work for me, I’m just going to outline how I do things to give you ideas how to keep your inbox clear. Read the rest of this post »