Skip to content

Instantly share code, notes, and snippets.

@briped
Last active January 22, 2017 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briped/ce222d794f11225dc13b5dea498803c3 to your computer and use it in GitHub Desktop.
Save briped/ce222d794f11225dc13b5dea498803c3 to your computer and use it in GitHub Desktop.
--
-- Tellstick Switch Handler
-- Created by: Brian Schmidt Pedersen (@briped)
--
-- The device names from the Tellstick MUST be the same as the device names
-- defined in domoticz, or this script will not work.
--
-- The script is hacked together from several sources, without any knowledge of
-- Lua, so just about everything can probably be done better than my attempt.
-- This also means that no support is given beyond the comments found in this
-- script, and the use of this script is at your own risk.
--
-- Use tdtool to list all devices and insert into table
tdtoolDevices = {}
local f = assert(io.popen('tdtool --list-devices'))
for line in f:lines() do
table.insert(tdtoolDevices, line)
end
f:close()
-- Loop through all found devices and build a table with all devices and their keys/values
devices = {}
for k, v in pairs(tdtoolDevices) do
-- print('DEBUG: k: ' .. k .. '. v: ' .. v)
t = {}
for tdtoolKeyVal in tdtoolDevices[k]:gmatch('%S+') do
for tdtoolKey, tdtoolVal in string.gmatch(tdtoolKeyVal, "(.+)=(.+)") do
-- print('DEBUG: tdtoolKey = tdtoolVal: ' .. tdtoolKey .. ' = ' .. tdtoolVal)
t[tdtoolKey] = tdtoolVal
end
end
table.insert(devices, t)
end
--
-- devices contains the following key/value pairs:
-- * type
-- * id
-- * name
-- * lastsentcommand
--
commandArray = {}
for deviceName, deviceValue in pairs(devicechanged) do
print("INFO: Device based event triggered: Device Name: '" .. deviceName .. "'. Device Value: '" .. tostring(deviceValue) .. "'")
for k, v in pairs(devices) do
if (string.lower(tostring(deviceName)) == string.lower(tostring(v.name))) then
if (string.lower(tostring(deviceValue)) == 'on') then
local handle = io.popen('tdtool --on ' .. v.id)
local result = handle:read('*a')
handle:close()
print(result) -- Will put the result in the log
-- commandArray[deviceName] = 'On'
elseif (string.lower(tostring(deviceValue)) == 'off') then
local handle = io.popen('tdtool --off ' .. v.id)
local result = handle:read('*a')
handle:close()
print(result) -- Will put the result in the log
-- commandArray[deviceName] = 'Off'
end
end
end
end
return commandArray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment