Sure, here's the code I'm currently using. It's split into two files (one lua and one python); the lua file for handling the selector switch in Domoticz and that calls the python script to connect to the Kettle.
As I've already mentioned this is the first python script I've ever written and I'm pretty new to lua too - so if anyone can improve them, I'm happy to receive a better version!
script_device_kettle.lua (domoticz/scripts/lua/script_device_kettle.lua)
- You'll need to edit the script variable to the location of the python script on your system
- My virtual device is called 'Kettle' - if yours is not, you'll need to change that
- Based on a selector switch with buttons saying.. Off | 65 C | 80 C | 95 C | 100 C
- I've included the code for a 'Warm' button, but haven't used or tested it
Code: Select all
------------------------------
-- iKettle Control --
-- script_device_kettle.lua --
------------------------------
-- Replace with location of your kettle python script
script = "/home/USER/domoticz/scripts/python/kettle.py"
commandArray = {}
if (devicechanged['Kettle'] == '65 C') then
print("Kettle switched on / Set to 65")
os.execute(string.format("%s 65", script))
commandArray['Kettle'] = "Off AFTER 300"
elseif (devicechanged['Kettle'] == '80 C') then
print("Kettle switched on / Set to 80")
os.execute(string.format("%s 90", script))
commandArray['Kettle'] = "Off AFTER 300"
elseif (devicechanged['Kettle'] == '95 C') then
print("Kettle switched on / Set to 95")
os.execute(string.format("%s 95", script))
commandArray['Kettle'] = "Off AFTER 300"
elseif (devicechanged['Kettle'] == '100 C') then
print("Kettle switched on / Set to 100")
os.execute(string.format("%s 100", script))
commandArray['Kettle'] = "Off AFTER 300"
elseif (devicechanged['Kettle'] == 'Off') then
print("Kettle switched off")
os.execute(string.format("%s off", script))
elseif (devicechanged['Kettle'] == 'Warm') then
print("Kettle set to warm")
os.execute(string.format("%s warm", script))
end
return commandArray
I've also coded into the lua script a 'turn-off' the (virtual) kettle after 300 seconds. This is so the device is switched off in Domoticz after it's finished boiling as these scripts don't do any checking to see if it's actually boiled.
kettle.py (domoticz/scripts/python/kettle.py)
- You'll need to add the IP address of your kettle
Code: Select all
#!/usr/bin/env python
import time
import socket
import sys #For Command line args
TCP_IP = '192.168.1.XXX' #Change to your Kettle's (fixed) IP.
TCP_PORT = 2000
BUFFER_SIZE = 10
INITIATE = "HELLOKETTLE\n"
START = "set sys output 0x4\n" #Switch on kettle
T_65 = "set sys output 0x200\n" #Set to 65 degrees
T_80 = "set sys output 0x4000\n" #Set to 80 degrees
T_95 = "set sys output 0x2\n" #Set to 95 degrees
T_100 = "set sys output 0x80\n" #Set to 100 degrees
WARM = "set sys output 0x8\n" #Keep warm for 30 minutes.
OFF = "set sys output 0x0\n" #Turn off kettle
#Grab switch from commmand line
try:
action = str(sys.argv[1])
except IndexError:
print "No option provided, unable to continue"
sys.exit(2);
else:
#has one argument, initiate connection
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(INITIATE)
data = s.recv(BUFFER_SIZE)
print "Initiate: ", data
if action == "off":
s.send(OFF)
else:
#Switch on kettle
s.send(START)
data = s.recv(BUFFER_SIZE)
print "Start data", data
time.sleep(1)
#Send chosen action to kettle
if action == "65":
s.send(T_65)
elif action == "80":
s.send(T_80)
elif action == "95":
s.send(T_95)
elif action == "100":
s.send(T_100)
elif action == "warm":
s.send(WARM)
else:
print "Unknown argument: (%s). Sending OFF command" % sys.argv[1]
s.send(OFF)
data = s.recv(BUFFER_SIZE)
print "Func:", data
#Close connection
print "Closing connection"
s.close()
Hope that helps, happy to try and help out with it, if I can.