Ryan Herbst

Scheduler

Any home automation system needs a scheduler to trigger any number of events. The current version of my scheduler is pretty boring and uses a crontab file on my Linux server to initiate the execution of python scripts. These scripts are used to turn lights and devices on or off, process entries in the MySql database, upload weather station data to www.weatherunderground.com and perform other necessary operations.

My crontab file:

home_control

Python Functions To Send XPL Messages:

# sendXpl.py
# Python functions to send XPL messages

import socket
import time

# Function to send xpl message
def sendXpl ( msgType, target, schema, body ):

   # Prepare the socket for transmitting the message
   addr = ("172.16.10.255",3865)
   UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
   UDPSock.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)

   msg  = msgType + "n{nhop=1nsource=amaroq-sender.pythonntarget="
   msg += target + "n}n" + schema + "n{n" + body + "n}n"

   # Send the message
   UDPSock.sendto(msg,addr)

   return

# Function to set level of IO Device
def sendIodeviceLevel ( device, level ) :
   msgBody = "command=gotonlevel=" + level + "ndevice=" + device + "n"
   sendXpl("xpl-cmnd","amaroq-iodevice.iodevice","lighting.basic",msgBody);
   time.sleep(1)
   sendXpl("xpl-cmnd","amaroq-iodevice.iodevice","lighting.basic",msgBody);
   return

# Function to set level of IPLC Device
def sendIplcLevel ( device, level ) :
   msgBody = "command=gotonlevel=" + level + "ndevice=" + device + "n"
   sendXpl("xpl-cmnd","amaroq-iplcd.iplcd","lighting.basic",msgBody);
   time.sleep(1)
   sendXpl("xpl-cmnd","amaroq-iplcd.iplcd","lighting.basic",msgBody);
   return

# Function to send log message
def sendLogBasic ( type, text ) :
   msgBody = "type=" + type + "ntext=" + text + "n"
   sendXpl("xpl-stat","*","log.basic",msgBody);
   return

 

 Python Script To Send Turn on Insteon Devices:

#!/usr/bin/python
# Script to control IPLC device

# Includes
import sendXpl
import sys

sendXpl.sendIplcLevel ( sys.argv[1], sys.argv[2] )

 

Python Script To Send Turn on Relay Devices:

#!/usr/bin/python
# Script to control IPLC device

# Includes
import sendXpl
import sys

sendXpl.sendIodeviceLevel ( sys.argv[1], sys.argv[2] )

 

Python Script To Control Irrigation Devices:
This script will poll the MySql database to determine if it has recently rained. If rain is detected the irrigation device will not be turned on.

#!/usr/bin/python

# Script to control IPLC device

# Includes
import sendXpl
import sys
import time
import MySQLdb

inc = 0;

# Do we check for rain
if (len(sys.argv) > 3):

   # Lookup rain for the last 120 hours (5 Days)
   db = MySQLdb.connect(host="myhost",user="myuser",passwd="mypass",db="home")
   cursor = db.cursor();

   cursor.execute("select (current*0.0393700787) as value from sensor_basic where
                   device = 'Rain' and type = 'count' and time <= (now()-interval 120 hour)
                   order by time desc limit 1");
   row = cursor.fetchone();
   first = row[0];

   cursor.execute("select (current*0.0393700787) as value from sensor_basic where
                   device = 'Rain' and type = 'count' order by time desc limit 1");
   row = cursor.fetchone();
   last = row[0];
   inc = last - first;

# Send command if there is no rain or turning device off
if ( inc < 0.01 or sys.argv[2] == "0" ):
   sendXpl.sendIodeviceLevel ( sys.argv[1], sys.argv[2] )

Future Plans:

In the future I plan to implement a scheduler which is MySql based. This will make it easier to add/delete/modify the scheduler operations while providing the ability to access the control panel remotely.

Email me if you have any questions.

Amaroq Weather Station

Beresford Ave, Redwood City, CA
temp: 72.1 F (22.3 C) (22.3)
humidity: 52%
wind: From the WNW at 3.1 MPH Gusting to 2.5 MPH
pressure: 30.00" (1015.9 mb)
station: KCAREDWO4
hardware: WMR918
updated:July 17, 11:40 AM PDT
local forecast