Page 1 of 1
Update status (dummy) switch with LUA / DzVents
Posted: Monday 04 September 2017 17:02
by htilburgs
I'm not a programmer, so I'm looking for a starting point.
Situation:
- Dummy switch for turning on / off Motion Detection on (Foscam) Webcam through URL with CGI-BIN
- With CGI-BIN / URL it's possible to detect current state of Motion Detection
Example:
Check status MD on Foscam Webcam
Code: Select all
http://192.168.0.10:88/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr=XXX&pwd=XXX
Result:
Code: Select all
<CGI_Result>
<result>0</result>
<isEnable>1</isEnable>
<linkage>142</linkage>
<snapInterval>1</snapInterval>
<sensitivity>3</sensitivity>
<triggerInterval>0</triggerInterval>
<isMovAlarmEnable>1</isMovAlarmEnable>
<isPirAlarmEnable>0</isPirAlarmEnable>
<schedule0>281474976710655</schedule0>
<schedule1>281474976710655</schedule1>
<schedule2>281474976710655</schedule2>
<schedule3>281474976710655</schedule3>
<schedule4>281474976710655</schedule4>
<schedule5>281474976710655</schedule5>
<schedule6>281474976710655</schedule6>
<area0>1022</area0>
<area1>1022</area1>
<area2>1020</area2>
<area3>992</area3>
<area4>768</area4>
<area5>772</area5>
<area6>518</area6>
<area7>7</area7>
<area8>7</area8>
<area9>7</area9>
</CGI_Result>
What I like:
- When <isEnable>xx1</isEnable> = 1 and Dummy Switch is ON -> no action
- When <isEnable>xx/isEnable> = 1 and Dummy Switch is OFF -> update status Dummy Switch to ON
- When <isEnable>xx</isEnable> = 0 and Dummy Switch is OFF ->no action
- When <isEnable>xx</isEnable> = 0 and Dummy Switch is ON ->update status Dummy Switch to OFF
Is this possible and how do I start this in DzVents of LUA?
Maybe there is an example where I can hold on-to, so I can try to build a script.
Re: Update status (dummy) switch with LUA / DzVents
Posted: Wednesday 06 September 2017 20:37
by zicht
Hi
This is possible in both Lua and dzEvents
Just purge the url with io.popen and capture the content in a string.
Then you can do whatever you want with the captured string.
Read an XML or Json string and split, or just do a string.find() and string.sub() all depending on where you would like to go
example of getting content of a website with io,popen with curl in lua
Code: Select all
function os.capture(cmd, raw) -- os.command uitvoeren en resultaat daarvan lezen in string
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
you could call this function with
Code: Select all
commando = 'curl -s https://api.telegram.org/'..apikey..'/sendPhoto -F chat_id='..chat_id..' -F photo=@cam/'..naam..'.jpg'
result = os.capture(commando)
if (not(string.find(result, '"ok"'))) then print("SendCam error "..result) end
Other example using json
Code: Select all
url = 'https://public-xxx"
json = assert(loadfile "C:/PROGRA~2/domoticz/scripts/lua/JSON.lua")()
local config = assert(io.popen('curl "'..url..'"'))
local x = config:read('*all')
config:close()
local jsonx = json:decode(x)
All are just copy paste from working scripts i use myself for different purpose.
Hope it will get you inspired to create you own...
Re: Update status (dummy) switch with LUA / DzVents
Posted: Thursday 07 September 2017 9:18
by htilburgs
First, thanx for the reply. I was already started with looking at existing scripts and came up with the following
Code: Select all
--
-- Domoticz passes MotionDetect status from webcams to Dummy Switches
--
-- Variables (Customize) ----------------------------------------------------------------------------------
local gebruiker = "xxx" -- uservar for username
local wachtwoord = "xxx" -- uservar for password
local tempfilename = '/home/pi/domoticz/scripts/tmp/motion.tmp' -- can be anywhere writeable
local debug = false -- false, true for domoticz log
-----------------------------------------------------------------------------------------------------------
function file_exists(path) -- function to check if a file exists
local file = io.open(path, "rb")
if file then file:close() end
return file ~= nil
end
commandArray = {}
-- Array of webcams to be checked
-- cam= Name of Dummy Device in domoticz
-- url= URL used to check the webcam
webcams = {
Gang = {cam="MotionDetect gang" ; url='http://192.168.0.30:88/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr='..gebruiker..'&pwd='..wachtwoord..''};
Huiskamer = {cam='MotionDetect woonkamer' ; url='http://192.168.0.31:88/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr='..gebruiker..'&pwd='..wachtwoord..''};
Buiten = {cam='MotionDetect buiten' ; url='http://192.168.0.32:88/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr='..gebruiker..'&pwd='..wachtwoord..''};
}
-- Webcam status
for webcam,location in pairs(webcams) do
url='..location.url'
if debug then print("Webcam MotionDetect - Collecting data from: "..location.url) end
read = os.execute('curl -s -o '..tempfilename..' "'..location.url..'"')
file = io.open(tempfilename, "r")
s= file:read("*a")
s = (string.gsub(s, "%c+", ""))
file:close()
-- MotionDetect Status
motionStatus = string.match(s, [[<isEnable>(%d+)]])
if debug then print("MotionDetectStatus:\t"..motionStatus.." from webcam: "..webcam) end
if debug then print("Domoticz Device:\t"..location.cam) end
end
return commandArray
This parts works and I get the correct results.
Code: Select all
2017-09-07 09:08:00.610 dzVents: Webcam MotionDetect - Collecting data from: http://192.168.0.32:88/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr=xxx&pwd=xxx
2017-09-07 09:08:00.825 dzVents: MotionDetectStatus: 1 from webcam: Buiten
2017-09-07 09:08:00.826 dzVents: Domoticz Device: MotionDetect buiten
2017-09-07 09:08:00.826 dzVents: Webcam MotionDetect - Collecting data from: http://192.168.0.30:88/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr=xxx&pwd=xxx
2017-09-07 09:08:00.981 dzVents: MotionDetectStatus: 0 from webcam: Gang
2017-09-07 09:08:00.982 dzVents: Domoticz Device: MotionDetect gang
2017-09-07 09:08:00.982 dzVents: Webcam MotionDetect - Collecting data from: http://192.168.0.31:88/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr=xxx&pwd=xxx
2017-09-07 09:08:01.124 dzVents: MotionDetectStatus: 0 from webcam: Huiskamer
2017-09-07 09:08:01.124 dzVents: Domoticz Device: MotionDetect woonkamer
The next part is updating the status of the dummy switch. I'm working on that, but I get errors.
Code: Select all
if motionStatus='0' and otherdevices[..location.cam] == 'On' then
commandArray[..location.cam] = 'Off'
table.insert(commandArray)
end
if motionStatus='1' and otherdevices[..location.cam] == 'Off' then
commandArray[..location.cam] = 'On'
table.insert(commandArray)
end
The error is:
Code: Select all
2017-09-07 09:15:01.620 Error: EventSystem: in Test: [string "-- ..."]:44: 'then' expected near '='
Line 44 is the part of:
Code: Select all
if motionStatus='0' and otherdevices[..location.cam] == 'On' then
I think it has something to do with the "..location.cam", but I'm not sure if I using the correct way to update my device.
Re: Update status (dummy) switch with LUA / DzVents
Posted: Friday 08 September 2017 16:07
by htilburgs
Anybody a hint? I'm looking in the Wiki and reading older posts, but I can't get it to work.
Problably I'm doing something wrong, but as I said I'm not a (lua) programmer.
This is my first attempt, any help is appreciated!
Re: Update status (dummy) switch with LUA / DzVents
Posted: Friday 08 September 2017 21:56
by zicht
maybe (just making educated guess...)
should be :
== is used for evaluating purpose like status in "if aa==1 then"
= is used to set value into something like a=1
Re: Update status (dummy) switch with LUA / DzVents
Posted: Sunday 10 September 2017 13:00
by htilburgs
Succes! After a lot of puzzeling and testing, I've found a working solution. For me it looks nice....
I put the code here, so maybe someone can benefit from it.
Code: Select all
--
-- Domoticz passes MotionDetect status from webcams to Dummy Switches
--
-- Variables (Customize) ----------------------------------------------------------------
local gebruiker = "xxx" -- uservar for username
local wachtwoord = "xxx" -- uservar for password
local tempfilename = '/tmp/log/motion.tmp' -- can be anywhere writeable
local debug = false -- false, true for domoticz log
-----------------------------------------------------------------------------------------
-- Function to check if a file exists
function file_exists(path)
local file = io.open(path, "rb")
if file then file:close() end
return file ~= nil
end
commandArray = {}
-- Array of webcams to be checked
-- idx : IDX of device in Domoticz
-- cam : Name of Dummy Switch in Domoticz
-- url : URL used to check the webcam
webcams = {
Gang = {idx='417' ; cam="MotionDetect Gang" ; url='http://192.168.0.30:88/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr='..gebruiker..'&pwd='..wachtwoord..''};
Huiskamer = {idx='418' ; cam='MotionDetect Woonkamer' ; url='http://192.168.0.31:88/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr='..gebruiker..'&pwd='..wachtwoord..''};
Buiten = {idx='416' ; cam='MotionDetect Buiten' ; url='http://192.168.0.32:88/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr='..gebruiker..'&pwd='..wachtwoord..''};
}
-- Webcam status
for webcam,location in pairs(webcams) do
url='..location.url'
if debug then print("Webcam MotionDetect - Collecting data from: "..location.url) end
read = os.execute('curl -s -o '..tempfilename..' "'..location.url..'"')
file = io.open(tempfilename, "r")
s= file:read("*a")
s = (string.gsub(s, "%c+", ""))
file:close()
-- MotionDetect Status
motionStatus = string.match(s, [[<isEnable>(%d+)]])
if debug then print("MotionDetectStatus:\t"..motionStatus.." from webcam: "..webcam) end
if debug then print("Domoticz Device:\t"..location.cam.." IDX: "..location.idx) end
-- Turn Dummy switch on/off
if motionStatus == '1' and otherdevices[''..location.cam..''] == 'Off' then
commandArray[''..location.cam..''] = 'On'
print("Device:\t"..location.cam.." turned: On")
elseif motionStatus == '0' and otherdevices[''..location.cam..''] == 'On' then
commandArray[''..location.cam..''] = 'Off'
print("Device:\t"..location.cam.." turned: Off")
end
end
return commandArray