Page 3 of 17

Re: version 0.9.8 released

Posted: Saturday 12 March 2016 0:40
by simonrg
dannybloe wrote:Btw, fetching extra device data from Domoticz like battery info only works on linux file systems. It requires bash, curl and sed and lots of pipes ;).
Brilliant, 8 piped seds is quite something and spawning gets over the 10 second rule.

A little suggestion I think would be an improvment, is to make the request DomoticzData function into a normal time script, call it with the normal time mechanism, this would also allow users to attach it to a virtual switch and force an update by simply operating the switch.

The various json libraries are fairly efficient and in the past I had no problem doing dumb dumps even on a Raspberry Pi B as per @Westcott, the slow bit I found was file passing to the os.execute and back, which I overcame by using Lua http libaries.

Installation of the http, socket, ssl and json libraries I use for Raspberry Pi is described here - http://www.domoticz.com/wiki/Remote_Con ... _Libraries

I attach an example of doing the dump of all the json as Lua table script to devices.lua every minute - strangely having run it for an hour occassionally it would come up with the Lua 10 second warning - once again it could be spawned and run as a separate process doesn't need the direct connection to Domoticz as just uding the API.

Theoretically with the LuaRocks framework it should be possible to run this code anywhere LuaRocks is available - https://luarocks.org/.

The key bit of code is here, all the code with the large bit of stolen table to lua script dumping code is hidden below:

Code: Select all

commandArray = {}

DomoticzIP = "127.0.0.1"
DomoticzPort = "8080"
server_url = "http://"..DomoticzIP..":"..DomoticzPort
-- Retrieve all the device data
jresponse, status = http.request(server_url.."/json.htm?type=devices")
-- Decode the json to a Lua table
decoded_response = JSON:decode(jresponse)

print('About to have a go at dumping')

table.dump('device.lua',decoded_response,false,false)

print('Done the dumping')

return commandArray
Full script_time_luadevicedata.lua
Spoiler: show

Code: Select all

-- script_time_luadevicedata.lua
-- Load necessary Lua libraries
http = require "socket.http";
socket = require "socket";
https = require "ssl.https";
JSON = require "JSON";

-- Table dumping from https://github.com/CentauriSoldier/Open-War/blob/master/Plugins/luaext/modules/table.lua

local tEscapeChars = {
        [1] = {
                Char = "\\",
                RelacementChar = "\\\\",
        },
        [2] = {
                Char = "\a",
                RelacementChar = "\\a",
        },
        [3] = {
                Char = "\b",
                RelacementChar = "\\b",
        },
        [4] = {
                Char = "\f",
                RelacementChar = "\\f",
        },
        [5] = {
                Char = "\n",
                RelacementChar = "\\n",
        },
        [6] = {
                Char = "\r",
                RelacementChar = "\\r",
        },
        [7] = {
                Char = "\t",
                RelacementChar = "\\t",
        },
        [8] = {
                Char = "\v",
                RelacementChar = "\\v",
        },
        [9] = {
                Char = "\"",
                RelacementChar = "\\\"",
        },
        [10] = {
                Char = "\'",
                RelacementChar = "\\'",
        },
        [11] = {
                Char = "%[",
                RelacementChar = "%%[",
        },
        [12] = {
                Char = "%]",
                RelacementChar = "%%]",
        },
};

function table.tostring(tInput, nStartCount)
local sRet = "";
local nCount = 0;

        if type(nStartCount) == "number" then
        nCount = nStartCount;
        end

local sTab = "";

for x = 1, nCount do
sTab = sTab.."\t";
end

local sIndexTab = sTab.."\t";

nCount = nCount + 1;

        if type(tInput) == "table" then
        sRet = sRet.."{\r\n";

                for vIndex, vItem in pairs(tInput) do
                local sIndexType = type(vIndex);
                local sItemType = type(vItem);
                local sIndex = "";

                        --write the index to string
                        if sIndexType == "boolean" then

                                if vIndex == true then
                                sRet = sRet..sIndexTab.."[true] = ";

                                else
                                sRet = sRet..sIndexTab.."[false] = ";

                                end


                        elseif sIndexType == "function" then
                        sRet = sRet..sIndexTab.."["..GetFunctionName(vIndex).."] = ";


                        elseif sIndexType == "number" then
                        sRet = sRet..sIndexTab.."["..vIndex.."] = ";

                        elseif sIndexType == "string" then
                        sIndex = sIndexTab.."[\""..vIndex.."\"] = ";

                        elseif sIndexType == "table" then
                        sIndex = sIndexTab.."["..table.tostring(vIndex, nCount).."] = ";

                        end

                        --write the item to string
                        if sItemType == "number" then
                        sRet = sRet..sIndex..vItem..",\n"

                        elseif sItemType == "string" then

                                for nIndex, tChar in pairs(tEscapeChars) do
                                vItem = string.gsub(vItem, tChar.Char, tChar.RelacementChar);
                                end

                        sRet = sRet..sIndex.."\""..vItem.."\",\n";

                        elseif sItemType == "boolean" then

                                if vItem then
                                sRet = sRet..sIndex.."true,\n";
                                else
                                sRet = sRet..sIndex.."false,\n";
                                end

                        elseif sItemType == "nil" then
                        sRet = sRet..sIndex.."nil,\n";

                        elseif sItemType == "function" then
                        sRet = sRet..sIndex..GetFunctionName(vItem)..",\n";

                        elseif sItemType == "userdata" then
                        sRet = sRet..sIndex.."\"_USERDATA_\",\n"

                        elseif sItemType == "table" then
                        sRet = sRet..sIndex..table.tostring(vItem, nCount)..",\n";

                        end

                end

        end


sRet = sRet..sTab.."}"

return sRet
end

function table.dump(pFile, tTable, bAppend, bOpenBinary)
assert(type(pFile) == "string", "File path must be a string.");
assert(type(tTable) == "table", "Input table must be of type table.");

local sOpenType = "w";

        if bOpenBinary then
        sOpenType = sOpenType.."b";
        end

        if bAppend then
        sOpenType = sOpenType.."+";
        end

        local hFile = assert(io.open(pFile, sOpenType), "Error in file: "..pFile.."\r\nDestination file could not be created or opened.\r\nPerhaps you do not have the required permissions for this action.");

        if hFile then
        hFile:write(table.tostring(tTable, 0));

        hFile:close();
        end

end

commandArray = {}

DomoticzIP = "127.0.0.1"
DomoticzPort = "8080"
server_url = "http://"..DomoticzIP..":"..DomoticzPort
-- Retrieve all the device data
jresponse, status = http.request(server_url.."/json.htm?type=devices")
decoded_response = JSON:decode(jresponse)

print('About to have a go at dumping')

table.dump('device.lua',decoded_response,false,false)

print('Done the dumping')

return commandArray

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 0:44
by commodore white
Thanks for the update. Well worth it as most of my immediate problems were resolved. Again I used your code snippet as my test and found that it worked as expected as long as the trigger criteria was a device name, in my case, a virtual on/off switch called DzVents.

Code: Select all

2016-03-11 23:48:00.492 LUA: Found module in scripts folder: devicetest
2016-03-11 23:48:00.492 LUA: Found module in scripts folder: example
2016-03-11 23:48:00.493 LUA: Found module in scripts folder: test
2016-03-11 23:48:32.615 LUA: Handling events for: "DzVents", value: "Off"
2016-03-11 23:48:32.615 LUA: =====================================================
2016-03-11 23:48:32.615 LUA: >>> Handler: test
2016-03-11 23:48:32.616 LUA: >>> Device: "DzVents" Index: nil
2016-03-11 23:48:32.616 LUA: .....................................................
2016-03-11 23:48:32.616 LUA: Nothing to take care off, all clear again
2016-03-11 23:48:32.616 LUA: DzVents did it
2016-03-11 23:48:32.616 LUA: .....................................................
2016-03-11 23:48:32.617 LUA: <<< Done
2016-03-11 23:48:32.617 LUA: -----------------------------------------------------
2016-03-11 23:48:32.403 (Virtual) Lighting 1 (DzVents)
2016-03-11 23:48:35.024 LUA: Handling events for: "DzVents", value: "On"
2016-03-11 23:48:35.024 LUA: =====================================================
2016-03-11 23:48:35.024 LUA: >>> Handler: test
2016-03-11 23:48:35.025 LUA: >>> Device: "DzVents" Index: nil
2016-03-11 23:48:35.025 LUA: .....................................................
2016-03-11 23:48:35.025 LUA: DzVents did it
2016-03-11 23:48:35.025 LUA: .....................................................
2016-03-11 23:48:35.025 LUA: <<< Done
2016-03-11 23:48:35.025 LUA: -----------------------------------------------------
2016-03-11 23:48:35.026 LUA: [1] = SendNotification: Gutter is clogged#DzVents#2
2016-03-11 23:48:35.026 LUA: =====================================================
2016-03-11 23:48:35.026 Error: Error sending notification: 'Logitech Media Server' not found in Hardware-list
2016-03-11 23:48:35.026 Error: Notification sent (lms) => Failed
2016-03-11 23:48:35.611 Notification sent (pushbullet) => Success
2016-03-11 23:48:35.611 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_main.lua
2016-03-11 23:48:34.887 (Virtual) Lighting 1 (DzVents)
2016-03-11 23:49:00.055 LUA: Phones: Peter's phone found 1h 18m ago
2016-03-11 23:49:00.055 LUA: Phones: Rita's phone found 3h 15m ago
2016-03-11 23:4
As soon as I changed the trigger to DzVents' idx, 95 in this case, the events were not recognized as evidenced by log snippet below. Note I have disabled all but the dummy hardware device in domoticz for these tests to reduce the log file clutter. I trust this didn't affect the way the dzVents worked.

I noticed the dzVents doesn't display what I might have expected for the device id in the log entry timed at 23:48:32.616 in the log file extract above. Might this point to the problem? I dread to think that your gutters have been blocked all this time simply because dzVents doesn't trigger on device IDs.

Code: Select all

2016-03-11 23:54:00.401 LUA: Phones: Rita's phone found 3h 20m ago
2016-03-11 23:54:00.412 LUA: Handle timer events
2016-03-11 23:55:00.120 LUA: Phones: Peter's phone found 1h 24m ago
2016-03-11 23:55:00.120 LUA: Phones: Rita's phone found 3h 21m ago
2016-03-11 23:55:00.180 LUA: Handle timer events
2016-03-11 23:55:35.057 (Virtual) Lighting 1 (DzVents)
2016-03-11 23:55:37.349 (Virtual) Lighting 1 (DzVents)
2016-03-11 23:55:41.019 (Virtual) Lighting 1 (DzVents)
2016-03-11 23:56:00.379 LUA: Phones: Peter's phone found 1h 25m ago
2016-03-11 23:56:00.379 LUA: Phones: Rita's phone found 3h 22m ago
2016-03-11 23:56:00.390 LUA: Handle timer events
If there is a specific test you want me to do I'd be happy to oblige.

Kind regards, enjoy your weekend, Danny.

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 1:41
by simonrg
dakipro wrote:dear sir this is brilliant!
As a developer myself (php though) I was looking at some way to organize the code and group functions, but this is exactly what I needed!
I managed to make "night motion lights" script that will turn on the lights on motion and then turn them off after ~2min. All that in basically 10 lines, wheres before it would be 30 with a bunch of if elses and parsing and things..

Code: Select all

on = {
		39,                         -- index of the device - 39 book motion
		['timer'] = 'every other minute',  -- minutes: xx:00, xx:02, xx:04, ..., xx:58
	},
	execute = function(domoticz, mySwitch) -- see readme for what you get
		if (domoticz.devices[39].state == 'On') then
			domoticz.devices[36].switchOn()
		end
		if(domoticz.devices[39].minutesAgo>1) then
			domoticz.devices[36].switchOff()
		end
	end
And what is really beautiful is that I can run both motion controlled ON event and the timer controlled OFF event in the same script.

Regarding of which, do you have a comment about a better way to write my script? Maybe you can make a folder on github and collect some of the common examples there?

I also tried using the mySwitch.state in the first if, it works on sensor change but when script runs on timer it never reaches the second if, I guess it brakes because the mySwitch doesn't exist on timer calls.
Nice use of these concepts, my only comment would be it would be a lot more readable if you used names rather than ids.

In terms of improvement as written now it will keep on turning the light off for ever every other minute, so should really switch off only between 1 and 3 minutes:

Code: Select all

if(domoticz.devices[39].minutesAgo>1) and (domoticz.devices[39].minutesAgo<3) then
I'd like to think I could extend it to work with all your pirs, by naming the pir associated with a light as PIRlight as below:

Code: Select all

on = {
		'PIR*',                         -- name of all PIRs
		['timer'] = 'every other minute',  -- minutes: xx:00, xx:02, xx:04, ..., xx:58
	},
	execute = function(domoticz, mySwitch) -- see readme for what you get
		if (mySwitch == 'On') then
			domoticz.devices[string.sub(mySwitch.name,4)].switchOn()
		end
		-- Won't work as mySwitch is not defined in timer case
		if(mySwitch.minutesAgo>1) then
			domoticz.devices[string.sub(mySwitch.name,4)].switchOff()
		end
	end
}
The first if will work as mySwitch is defined, the second won't :cry: .

Something like this might work, but is now more complex as you have to go through all the devices to find the PIR ones.

Code: Select all

on = {
		'PIR*',                         -- name of all PIRs
		['timer'] = 'every other minute',  -- minutes: xx:00, xx:02, xx:04, ..., xx:58
	},
	execute = function(domoticz, mySwitch) -- see readme for what you get
		if (mySwitch == 'On') then
			domoticz.devices[string.sub(mySwitch.name,4)].switchOn()
		else
			for i, v in pairs(domoticz.devices) do
			tc = tostring(i)
			v = i:sub(1,3)
				if (v == 'PIR') then
					if(domoticz.devices[tc].minutesAgo>1) and (domoticz.devices[tc].minutesAgo<3) then
						domoticz.devices[string.sub(tc,4)].switchOff()
					end
				end
			end
		end
	end
}

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 10:30
by dannybloe
Just fixed a wild card bug that was in 0.9.8. Sorry about that :(
Please switch to 0.9.9

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 10:57
by commodore white
Interesting. With debug=3, I got this extract from the log file when I used 95 as the device id as a trigger for Dannys' blocked gutter script. As far as I can tell from the log, dzVents correctly identified the device as "DzVents" though it didn't action my test.lua script.

The Off event was identified at 10:32:46.408.

What follows is an extract from the log:
Spoiler: show
2016-03-12 10:32:46.363 LUA: Http data for device DzVents
2016-03-12 10:32:46.363 LUA: =========================
2016-03-12 10:32:46.363 LUA: LevelInt: 0
2016-03-12 10:32:46.363 LUA: Type: Lighting 1
2016-03-12 10:32:46.363 LUA: StrParam2:
2016-03-12 10:32:46.363 LUA: Status: Off
2016-03-12 10:32:46.364 LUA: Favorite: 0
2016-03-12 10:32:46.364 LUA: StrParam1:
2016-03-12 10:32:46.364 LUA: Timers: false
2016-03-12 10:32:46.364 LUA: HardwareTypeVal: 15
2016-03-12 10:32:46.364 LUA: TypeImg: lightbulb
2016-03-12 10:32:46.364 LUA: SwitchType: On/Off
2016-03-12 10:32:46.364 LUA: Name: DzVents
2016-03-12 10:32:46.365 LUA: BatteryLevel: 255
2016-03-12 10:32:46.365 LUA: Unit: 1
2016-03-12 10:32:46.365 LUA: HaveTimeout: false
2016-03-12 10:32:46.365 LUA: HardwareType: Dummy (Does nothing, use for virtual switches only)
2016-03-12 10:32:46.365 LUA: AddjValue2: 0
2016-03-12 10:32:46.365 LUA: SignalLevel: 7
2016-03-12 10:32:46.366 LUA: Level: 0
2016-03-12 10:32:46.366 LUA: LastUpdate: 2016-03-11 23:55:41
2016-03-12 10:32:46.366 LUA: Description:
2016-03-12 10:32:46.366 LUA: UsedByCamera: false
2016-03-12 10:32:46.366 LUA: AddjMulti2: 1
2016-03-12 10:32:46.366 LUA: AddjValue: 0
2016-03-12 10:32:46.366 LUA: ID: 68
2016-03-12 10:32:46.367 LUA: CustomImage: 0
2016-03-12 10:32:46.367 LUA: ShowNotifications: true
2016-03-12 10:32:46.367 LUA: AddjMulti: 1
2016-03-12 10:32:46.367 LUA: HardwareID: 2
2016-03-12 10:32:46.367 LUA: idx: 95
2016-03-12 10:32:46.367 LUA: HaveGroupCmd: false
2016-03-12 10:32:46.368 LUA: SubType: RisingSun
2016-03-12 10:32:46.368 LUA: Used: 1
2016-03-12 10:32:46.368 LUA: SwitchTypeVal: 0
2016-03-12 10:32:46.368 LUA: MaxDimLevel: 0
2016-03-12 10:32:46.368 LUA: Data: Off
2016-03-12 10:32:46.368 LUA: HardwareName: Virtual
2016-03-12 10:32:46.368 LUA: YOffset: 0
2016-03-12 10:32:46.369 LUA: XOffset: 0
2016-03-12 10:32:46.369 LUA: PlanIDs: table: 0xaddc9d40
2016-03-12 10:32:46.369 LUA: PlanID: 0
2016-03-12 10:32:46.369 LUA: IsSubDevice: false
2016-03-12 10:32:46.369 LUA: Notifications: false
2016-03-12 10:32:46.370 LUA: Image: Light
2016-03-12 10:32:46.370 LUA: HaveDimmer: false
2016-03-12 10:32:46.370 LUA: Protected: false
2016-03-12 10:32:46.397 LUA: Found module in scripts folder: devicetest
2016-03-12 10:32:46.397 LUA: Found module in scripts folder: example
2016-03-12 10:32:46.397 LUA: Found module in scripts folder: test
2016-03-12 10:32:46.408 LUA: Event in devicechanged: DzVents value: Off
2016-03-12 10:32:46.408 LUA: findBindingByEvent: DzVents
2016-03-12 10:32:46.046 (Virtual) Lighting 1 (DzVents)
2016-03-12 10:33:00.537 LUA: MaxDimLevel: 0
2016-03-12 10:33:00.537 LUA: ShowNotifications: true
2016-03-12 10:33:00.537 LUA: PlanIDs: table: 0x150db00
2016-03-12 10:33:00.537 LUA: Favorite: 0
2016-03-12 10:33:00.537 LUA: Description:
2016-03-12 10:33:00.537 LUA: ID: 3E317D
2016-03-12 10:33:00.538 LUA: AddjValue: 0
2016-03-12 10:33:00.538 LUA: CustomImage: 0
2016-03-12 10:33:00.538 LUA: Http data for device mscDoor2Alarm
2016-03-12 10:33:00.538 LUA: =========================
2016-03-12 10:33:00.538 LUA: Unit: 0
2016-03-12 10:33:00.539 LUA: HardwareTypeVal: 1
2016-03-12 10:33:00.539 LUA: Notifications: false
2016-03-12 10:33:00.539 LUA: HardwareName: RFXCOM
2016-03-12 10:33:00.539 LUA: AddjMulti: 1
2016-03-12 10:33:00.539 LUA: TypeImg: security
2016-03-12 10:33:00.539 LUA: HaveTimeout: false
2016-03-12 10:33:00.550 LUA: PlanID: 0
2016-03-12 10:33:00.550 LUA: Name: mscDoor2Alarm
2016-03-12 10:33:00.550 LUA: BatteryLevel: 100
2016-03-12 10:33:00.550 LUA: HaveDimmer: false
2016-03-12 10:33:00.550 LUA: HardwareID: 3
2016-03-12 10:33:00.550 LUA: SwitchType: Security
2016-03-12 10:33:00.551 LUA: HardwareType: RFXCOM - RFXtrx433 USB 433.92MHz Transceiver
2016-03-12 10:33:00.551 LUA: Protected: false
2016-03-12 10:33:00.551 LUA: Data: Alarm
2016-03-12 10:33:00.551 LUA: AddjMulti2: 1
2016-03-12 10:33:00.551 LUA: AddjValue2: 0
2016-03-12 10:33:00.551 LUA: Status: Alarm
2016-03-12 10:33:00.552 LUA: StrParam2:
2016-03-12 10:33:00.552 LUA: SubType: X10 security
2016-03-12 10:33:00.552 LUA: Type: Security
2016-03-12 10:33:00.552 LUA: YOffset: 0
2016-03-12 10:33:00.552 LUA: LastUpdate: 2016-03-12 09:47:43
2016-03-12 10:33:00.552 LUA: XOffset: 0
2016-03-12 10:33:00.552 LUA: Used: 1
2016-03-12 10:33:00.553 LUA: idx: 90
2016-03-12 10:33:00.553 LUA: SwitchTypeVal: 0
2016-03-12 10:33:00.553 LUA: HaveGroupCmd: false
2016-03-12 10:33:00.553 LUA: StrParam1:
2016-03-12 10:33:00.553 LUA: Timers: false
2016-03-12 10:33:00.553 LUA: SignalLevel: 7
2016-03-12 10:33:00.553 LUA: MaxDimLevel: 0
2016-03-12 10:33:00.554 LUA: ShowNotifications: true
2016-03-12 10:33:00.554 LUA: PlanIDs: table: 0x150db68
2016-03-12 10:33:00.554 LUA: Favorite: 0
2016-03-12 10:33:00.554 LUA: Description:
2016-03-12 10:33:00.554 LUA: ID: BEB1FD
2016-03-12 10:33:00.555 LUA: AddjValue: 0
2016-03-12 10:33:00.555 LUA: CustomImage: 0
2016-03-12 10:33:00.555 LUA: Http data for device DzVents
2016-03-12 10:33:00.555 LUA: =========================

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 11:41
by dakipro
Thanks, I noticed in logs that I need a bit more finetuning.
However, I think I have found the bug (or a feature :) ). In the "night light" example from above I have several sensors on one MySensors node.
Not sure if you use it, but I have one node that has multiple sensors, f.eks. 39 is PIR sensor on one node, and there is also 40 that is Lux sensor, 41 that is temperature sensor. When 39 is triggered, it is available in domoticz.devices[39], but 40 and 41 are not.
Here is the full debug I got in log
Spoiler: show
2016-03-12 00:41:00.151 LUA: -----------------------------------------------------
2016-03-12 00:41:15.304 LUA: Notifications: false
2016-03-12 00:41:15.304 LUA: AddjValue: 0
2016-03-12 00:41:15.304 LUA: Name: Book Light Level
2016-03-12 00:41:15.304 LUA: LastUpdate: 2016-03-12 00:20:39
2016-03-12 00:41:15.304 LUA: SubType: Lux
2016-03-12 00:41:15.304 LUA: HardwareType: MySensors Gateway with LAN interface
2016-03-12 00:41:15.304 LUA: Favorite: 0
2016-03-12 00:41:15.304 LUA: Protected: false
2016-03-12 00:41:15.304 LUA: Data: 30 Lux
2016-03-12 00:41:15.304 LUA: Http data for device Book TempHum
2016-03-12 00:41:15.304 LUA: =========================
2016-03-12 00:41:15.304 LUA: DewPoint: -2.58
2016-03-12 00:41:15.304 LUA: HardwareName: MySensors
2016-03-12 00:41:15.304 LUA: HumidityStatus: Dry
2016-03-12 00:41:15.304 LUA: HardwareID: 6
2016-03-12 00:41:15.304 LUA: AddjMulti2: 1
2016-03-12 00:41:15.304 LUA: AddjMulti: 1
2016-03-12 00:41:15.304 LUA: BatteryLevel: 100
2016-03-12 00:41:15.305 LUA: Timers: false
2016-03-12 00:41:15.305 LUA: ID: 0103
2016-03-12 00:41:15.305 LUA: HaveTimeout: false
2016-03-12 00:41:15.305 LUA: Used: 1
2016-03-12 00:41:15.305 LUA: Notifications: false
2016-03-12 00:41:15.305 LUA: SubType: WTGR800
2016-03-12 00:41:15.305 LUA: Favorite: 0
2016-03-12 00:41:15.305 LUA: SignalLevel: -
2016-03-12 00:41:15.305 LUA: Humidity: 20
2016-03-12 00:41:15.305 LUA: PlanIDs: table: 0x6cdc82d8
2016-03-12 00:41:15.305 LUA: AddjValue2: 0
2016-03-12 00:41:15.305 LUA: XOffset: 77
2016-03-12 00:41:15.305 LUA: HardwareType: MySensors Gateway with LAN interface
2016-03-12 00:41:15.305 LUA: CustomImage: 0
2016-03-12 00:41:15.305 LUA: HardwareTypeVal: 42
2016-03-12 00:41:15.305 LUA: Description:
2016-03-12 00:41:15.305 LUA: idx: 41
2016-03-12 00:41:15.305 LUA: Unit: 0
2016-03-12 00:41:15.305 LUA: YOffset: 383
2016-03-12 00:41:15.305 LUA: TypeImg: temperature
2016-03-12 00:41:15.306 LUA: Type: Temp + Humidity
2016-03-12 00:41:15.306 LUA: AddjValue: 0
2016-03-12 00:41:15.306 LUA: Temp: 21.3
2016-03-12 00:41:15.306 LUA: ShowNotifications: true
2016-03-12 00:41:15.306 LUA: LastUpdate: 2016-03-12 00:29:19
2016-03-12 00:41:15.306 LUA: PlanID: 2
2016-03-12 00:41:15.306 LUA: Name: Book TempHum
2016-03-12 00:41:15.306 LUA: Protected: false
2016-03-12 00:41:15.306 LUA: Data: 21.3 C, 20 %
2016-03-12 00:41:15.306 LUA: Http data for device Master PC
2016-03-12 00:41:15.306 LUA: =========================
2016-03-12 00:41:15.306 LUA: Type: Lighting 2
2016-03-12 00:41:15.306 LUA: HardwareName: Wake on LAN
2016-03-12 00:41:15.306 LUA: HardwareID: 7
2016-03-12 00:41:15.306 LUA: AddjMulti2: 1
2016-03-12 00:41:15.306 LUA: AddjMulti: 1
2016-03-12 00:41:15.306 LUA: HaveGroupCmd: true
2016-03-12 00:41:15.306 LUA: BatteryLevel: 255
2016-03-12 00:41:15.306 LUA: HaveDimmer: true
2016-03-12 00:41:15.306 LUA: Timers: false
2016-03-12 00:41:15.306 LUA: ID: 0000006
2016-03-12 00:41:15.306 LUA: StrParam1:
2016-03-12 00:41:15.307 LUA: HaveTimeout: false
2016-03-12 00:41:15.307 LUA: Used: 1
2016-03-12 00:41:15.307 LUA: Notifications: false
2016-03-12 00:41:15.307 LUA: SubType: AC
2016-03-12 00:41:15.307 LUA: Favorite: 1
2016-03-12 00:41:15.307 LUA: idx: 43
2016-03-12 00:41:15.307 LUA: XOffset: 0
2016-03-12 00:41:15.307 LUA: IsSubDevice: false
2016-03-12 00:41:15.307 LUA: YOffset: 0
2016-03-12 00:41:15.307 LUA: PlanIDs: table: 0x6cdc8328
2016-03-12 00:41:15.307 LUA: LevelInt: 0
2016-03-12 00:41:15.307 LUA: SignalLevel: -
2016-03-12 00:41:15.307 LUA: AddjValue2: 0
2016-03-12 00:41:15.307 LUA: SwitchType: Push On Button
2016-03-12 00:41:15.307 LUA: HardwareType: Wake-on-LAN
2016-03-12 00:41:15.307 LUA: CustomImage: 0
2016-03-12 00:41:15.307 LUA: TypeImg: push
2016-03-12 00:41:15.307 LUA: HardwareTypeVal: 29
2016-03-12 00:41:15.307 LUA: SwitchTypeVal: 9
2016-03-12 00:41:15.307 LUA: Description:
2016-03-12 00:41:15.307 LUA: StrParam2:
2016-03-12 00:41:15.308 LUA: Status:
2016-03-12 00:41:15.308 LUA: InternalState: On
2016-03-12 00:41:15.308 LUA: Unit: 1
2016-03-12 00:41:15.308 LUA: ShowNotifications: true
2016-03-12 00:41:15.308 LUA: LastUpdate: 2016-03-09 22:52:01
2016-03-12 00:41:15.308 LUA: Level: 0
2016-03-12 00:41:15.308 LUA: AddjValue: 0
2016-03-12 00:41:15.308 LUA: PlanID: 0
2016-03-12 00:41:15.308 LUA: Name: Master PC
2016-03-12 00:41:15.308 LUA: Image: Light
2016-03-12 00:41:15.308 LUA: MaxDimLevel: 15
2016-03-12 00:41:15.308 LUA: UsedByCamera: false
2016-03-12 00:41:15.308 LUA: Protected: false
2016-03-12 00:41:15.308 LUA: Data: On
2016-03-12 00:41:15.321 LUA: Found module in scripts folder: example
2016-03-12 00:41:15.321 LUA: Found module in scripts folder: nightLight
2016-03-12 00:41:15.323 LUA: Event in devicechanged: LivingRoom Light value: On
2016-03-12 00:41:15.323 LUA: findBindingByEvent: LivingRoom Light
Maybe I am doing something wrong, I tried
print(domoticz.devices[40].state)
print(domoticz.devices[40].value)
print(domoticz.devices[40].nValue)
but all were empty (even thought in the log I do see lux sensor). Any tips?

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 15:01
by commodore white
AFAIK your PIR event code will get triggered when the light is turned off or on so you get more events than you might expect.

Consider testing if a light is on before you turn it off c.f.

if your light is on AND it last changed state > 5 minutes ago then turn it off

Peter

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 18:34
by dannybloe
commodore white wrote:Looking forward to the update.
[*] Added dimTo(percentage) method to control dimmers.
Makes sence. Also be nice to have ".duration(nnn)" to define how long the fade should take

Peter
Huh, is this supported by Domoticz? If so then I can easily add it.

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 18:51
by dannybloe
commodore white wrote:Looking forward to the update.
  • Added device.switchSelector method on a device to set a selector switch.
    No idea what this is but I bet its going to be handy
You can create a dummy selector switch with a predefined set of states. So you can make such a switch with states like 'low', 'medium', 'high'. In the UI you get three little buttons. You could then create an event script that when one of the values is clicked, it controls a dimmer:

Code: Select all

return {
	active = true,
	on = {
		'FastDimmer'
	},

	execute = function(domoticz, device)
		if (device.level == 'low') then
			domoticz.devices['My dimmer'].dimTo(10)
		elseif (device.level == 'medium') then
			domoticz.devices['My dimmer'].dimTo(50)
		elseif (device.level == 'high') then
			domoticz.devices['My dimmer'].dimTo(100)
		end
	end
}
Added http request data from Domoticz to devices. Now you can check the battery level and switch type and more.
Wonder if this will place a lower limit on the spec of cpu?
Well, I hope that this shouldn't be a problem since you only have to do this every once in a while. I've set it to once every half hour but this could easily be changed to like every 24 hours. But if you want a more accurate battery reading then this may be too long.

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 18:56
by dannybloe
simonrg wrote:
thorbj wrote:Thanks! It's a bit clearer now :)
But what if I have some switches that I want to be triggered ONLY if other switches are off - and vice versa. Can this be done with this script?

In my case I have the switch "Overstyr" (means override). If this is off, I want the script to operate as normal based on given conditions for lux, time of day, movement, etc. But if the override-switch is on, I want the script to do nothing.
Once your script has been triggered by a single device changing, then you decide whether your script carries out an action you can do any other test you want inside your function just like you do with normal scripts. You can either use the logic you are currently using with otherdevices etc. or you can convert to the objects from dzVents.
Exactly. All dzVents does is make it easier to define triggers and provide easy access to all the data coming from Domoticz. But your algorithms have to be created by yourself of course ;-).

So I'd create a event script for when the conditions change and in there first check if the override switch is on or off and then decide to handle upon it or not.

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 19:03
by dannybloe
commodore white wrote: It'll be interesting to see how long it takes dzVents to gain traction. Might need a catchier name first though.
Peter
:D even more catchier? Hehe.. ideas are welcome of course.

But indeed it could easily be accompanied with Domoticz itself. Doesn't harm any script already out there.

Re: version 0.9.8 released

Posted: Saturday 12 March 2016 19:30
by dannybloe
simonrg wrote:
dannybloe wrote:Btw, fetching extra device data from Domoticz like battery info only works on linux file systems. It requires bash, curl and sed and lots of pipes ;).
Brilliant, 8 piped seds is quite something and spawning gets over the 10 second rule.
Does it? It was intended that the script execution was detached from the process so Domoticz wouldn't know that it could take a while. I didn't see this happening here. Domoticz doesn't notice anything over here.
Or maybe I didn't get you here ;-).

A little suggestion I think would be an improvment, is to make the request DomoticzData function into a normal time script, call it with the normal time mechanism, this would also allow users to attach it to a virtual switch and force an update by simply operating the switch.
Well, by default I want this to be seamless for users. But I created a method on the domoticz object

Code: Select all

domoticz.fetchHttpDomoticzData()
so you can create a script yourself and tie it to a switch action or something like that. You can disable the automatic fetching in dzVents_settings.lua.
The various json libraries are fairly efficient and in the past I had no problem doing dumb dumps even on a Raspberry Pi B as per @Westcott, the slow bit I found was file passing to the os.execute and back, which I overcame by using Lua http libaries.

Installation of the http, socket, ssl and json libraries I use for Raspberry Pi is described here - http://www.domoticz.com/wiki/Remote_Con ... _Libraries
I didn't want anybody to have to install libraries etc so I chose for basic bash processing and spawn it in the background. Then use plain old Lua require to load the table. Then you use native Lua parsing which is native C code so faster than a Lua json parser. The sed commands are of course not flexible but it is pretty fast to turn the json into Lua code without any parsing and processing. Works for now until someone changes the json structure of course.

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 19:39
by dannybloe
dakipro wrote:dear sir this is brilliant!
Takes a bow..
As a developer myself (php though) I was looking at some way to organize the code and group functions, but this is exactly what I needed!
I managed to make "night motion lights" script that will turn on the lights on motion and then turn them off after ~2min. All that in basically 10 lines, wheres before it would be 30 with a bunch of if elses and parsing and things..

Code: Select all

on = {
		39,                         -- index of the device - 39 book motion
		['timer'] = 'every other minute',  -- minutes: xx:00, xx:02, xx:04, ..., xx:58
	},
	execute = function(domoticz, mySwitch) -- see readme for what you get
		if (domoticz.devices[39].state == 'On') then
			domoticz.devices[36].switchOn()
		end
		if(domoticz.devices[39].minutesAgo>1) then
			domoticz.devices[36].switchOff()
		end
	end
And what is really beautiful is that I can run both motion controlled ON event and the timer controlled OFF event in the same script.

Regarding of which, do you have a comment about a better way to write my script? Maybe you can make a folder on github and collect some of the common examples there?

I also tried using the mySwitch.state in the first if, it works on sensor change but when script runs on timer it never reaches the second if, I guess it brakes because the mySwitch doesn't exist on timer calls.
Correctly noted. Of course, when the timer calls the script, there is no device that was changed so dzVents cannot guess what you need here. But you can easily check for this:

Code: Select all

if (mySwitch==nil) then
	-- timer was triggered
	-- get my switch
	mySwitch = domoticz.devices[39]
end

-- the rest of your code

Good idea about examples. I may create a subfolder with some examples in the very near future.

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 19:45
by NietGiftig
dannybloe wrote:Good idea about examples. I may create a subfolder with some examples in the very near future.
Oh, would really like this.
It has evolved a lot since I gave a reaction on the first version.

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 19:57
by dannybloe
dakipro wrote:Thanks, I noticed in logs that I need a bit more finetuning.
However, I think I have found the bug (or a feature :) ). In the "night light" example from above I have several sensors on one MySensors node.
Not sure if you use it, but I have one node that has multiple sensors, f.eks. 39 is PIR sensor on one node, and there is also 40 that is Lux sensor, 41 that is temperature sensor. When 39 is triggered, it is available in domoticz.devices[39], but 40 and 41 are not.
Here is the full debug I got in log
Spoiler: show
2016-03-12 00:41:00.151 LUA: -----------------------------------------------------
2016-03-12 00:41:15.304 LUA: Notifications: false
2016-03-12 00:41:15.304 LUA: AddjValue: 0
2016-03-12 00:41:15.304 LUA: Name: Book Light Level
2016-03-12 00:41:15.304 LUA: LastUpdate: 2016-03-12 00:20:39
2016-03-12 00:41:15.304 LUA: SubType: Lux
2016-03-12 00:41:15.304 LUA: HardwareType: MySensors Gateway with LAN interface
2016-03-12 00:41:15.304 LUA: Favorite: 0
2016-03-12 00:41:15.304 LUA: Protected: false
2016-03-12 00:41:15.304 LUA: Data: 30 Lux
2016-03-12 00:41:15.304 LUA: Http data for device Book TempHum
2016-03-12 00:41:15.304 LUA: =========================
2016-03-12 00:41:15.304 LUA: DewPoint: -2.58
2016-03-12 00:41:15.304 LUA: HardwareName: MySensors
2016-03-12 00:41:15.304 LUA: HumidityStatus: Dry
2016-03-12 00:41:15.304 LUA: HardwareID: 6
2016-03-12 00:41:15.304 LUA: AddjMulti2: 1
2016-03-12 00:41:15.304 LUA: AddjMulti: 1
2016-03-12 00:41:15.304 LUA: BatteryLevel: 100
2016-03-12 00:41:15.305 LUA: Timers: false
2016-03-12 00:41:15.305 LUA: ID: 0103
2016-03-12 00:41:15.305 LUA: HaveTimeout: false
2016-03-12 00:41:15.305 LUA: Used: 1
2016-03-12 00:41:15.305 LUA: Notifications: false
2016-03-12 00:41:15.305 LUA: SubType: WTGR800
2016-03-12 00:41:15.305 LUA: Favorite: 0
2016-03-12 00:41:15.305 LUA: SignalLevel: -
2016-03-12 00:41:15.305 LUA: Humidity: 20
2016-03-12 00:41:15.305 LUA: PlanIDs: table: 0x6cdc82d8
2016-03-12 00:41:15.305 LUA: AddjValue2: 0
2016-03-12 00:41:15.305 LUA: XOffset: 77
2016-03-12 00:41:15.305 LUA: HardwareType: MySensors Gateway with LAN interface
2016-03-12 00:41:15.305 LUA: CustomImage: 0
2016-03-12 00:41:15.305 LUA: HardwareTypeVal: 42
2016-03-12 00:41:15.305 LUA: Description:
2016-03-12 00:41:15.305 LUA: idx: 41
2016-03-12 00:41:15.305 LUA: Unit: 0
2016-03-12 00:41:15.305 LUA: YOffset: 383
2016-03-12 00:41:15.305 LUA: TypeImg: temperature
2016-03-12 00:41:15.306 LUA: Type: Temp + Humidity
2016-03-12 00:41:15.306 LUA: AddjValue: 0
2016-03-12 00:41:15.306 LUA: Temp: 21.3
2016-03-12 00:41:15.306 LUA: ShowNotifications: true
2016-03-12 00:41:15.306 LUA: LastUpdate: 2016-03-12 00:29:19
2016-03-12 00:41:15.306 LUA: PlanID: 2
2016-03-12 00:41:15.306 LUA: Name: Book TempHum
2016-03-12 00:41:15.306 LUA: Protected: false
2016-03-12 00:41:15.306 LUA: Data: 21.3 C, 20 %
2016-03-12 00:41:15.306 LUA: Http data for device Master PC
2016-03-12 00:41:15.306 LUA: =========================
2016-03-12 00:41:15.306 LUA: Type: Lighting 2
2016-03-12 00:41:15.306 LUA: HardwareName: Wake on LAN
2016-03-12 00:41:15.306 LUA: HardwareID: 7
2016-03-12 00:41:15.306 LUA: AddjMulti2: 1
2016-03-12 00:41:15.306 LUA: AddjMulti: 1
2016-03-12 00:41:15.306 LUA: HaveGroupCmd: true
2016-03-12 00:41:15.306 LUA: BatteryLevel: 255
2016-03-12 00:41:15.306 LUA: HaveDimmer: true
2016-03-12 00:41:15.306 LUA: Timers: false
2016-03-12 00:41:15.306 LUA: ID: 0000006
2016-03-12 00:41:15.306 LUA: StrParam1:
2016-03-12 00:41:15.307 LUA: HaveTimeout: false
2016-03-12 00:41:15.307 LUA: Used: 1
2016-03-12 00:41:15.307 LUA: Notifications: false
2016-03-12 00:41:15.307 LUA: SubType: AC
2016-03-12 00:41:15.307 LUA: Favorite: 1
2016-03-12 00:41:15.307 LUA: idx: 43
2016-03-12 00:41:15.307 LUA: XOffset: 0
2016-03-12 00:41:15.307 LUA: IsSubDevice: false
2016-03-12 00:41:15.307 LUA: YOffset: 0
2016-03-12 00:41:15.307 LUA: PlanIDs: table: 0x6cdc8328
2016-03-12 00:41:15.307 LUA: LevelInt: 0
2016-03-12 00:41:15.307 LUA: SignalLevel: -
2016-03-12 00:41:15.307 LUA: AddjValue2: 0
2016-03-12 00:41:15.307 LUA: SwitchType: Push On Button
2016-03-12 00:41:15.307 LUA: HardwareType: Wake-on-LAN
2016-03-12 00:41:15.307 LUA: CustomImage: 0
2016-03-12 00:41:15.307 LUA: TypeImg: push
2016-03-12 00:41:15.307 LUA: HardwareTypeVal: 29
2016-03-12 00:41:15.307 LUA: SwitchTypeVal: 9
2016-03-12 00:41:15.307 LUA: Description:
2016-03-12 00:41:15.307 LUA: StrParam2:
2016-03-12 00:41:15.308 LUA: Status:
2016-03-12 00:41:15.308 LUA: InternalState: On
2016-03-12 00:41:15.308 LUA: Unit: 1
2016-03-12 00:41:15.308 LUA: ShowNotifications: true
2016-03-12 00:41:15.308 LUA: LastUpdate: 2016-03-09 22:52:01
2016-03-12 00:41:15.308 LUA: Level: 0
2016-03-12 00:41:15.308 LUA: AddjValue: 0
2016-03-12 00:41:15.308 LUA: PlanID: 0
2016-03-12 00:41:15.308 LUA: Name: Master PC
2016-03-12 00:41:15.308 LUA: Image: Light
2016-03-12 00:41:15.308 LUA: MaxDimLevel: 15
2016-03-12 00:41:15.308 LUA: UsedByCamera: false
2016-03-12 00:41:15.308 LUA: Protected: false
2016-03-12 00:41:15.308 LUA: Data: On
2016-03-12 00:41:15.321 LUA: Found module in scripts folder: example
2016-03-12 00:41:15.321 LUA: Found module in scripts folder: nightLight
2016-03-12 00:41:15.323 LUA: Event in devicechanged: LivingRoom Light value: On
2016-03-12 00:41:15.323 LUA: findBindingByEvent: LivingRoom Light
Maybe I am doing something wrong, I tried
print(domoticz.devices[40].state)
print(domoticz.devices[40].value)
print(domoticz.devices[40].nValue)
but all were empty (even thought in the log I do see lux sensor). Any tips?
First of all, in your script, checking for last update should be like this:

Code: Select all

domoticz.devices[39].lastUpdate.minutesAgo...
minutesAgo is an attribute of the lastUpdate object.

Secondly, I noticed that Domoticz doesn't pass the lux value as separate attribute (like otherdevices_lux) but as part of the sValue set. dzVents puts this information in de rawData attribute of the device. You can access it like this:

Code: Select all

domoticz.devices[40].rawData[1] -- For my sensor lux is in the first item, some devices have multiple raw values here hence the array.
I know that the debug info suggests that you can access the value on the device directly but that is information coming from the http data. The information in there is not live (could be 30 minutes old). I specifically take sensor readings and other states from the information that is given from the Domoticz event system because they hold the latest value. So yeah, if you cannot find what you are looking for on a device... check the rawData array (Lua table).

I'll update the documentation for this.

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 21:14
by WebStar
For the scripts, I made some changes to be able to sent also a sound with a notification.

I'm new on github, so i'm not sure if i've did it right to create a pull request.
But I tried to help and make the scripts even better than they already are :)

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 21:32
by dannybloe
Excellent. Just merged it and made a new release 0.9.10.

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 21:38
by dakipro
Thanks about the LUX, it works as raw data. How to access temperature, also like raw?
I tried briefly and didn't find it, althought I do not need it at the moment

Here is the latest "night lights" version, might be useful for someone as a start script

Code: Select all

return {
	active = true,                  -- set to true to activate this script
	on = {
		39,                         	-- index of the device - 39 book PIR
		['timer'] = 'every 2 minutes',  -- minutes: xx:00, xx:02, xx:04, ..., xx:58
		
	},

	execute = function(domoticz, motionSensor) 
		--initialize variables
		if (motionSensor==nil) then
		   -- timer was triggered, get my switch
		   motionSensor = domoticz.devices[39] --book PIR
		end
		luxLightValue = domoticz.devices[40].rawData[1] 
		tvLED = domoticz.devices[35]
		livingRoomLights = domoticz.devices[36]
		--settings
		dimToPercentages = 20
		waitingMinutes = 1
		minimumAmbientLight = 50
		
		-- motion triggered
		if (tonumber(luxLightValue) <minimumAmbientLight and motionSensor.state == 'On') then
			print('motion detected, tun ON the light')
			tvLED.dimTo(dimToPercentages)
			livingRoomLights.switchOn()
			
		end
		
		-- time passed since last movement detected
		if(tvLED.state == "On" and motionSensor.lastUpdate.minutesAgo>waitingMinutes) then
			print('NO motion for long enought, tun OFF the light')
			tvLED.switchOff()
			livingRoomLights.switchOff()
		end
	end
}
Now I go to scripts folder with a smile, vs before when I was going there... without a smile :)

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Saturday 12 March 2016 22:03
by commodore white
dannybloe wrote:
commodore white wrote:Looking forward to the update.
[*] Added dimTo(percentage) method to control dimmers.
Makes sence. Also be nice to have ".duration(nnn)" to define how long the fade should take

Peter
Huh, is this supported by Domoticz? If so then I can easily add it.
Yeah. Sorry about that. Responded to your release notes but it was a while before I read your README file where I read that you weren't going to be implementing it. Seems such a good idea too. Sigh.

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Posted: Sunday 13 March 2016 12:23
by Derik
Looks great
What is the simpelst way to start...