
I have them installed as described in the DTGBOT wiki: https://www.domoticz.com/wiki/Remote_Co ... _Libraries
Jos
Moderator: leecollings
Code: Select all
DOMO_url = "http:///127.0.0.1:8080"
Code: Select all
-- This script requires the installation of Lua 5.2 and the libraries
-- https://www.domoticz.com/wiki/Remote_Control_of_Domoticz_by_Telegram_Bot#Installing_Lua_Libraries
-- JQ dependencie is not requiered
--- This script sends an email when a user has turned the Test Switch On/Off
--- !!! Make sure this Lua is set as "Device", NOT as "All".
http = require "socket.http";
socket = require "socket";
https = require "ssl.https";
json = require "JSON";
DOMO_url = "http://192.168.x.xx:8080" -- Raspberry Pi local IP address
commandArray = {}
-- Get information recent log entries for switch action
function getswitchedbyinfo(device)
local response, returncode = http.request(DOMO_url..'/json.htm?type=command¶m=getlog&loglevel=2') ---&loglevel=2 is required with the latest beta's.
local suser=""
local stime=""
local sidx=""
local sstatus=""
local input = json:decode(response)
local result_table = input['result']
local tc = #result_table
for i = 1, tc do
for ltime,luser,lidx,ldevice,lstatus in string.gmatch(input.result[i].message, '(.-)%sUser:%s(.-)%sinitiated a switch command.-%((.-)/(.-)/(.-)%)')
do
if ldevice==device then
suser=luser
stime=ltime
sidx=lidx
sstatus=lstatus
end
end
end
return suser,stime,sidx,sstatus
end
device = "Test Switch" -- define the switch you want to use
if devicechanged[device] == 'On' then
user,timeinfo,idx,status = getswitchedbyinfo(device)
if user ~= "" then
commandArray['SendEmail'] = 'The Test Switch is turned On by '..user..', blabla # The Test Switch was turned On by '..user..' , blabla.#[email protected]'
end
end
if devicechanged[device] == 'Off' then
user,timeinfo,idx,status = getswitchedbyinfo(device)
if user ~= "" then
commandArray['SendEmail'] = 'The Test Switch is turned Off by '..user..', blabla # The Test Switch was turned Off by '..user..' , blabla.#[email protected]'
end
end
return commandArray
Code: Select all
2018-06-14 23:09:29.822 User: Eric initiated a switch command (52/Activity Main Switch/Off)
Many thanks Eric,EricT wrote: ↑Thursday 14 June 2018 23:11 Here it is:
Code: Select all
2018-06-14 23:09:29.822 User: Eric initiated a switch command (52/Activity Main Switch/Off)
please be aware that if you move on to a later version the way this is logged will change and therefore you then will need to add &loglevel=2 to the getlog json to get this type of message. Not sure with what version is starts but I know for sure >= V3.9580
Thanks guys!I have added it to the earlier posted code as the older domoticz version simply ignores it and it's required for the latest versions.
Code: Select all
http = require "socket.http";
socket = require "socket";
https = require "ssl.https";
json = require "JSON";
-- Get information from the last 15 seconds of log entries for the switch action details, returning User&Time and IDX&Status of the device
function getswitchedbyinfo(device)
local LastXXsecs = os.time()-15
local response, returncode = http.request('http://127.0.0.1:8080/json.htm?type=command¶m=getlog&loglevel=2&lastlogtime='..tostring(LastXXsecs))
local suser=""
local stime=""
local sidx=""
local sstatus=""
local input = json:decode(response)
local result_table = input['result']
local tc = #result_table
for i = 1, tc
do
for ltime,luser,lidx,ldevice,lstatus in string.gmatch(input.result[i].message, '(.-)%sUser:%s(.-)%sinitiated a switch command.-%((.-)/(.-)/(.-)%)')
do
if ldevice==device then
suser=luser
stime=ltime
sidx=lidx
sstatus=lstatus
end
end
end
return suser,stime,sidx,sstatus
end
by jvdz » Friday 15 June 2018 14:54
This is now the function i have in my general lua library file. It also limits the retrieval period of the log records to the last 15 seconds:
Users browsing this forum: No registered users and 0 guests