Home Automation With XBMC
Posted: February 5th, 2012 | CommentsAt 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!















