Some LUA magic: one script to rule them all..

Moderator: leecollings

Sicco28
Posts: 7
Joined: Sunday 27 October 2013 13:42
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Some LUA magic: one script to rule them all..

Post by Sicco28 »

Could it be you forgot to include the event_helpers.lua script in the latest: script-v0.9.2.zip ?

... the initial version 0.9 is returning an error:
Error: EventSystem: in /home/domoticz/scripts/lua/script_time_main.lua: /home/domoticz/scripts/lua/event_helpers.lua:162: bad argument #1 to 'pairs' (table expected, got nil)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Some LUA magic: one script to rule them all..

Post by dannybloe »

Crap.. ur are right..
Attachments
v0.9.2.zip
(6.21 KiB) Downloaded 73 times
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Sicco28
Posts: 7
Joined: Sunday 27 October 2013 13:42
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Some LUA magic: one script to rule them all..

Post by Sicco28 »

Yup, that worked, thanks! I will continue to implement and test.


Sent from my iPhone using Tapatalk
JustME125
Posts: 31
Joined: Monday 27 April 2015 16:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.4547
Location: The Netherlands
Contact:

Re: Some LUA magic: one script to rule them all..

Post by JustME125 »

I see something that will eat up a lot of the quality time with the misses [GRINNING FACE WITH SMILING EYES]. Going to give it a try soon. Expect some silly questions as I am no programmer ;)

Verstuurd vanaf mijn HTC One_M8 met Tapatalk
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Some LUA magic: one script to rule them all..

Post by dannybloe »

I found a bug that prevented execution of the scripts as soon as one script was set to be inactive. Fixed that one.

Also added something extra: the device scripts not only receive the value but also the name and the index of the device triggering the script. This again saves you some code and is handy if you have multiple triggers/devices triggering one script:

Code: Select all

return {
	active = true,
	on = {
		47, -- ventilator
		46 -- lighting
	},
	execute = function(value, deviceName, deviceIndex)
		local commandArray = {}

		if (deviceIndex == 47 and value == 'On') then
			-- ventilator was turned on
		end
		
		if (deviceIndex == 46 and value == 'Off') then
			-- lights were turned off
		end

		return commandArray
	end
}
Attachments
v0.9.3.zip
(6.37 KiB) Downloaded 58 times
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Some LUA magic: one script to rule them all..

Post by simonrg »

dannybloe wrote:Also added something extra: the device scripts not only receive the value but also the name and the index of the device triggering the script. This again saves you some code and is handy if you have multiple triggers/devices triggering one script:
New simpler approach is looking interesting :D

A lot of my devices change their idx whenever I need to replace the batteries, so I try to work solely with names and look up idx, made a lot easier with otherdevices_idx table.

All your examples seem to give the idx rather than the name of the device, I assume I could just give the name of the device.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Some LUA magic: one script to rule them all..

Post by dannybloe »

I didn't know you'd get new indexes. But yes, you can use the name as the trigger. As you can see in example.lua.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Toni
Posts: 66
Joined: Monday 20 July 2015 14:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Some LUA magic: one script to rule them all..

Post by Toni »

I added the 'otherdevices_idx[]' a while ago, as some of the commands absolutely require the IDX, and there was no way to get it in Lua.

Like, updating the state of a multiselector device so that it also fires a new event on the selector:

Code: Select all

commandArray['OpenURL'] = 'http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=' .. otherdevices_idx['name of your multiselector'] .. '&svalue=10'
Sicco28
Posts: 7
Joined: Sunday 27 October 2013 13:42
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Some LUA magic: one script to rule them all..

Post by Sicco28 »

@Toni, slightly off-topic, but you can actually use this in LUA:

commandArray[<random order>]={ ['UpdateDevice']='<idx>|0|' .. <integer value> }

e.g. commandArray[1]={ ['UpdateDevice']='171|0|' .. lightLevel }

@Danny, the version 0.9.3 seems to run stable for over a day now :) Good stuff!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Some LUA magic: one script to rule them all..

Post by dannybloe »

Thanks @Sicco28.
I'm still playing with the idea to have some more control over the timer events. Right now it fires every minute but I've noticed that people would like to have some larger intervals like every 5 minutes or maybe every hour. Now I think that can easily be done by this system as well so, and I am thinking out loud, something like this:

Code: Select all

	on = {
		-- pick one
		['timer'] = 'every minute',       -- minutes: xx:00, xx:01, xx:02, ..., xx:59
		['timer'] = 'every other minute', -- minutes: xx:00, xx:02, xx:04, ..., xx:58
		['timer'] = 'every 5 minutes',    -- minutes: xx:00, xx:05, xx:10, ..., xx:55
		['timer'] = 'every 10 minutes',   -- minutes: xx:00, xx:10, xx:20, ..., xx:50
		['timer'] = 'every 15 minutes',   -- minutes: xx:00, xx:15, xx:30, xx:45
		['timer'] = 'every 20 minutes',   -- minutes: xx:00, xx:20, xx:40
		['timer'] = 'every 30 minutes',   -- minutes: xx:00, xx:30
		['timer'] = 'every 60 minutes',   -- minutes: xx:00
		['timer'] = 'every hour',         -- minutes: 00:00, 01:00, ..., 23:00  (24x per 24hrs)
		['timer'] = 'every 2 hours',      -- minutes: 00:00, 02:00, ..., 22:00  (12x per 24hrs)
		['timer'] = 'every 3 hours',      -- minutes: 00:00, 03:00, ..., 21:00  (8x per 24hrs)
		['timer'] = 'every 4 hours',      -- minutes: 00:00, 04:00, ..., 20:00  (6x per 24hrs)
		['timer'] = 'every 6 hours',      -- minutes: 00:00, 06:00, 12:00, 18:00 (4x per 24hrs)
		['timer'] = 'every 12 hours',     -- minutes: 00:00, 12:00, (2x per 24hrs)
		['timer'] = 'every 24 hours',     -- minutes: 00:00, (1x per 24hrs),
		['timer'] = 'at 13:45',
		['timer'] = {'at 13:45', 'at 18:37'},
	}
I think that is not so hard to do and it gives you a lot more flexibility and you don't have to write the horrible Lua code for dealing with times.
Now the only issue we might have here is that the timer events are called every minute by Domoticz at 00 seconds. But of course there is no guarantee that if you have a trigger set 'at 13:45' and for some reason Domoticz skips the event at 13:45, you miss that trigger. But of course that's not a problem I guess.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Some LUA magic: one script to rule them all..

Post by dannybloe »

Ok, so, I just implemented configurable timer triggers. You now have a lot more control over your timed scripts and define timer schedules. This is an excerpt from the readme:

timer options

There are several options for time triggers. First, as you perhaps know, Domoticz event system only calls timer events every minute. So the smallest interval you can use for timer triggers is... you guessed it... one minute. But to prevent you from coding all kinds of timer functions, you can have all kinds of timer triggers that can all be combined for your convenience (all times are in 24hr format!):

Code: Select all

on = {
    'timer'                           -- the simplest form, causes the script to be called every minute
    ['timer'] = 'every minute',       -- same as above: every minute
    ['timer'] = 'every other minute', -- minutes: xx:00, xx:02, xx:04, ..., xx:58
    
    ['timer'] = 'every <xx> minutes', -- starting from xx:00 triggers every xx minutes (0 > xx < 60)

    ['timer'] = 'every hour',         -- 00:00, 01:00, ..., 23:00  (24x per 24hrs)
    ['timer'] = 'every other hour',   -- 00:00, 02:00, ..., 22:00  (12x per 24hrs)
    ['timer'] = 'every <xx> hours',   -- starting from 00:00, triggers every xx hours (0 > xx < 24)
    
    ['timer'] = 'at 13:45',           -- specific time
    ['timer'] = 'at *:45',            -- every 45th minute in the hour
    ['timer'] = 'at 15:*',            -- every minute between 15:00 and 16:00
    
    -- and last but not least, you can create a table with multiples:
    ['timer'] = {'at 13:45', 'at 18:37', 'every 3 minutes'},

},
One important note: if Domoticz for whatever reason skips a beat (skips a timer event) then you may miss the trigger! So you may have to build in some fail-safe checks or some redundancy if you have critical time-based stuff to control.

So, isn't that nice? :lol:

Just an example of a script:

Code: Select all

return {
	active = true,
	on = {
		['timer'] = {'at 8:00', 'at 18:00'}
	},

	execute = function()
		local commandArray = {}
		commandArray['Heater'] = 'On'
		return commandArray
	end
}
So what's next?
How about something like this?:

Code: Select all

	on = {
		['timer'] = {'at 8:00 on {'We', 'Tu, 'Fr', 'Sat'} }, -- day markers
		['timer'] = {'at 8:00-9:00 },  -- time range
		['timer'] = 'sunset',
		['timer'] = {'at ~8:00'},  -- around 8
	},
I know Domoticz has all kinds of schedule support through the GUI but some things may be easier to program or people just don't like GUIs ;-)

But that's for another day...(my vacation is almost over)
Attachments
v0.9.4.zip
(6.93 KiB) Downloaded 46 times
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
NietGiftig
Posts: 121
Joined: Sunday 11 October 2015 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6224
Location: Holland
Contact:

Re: Some LUA magic: one script to rule them all..

Post by NietGiftig »

Thanks!
I don't comprehend it all at the moment, but that is because I have to master the basics of Lua
As always, the best way of learning is trying things out.
RPI-2 + SSD / ESPEasy Sensors & Switches / Sonoff / RFLink / Action Switches / TP-Link switch / Node-Red / Reacticz
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Some LUA magic: one script to rule them all..

Post by dannybloe »

Ah what the heck... I couldn't help it... schedule galore!!:

I added the options to set time triggers for specific days and support for sunrise and sunset triggers:

Code: Select all

on = {
    ['timer'] = 'at 13:45 on mon,tue',     -- at 13:45 only on monday en tuesday (must be english!)
    ['timer'] = 'every hour on sat',       -- you guessed it correctly
    ['timer'] = 'at sunset',               -- uses sunset/sunrise info from Domoticz
    ['timer'] = 'at sunrise',
    ['timer'] = 'at sunset on sat,sun'
},
Time ranges and 'around <some time>' is more difficult and less trivial so I'm not going to add that. :mrgreen:
Attachments
v0.9.6.zip
(7.38 KiB) Downloaded 46 times
Last edited by dannybloe on Friday 26 February 2016 17:59, edited 1 time in total.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
WebStar
Posts: 8
Joined: Tuesday 03 June 2014 12:16
Target OS: Linux
Domoticz version:
Location: Venlo, Netherlands
Contact:

Re: Some LUA magic: one script to rule them all..

Post by WebStar »

When I try to use a function in the active variable, the function is script is always excecuted.

Code: Select all

return {
	active = function() return otherdevices['Thuis'] == 'On' end,                  -- set to true to activate this script,
								     --can also be a function returning either true or false
	on = {
		'Test',  -- see readme for more options and schedules
	},

	execute = function(value, deviceName, deviceIndex)
		local commandArray = {}

		print('Test function')
		print(otherdevices['Thuis'])

		return commandArray
	end
}
This is my function, but also when i write

Code: Select all

active = function() return false end,
The script is always excecuted, (I tried the code from version 0.9.5)
Do you know what i'm doing wrong?
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Some LUA magic: one script to rule them all..

Post by dannybloe »

Yup, I saw it. Fixed it in v0.9.6 (Some weird Lua logic)
Attachments
v0.9.6.zip
(7.38 KiB) Downloaded 58 times
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Some LUA magic: one script to rule them all..

Post by dannybloe »

I have moved the code to github and named it ezVents: https://github.com/dannybloe/ezVents. Feel free to contribute or comment.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
WebStar
Posts: 8
Joined: Tuesday 03 June 2014 12:16
Target OS: Linux
Domoticz version:
Location: Venlo, Netherlands
Contact:

Re: Some LUA magic: one script to rule them all..

Post by WebStar »

Thank you for the quick fix :) I realy like the scripts/codes you created.

Only one other small thing, on github (and your zips) the folder is called 'script' but the configuration is looking for the folder 'scripts'.
Sicco28
Posts: 7
Joined: Sunday 27 October 2013 13:42
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Some LUA magic: one script to rule them all..

Post by Sicco28 »

Maybe another 'feature' request: can we turn off/reduce the verbose logging per script? This setup generates a lot of device scripts (good thing :) ), but also generates a lot of log entries.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Some LUA magic: one script to rule them all..

Post by dannybloe »

Sicco28 wrote:Maybe another 'feature' request: can we turn off/reduce the verbose logging per script? This setup generates a lot of device scripts (good thing :) ), but also generates a lot of log entries.
Ah, ok, good idea. Check out my post about dzVents (new name, lot's of new features).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest