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

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Locked
commodore white
Posts: 63
Joined: Sunday 14 July 2013 11:19
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Ipswich, UK
Contact:

Timed events get lost

Post by commodore white »

Hi. I've been trying to debug my phones presence detection script and been confused by its behaviour on several occasions. Today I noticed that a per-minute event was missed completely. Here is an extract from the log:

Code: Select all

2016-03-15 10:31:00.695 LUA: =====================================================
2016-03-15 10:31:00.696 LUA: >>> Handler: devicetest
2016-03-15 10:31:00.696 LUA: .....................................................
2016-03-15 10:31:00.696 LUA: dzVents Phones:Peter's phone found 0h 1m ago
2016-03-15 10:31:00.697 LUA: dzVents Phones:Rita's phone found 6h 18m ago
2016-03-15 10:31:00.697 LUA: .....................................................
2016-03-15 10:31:00.697 LUA: <<< Done
2016-03-15 10:31:00.697 LUA: -----------------------------------------------------
2016-03-15 10:31:12.978 (RFXCOM) Energy (Electric Meter)
2016-03-15 10:31:47.659 (RFXCOM) Temp (envKitchen)
2016-03-15 10:31:48.110 (RFXCOM) Humidity (envKitchen)
2016-03-15 10:32:00.276 LUA: Handle timer events
2016-03-15 10:32:12.980 (RFXCOM) Energy (Electric Meter)
2016-03-15 10:32:46.732 (RFXCOM) Temp (envKitchen)
2016-03-15 10:32:47.297 (RFXCOM) Humidity (envKitchen)
2016-03-15 10:33:00.061 LUA: Handle timer events
2016-03-15 10:33:00.216 LUA: =====================================================
2016-03-15 10:33:00.216 LUA: >>> Handler: devicetest
2016-03-15 10:33:00.216 LUA: .....................................................
2016-03-15 10:33:00.217 LUA: dzVents Phones:Peter's phone found 0h 3m ago
2016-03-15 10:33:00.217 LUA: dzVents Phones:Rita's phone found 6h 20m ago
2016-03-15 10:33:00.217 LUA: .....................................................
2016-03-15 10:33:00.217 LUA: <<< Done
2016-03-15 10:33:00.217 LUA: -----------------------------------------------------
2016-03-15 10:33:12.979 (RFXCOM) Energy (Electric Meter)
The timed event for 10:32 was completely dropped. Not yet sure if this is a dzVents issue or a domoticz one. How might I track down the problem?

Peter
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post by dannybloe »

thorbj wrote:Thanks!
Does the "domoticz.variables['USERVAR'].set(no_motion_time)" string have to be inside an if statement, or can it be after end - before a new if?

I can't see that the uservar is changing.
I put a

Code: Select all

if (presenceDevice==nil) then
			-- timer was triggered, get my switch
			presenceDevice = domoticz.devices[MOTION,CELL_ONE,CELL_TWO]
		end
in my script. Maybe that broke it?
First of all, this isn't valid Lua:

Code: Select all

	presenceDevice = domoticz.devices[MOTION,CELL_ONE,CELL_TWO]
you can put only one thing between the [ ].

And the set command can be done anywhere you like. As soon as you do a set() it will push the command on the command list that is sent back to Domoticz.

(make sure you use the latest version from github)
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
commodore white
Posts: 63
Joined: Sunday 14 July 2013 11:19
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Ipswich, UK
Contact:

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

Post by commodore white »

Hi. Attached is my dzVents mobile phone presence detector. Its taken a while to convert from my raw LUA prototype but I've tried to use some of the more recent dzVents features to push myself up the learning curve.

If you can offer any improvements I'd like to hear. Enjoy.

Peter

Code: Select all

--[[ lua/scripts/phones.lua by commodore white

This dzVents script implements an android phone presence detector using the Domoticz System Alive
Checker (Ping) feature.

For each phone being monitored, you need two virtual devices, one that you bind to the System
Alive Checker, and another that keeps you informed of the phones' proximity. I needed the second
because our phones kept losing and regaining connectivity as we moved around the house or we went outside
for a few minutes perhaps to the washing line.

The script is in two parts, the first responds to sucessfull or failed pings while the second implements
a 'grace period' during which time a phone can be out of earshot for a short while without raising the alarm.

The script also maintains another virtual device called "AnyPhone" which is On whenever any phone is
in reach.

My virtual devices are called:

Peter and pingPeter
Rita  and pingRita, and
AnyPhone

... that is, 2n+1 virtual devices in total.

--]]

return {
   on = { "ping*", "timer" },
   active = true,
   execute = function(domoticz, ping)

   local phone
   local pingPhone
   local GRACE = 5 -- five minutes
   local lost_or_found
   local AnyPhone
   local _AnyPhone
   local _phone
   local _pingPhone

   printf = function(s,...) return print("dzVents Phones: " .. s:format(...)) end
   
   _AnyPhone = domoticz.devices["AnyPhone"]

   if ping ~= nil then
     phone = ping.name:sub(5,99)
     _phone = domoticz.devices[phone]

     if    ping.state == "On" and _phone.state == "Off"
     then  _phone.switchOn()
           if _AnyPhone.state == "Off" then _AnyPhone.switchOn() end
     end
   else
     AnyPhone = "Off" -- assume no phones found
     domoticz.devices.forEach(function(_pingPhone)
  	   if (_pingPhone.name:sub(1,4) == "ping") then
          phone = _pingPhone.name:sub(5,99)               _phone = domoticz.devices[phone]
          m_phone     = _phone.lastUpdate.minutesAgo      m_pingPhone = _pingPhone.lastUpdate.minutesAgo
         
          if _phone.state == "On" then           -- recently seen
            if    _pingPhone.state == "Off"      -- but currently not seen
            and   m_pingPhone > GRACE            -- and has been so for several minutes
            then  _phone.switchOff()
                  printf("%s's phone lost %um ago", phone, m_pingPhone%60)
            else
                  printf("%s's phone found %uh %um ago", phone, m_phone/60, m_phone%60)
                  AnyPhone = "On"
            end
          else
             printf("%s's phone lost %uh %um ago", phone, (m_phone+GRACE)/60, (m_phone+GRACE)%60)
          end -- if phone.state        
  		 end
  	 end) -- for each
    
    if AnyPhone ~= _AnyPhone.state then _AnyPhone.setState(AnyPhone) end

    end -- if ping

  end -- function
  
} -- return
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Timed events get lost

Post by dannybloe »

commodore white wrote: The timed event for 10:32 was completely dropped. Not yet sure if this is a dzVents issue or a domoticz one. How might I track down the problem?
Peter
That's indeed weird. As you can see the script_time_main was triggered at 10:32 but nothing was printed afterwards. Are you running 0.9.12? That was has a fix so errors are printed again. That's the only thing I can think of. What you can do is either set the loglevel to 3 in the settings file (debug level) but that might give you too much information. Other option is put some print statements in the script_time.main.lua file and see which one isn't executed (then it fails somewhere before).
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: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by dannybloe »

commodore white wrote:Hi. Attached is my dzVents mobile phone presence detector. Its taken a while to convert from my raw LUA prototype but I've tried to use some of the more recent dzVents features to push myself up the learning curve.

If you can offer any improvements I'd like to hear. Enjoy.

Peter
I don't see anything special Maybe I'd name the variables a bit more descriptive but that's about it.
I just learned that there's a System Alive Checker. Thanks :) I can use that for a couple of my esp8266 sensors.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
commodore white
Posts: 63
Joined: Sunday 14 July 2013 11:19
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Ipswich, UK
Contact:

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

Post by commodore white »

Hi. Definitely running the latest version. However, for some reason, errors raised because of coding errors are not reported which make the software development process a bit protracted. Takes me back to the old days when coding was done on punched cards and you were lucky if you could pass your code through a compiler once a day. It became really important to cosy up to the computer operators, who, if memory serves, never had to buy a pint in the pub!

At least punched cards were easier to edit compared to punched paper tape, it is until you dropped the whole deck of cards on the floor or worse still, in a puddle.

Sigh. Them were the days.
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

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

Post by thorbj »

dannybloe wrote:First of all, this isn't valid Lua:

Code: Select all

	presenceDevice = domoticz.devices[MOTION,CELL_ONE,CELL_TWO]
you can put only one thing between the [ ].

And the set command can be done anywhere you like. As soon as you do a set() it will push the command on the command list that is sent back to Domoticz.

(make sure you use the latest version from github)
Again, big thanks!
Your latest version with errorlogging is great!
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
dakipro
Posts: 49
Joined: Thursday 17 December 2015 22:24
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Norway
Contact:

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

Post by dakipro »

while on the subject of "punched paper tape" programming of Lua scripts... :)
How does one debug and tests the scripts and code?
I f.eks. bind script to one of the motion sensors and then I keep waving hands like a windmill in the living room each time I update the code and watch the log in domotics. Or I have to wait for a minute trigger to come by, and then at the end rebind the script to the actual device I want to use and test again.

Do you guys use the dummy/virtual switches or some more convenient method of testing? I am not still discovering possibilities of domoticz
Raspberry Pi 2 B - MySensors (dimmers, switches, motion, temperature, lux, humidity sensors)
zWave (220v switches)
various 433 via RFLink
commodore white
Posts: 63
Joined: Sunday 14 July 2013 11:19
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Ipswich, UK
Contact:

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

Post by commodore white »

I keep a PIR sensor close by and use that to trigger scripts as 'tis quicker than going to a domoticz switch screen and pressing a button. An alternative might be to get a wireless front door bell push button and keep it handy (only just thought of that so I'll redeploy my PIR and stop looking like I'm having some sort of fit).

You could also change a script trigger to include "*", that is 'any device'. Your script will be triggered almost continuously esp if your system includes hardware sensors, CPU, memory, hard disk and the like.

My dzVents installation still doesn't display errors raised by coding errors, despite others experiences, so I'm resorting to more intensive code review and random distribution of print statements.

I'm hoping dzVents gains traction. My scripts seem much more wordy. Perhaps thats not a bad thing.

Peter (looking for his ASR33 and hoping to fix the "Glue Out" indicator on its papertape punch backspace mechanism).
commodore white
Posts: 63
Joined: Sunday 14 July 2013 11:19
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Ipswich, UK
Contact:

Suggestion: per-script logging level

Post by commodore white »

Hows about an addition parameter at the beginning of scripts to define its logging level, c.f:

Code: Select all

return {
	active = true,
	on = { 'timer' },
   log = domoticz.LOG_INFO,

	execute = function(domoticz)
	
	whatever
This would allow developers to have comprehensive logging for the script they're working on, and eliminating the clutter from those they're not.

Just a thought.

Peter
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

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

Post by thorbj »

When using this time table:

Code: Select all

['timer'] = {'at 23:*', 'at 00:*', 'at 01:*', 'at 02:*', 'at 03:*', 'at 04:*'},
I get the following error:

Code: Select all

2016-03-17 09:32:59.443 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_main.lua: /home/pi/domoticz/scripts/lua/script_device_main.lua:30: bad argument #1 to 'find' (string expected, got table)
In the manual it says that I can use time tables:

Code: Select all

    -- and last but not least, you can create a table with multiples:
    ['timer'] = {'at 13:45', 'at 18:37', 'every 3 minutes'},
What am I missing here?
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
mcmacpl
Posts: 11
Joined: Friday 15 January 2016 21:05
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by mcmacpl »

Exactly the same issue:

Script:

Code: Select all

return {
        active = true,
        on = {
                ['timer'] = { 'at 14:25', 'at 14:30' }
             },
        execute =
                function(domoticz)
                        if (domoticz.devices[35].bState) then
                                domoticz.devices[35].switchOff()
                        end

                end
}
Result:

Code: Select all

2016-03-17 14:31:00.025 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_main.lua: /home/pi/domoticz/scripts/lua/script_device_main.lua:30: bad argument #1 to 'find' (string expected, got table)
2016-03-17 14:31:00.533 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_main.lua: /home/pi/domoticz/scripts/lua/event_helpers.lua:300: attempt to call global 'checkTimeDefs' (a nil value)
With only
on = { ['timer'] = 'at 14:25' }
it was working well
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

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

Post by thorbj »

I tried the following. No errors in the log yet:

Code: Select all

	on = {
		['timer'] = 'at 23:*',
		['timer'] = 'at 00:*',
		['timer'] = 'at 01:*',
		['timer'] = 'at 02:*',
		['timer'] = 'at 03:*',
		['timer'] = 'at 04:*',
	},
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
NietGiftig
Posts: 121
Joined: Sunday 11 October 2015 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6224
Location: Holland
Contact:

Example script entrancelight (Portieklicht)

Post by NietGiftig »

A small contribution for the examples directory, entrancelight (for me Portieklicht).
Tried to send it with Github, but that a new challenge for me, so i do it here

Code: Select all

-- prerequisite: LUX reader, Entrancelight (SONOFF), Virtual switch for manual ON
--LUX reader (homemade ESP8266 with ESPEasy ) placed facing the outside world
--Entrancelight (switched with SONOFF switch with ESPEasy firmware)

--settings
local waitingMinutes = 4				-- Anti ‎Hysteresis (fast switch flip flop)
												-- No need for fast reaction
local minimumAmbientLight = 350		--LUX Adjust to your own preference
local luxdevice_idx = 55				--Adjust to your own settings	
local entrancelight_idx = 70			--Adjust to your own settings
local manualON_idx = 79				--Adjust to your own settings

return {
   active = true,               	-- set to true to activate this script
   on = {
      ['timer'] =  'every ' .. waitingMinutes .. ' minutes',
		manualON_idx
   },
	 
   execute = function(domoticz)
      --initialize variables
			local luxValue = domoticz.devices[luxdevice_idx].lux
			local entrancelight = domoticz.devices[entrancelight_idx]
			local manualON = domoticz.devices[manualON_idx]
			print('LUX outside = ' .. luxValue .. ' - Minimum = ' .. minimumAmbientLight ) 

			if (entrancelight.state == 'Off') then
			  if (luxValue < minimumAmbientLight) or (manualON.state == 'On') then
				-- LUX below minimum or Manual ON
         			print('Entrancelight ON' .. ' Manual ' .. manualON.state)
				  
					entrancelight.switchOn()
					
				end 
			else 
				if (manualON.state == 'Off') then
					if (luxValue > minimumAmbientLight) then
						-- LUX above minimum			
						print('Entrancelight OFF')
					
						entrancelight.switchOff()
				
					end
				end
			end
		end
}
RPI-2 + SSD / ESPEasy Sensors & Switches / Sonoff / RFLink / Action Switches / TP-Link switch / Node-Red / Reacticz
mcmacpl
Posts: 11
Joined: Friday 15 January 2016 21:05
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by mcmacpl »

thorbj wrote:I tried the following. No errors in the log yet:

Code: Select all

	on = {
		['timer'] = 'at 23:*',
		['timer'] = 'at 00:*',
		['timer'] = 'at 01:*',
		['timer'] = 'at 02:*',
		['timer'] = 'at 03:*',
		['timer'] = 'at 04:*',
	},
Yes, but for me it will be switched only at 04:*.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post by dannybloe »

Crap, I didn't get any notifications again for this forum thread.. Quite annoying. Sorry that I missed these message. Will read up.
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: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by dannybloe »

thorbj wrote:I tried the following. No errors in the log yet:

Code: Select all

	on = {
		['timer'] = 'at 23:*',
		['timer'] = 'at 00:*',
		['timer'] = 'at 01:*',
		['timer'] = 'at 02:*',
		['timer'] = 'at 03:*',
		['timer'] = 'at 04:*',
	},
I just made a small release that fixes this bug. Sorry for the inconvenience.

Currently I am writing loads of unit test in the master branch to make it more stable (than it already is ;-)). Fortunately, while writing these tests, this problem was found by one of my tests :).

Anyhow... you should use the other syntax here:

Code: Select all

	on = {
		['timer'] = { .., .., ..}
	}
Let me know if this works now. Again sorry for this.
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: Example script entrancelight (Portieklicht)

Post by dannybloe »

NietGiftig wrote:A small contribution for the examples directory, entrancelight (for me Portieklicht).
Tried to send it with Github, but that a new challenge for me, so i do it here

Code: Select all

      ['timer'] =  'every ' .. waitingMinutes .. ' minutes',
That's interesting indeed.. Didn't come up with this kind of dynamic trigger myself.. might add some tests that makes sure this keeps working.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dakipro
Posts: 49
Joined: Thursday 17 December 2015 22:24
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Norway
Contact:

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

Post by dakipro »

Hi, is there a toggle function?
Does it exists in domoticz, I think it can be triggered using the json url, but can it be "toggled" using the lua?
Raspberry Pi 2 B - MySensors (dimmers, switches, motion, temperature, lux, humidity sensors)
zWave (220v switches)
various 433 via RFLink
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post by dannybloe »

dakipro wrote:Hi, is there a toggle function?
Does it exists in domoticz, I think it can be triggered using the json url, but can it be "toggled" using the lua?
I don't know other than a couple of if statements checking for the current state. I can easily make a toggle function that encapsulates this. If that's what you mean.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Locked

Who is online

Users browsing this forum: Google [Bot] and 0 guests