Page 1 of 1

Help wanted with LUA/dzVents

Posted: Monday 27 June 2016 12:57
by elmortero
Hi everyone,

I want to create a script set all smoke detectors I have to 'Panic' using Lua/dzvents.

More than help with actual writing I would like some ideas/feedback on how the logic should be handled.
I want to use a wild-card for the triggering 'smd*' but I surely don't want to create a loop when activating the 'Panic' modes which will then trigger the script again, since at that moment *all* detectors will have a changed status to 'panic' and so on..

I have no scripting/programming experience and only started with Lua a little while ago because Blockly does not serve my needs.

Thanks in advance,

Olivier

PS: I know I can write separate script for each detector and the trigger all but that one to avoid the loop, but if possible I would like to avoid having a multitude of scripts.

Re: Help wanted with LUA/dzVents

Posted: Monday 27 June 2016 12:59
by dannybloe
What would cause a loop then? What is the exact chain of events? One detector detects smoke and then all other detectors should start to panic?

Re: Help wanted with LUA/dzVents

Posted: Monday 27 June 2016 14:20
by elmortero
dannybloe wrote: One detector detects smoke and then all other detectors should start to panic?
That is indeed what I want. If smoke is detected, warning everybody in the house by activating the alarm of all detectors.
But wouldn't this trigger the script again, and again...?

Re: Help wanted with LUA/dzVents

Posted: Monday 27 June 2016 14:45
by dannybloe
I'd do something like this. By checking if the state isn't already Panic only then you set it to Panic (this prevents the looping).

Code: Select all

local detectors = {
	['smokeDetector1'] = true,
	['smokeDetector2'] = true,
	['smokeDetector3'] = true
}

return {
	active = true, 
	on = {
		'smokeDetector*'
	},
	execute = function(domoticz, smokeDetector)
		
		if (smokeDetector.status == 'Panic') then
			
			-- filter all devices based on the detectors list above
			-- and only those that aren't already in Panic mode
			domoticz.devices.filter(function(device)
				
				-- return true for non-panic devices and with the proper name
				return detectors[device.name] == true and device.state ~= 'Panic' 
				
			end).forEach(function(detector)
				-- set to panic
				detector.setState('Panic')
				
			end)
		end
		
	end
}

I don't have smoke detectors myself so it could be that this doesn't work ;-)
Of course there are many ways that lead to Rome...

Or if you want to make it less generic:

Code: Select all


return {
	active = true, 
	on = {
		'smokeDetector1',
		'smokeDetector2,'
		'smokeDetector3'
	},
	execute = function(domoticz, smokeDetector)
		
		if (smokeDetector.status == 'Panic') then
			
			if (domoticz.devices['smokeDetector1'].state ~= 'Panic') then 
				domoticz.devices['smokeDetector1'].setState('Panic')
			end
			
			if (domoticz.devices['smokeDetector2'].state ~= 'Panic') then 
				domoticz.devices['smokeDetector2'].setState('Panic')
			end
			
			if (domoticz.devices['smokeDetector3'].state ~= 'Panic') then 
				domoticz.devices['smokeDetector3'].setState('Panic')
			end

		end
		
	end
}


Re: Help wanted with LUA/dzVents

Posted: Monday 27 June 2016 14:56
by elmortero
Ok Danny, this is a big help.
Looks good, as soon as I am alone in the house I can do a test (my partner would kick me out of the house if I test this while she is home (too loud).

Actually my biggest mistake was forgetting that you can put several devices in the on = { } part and that I would have to have a script for each detector.
I knew this, just did not think about it.

BTW: I really like what you have created with dzVents, makes scripting life so much easier.

Re: Help wanted with LUA/dzVents

Posted: Monday 27 June 2016 15:01
by dannybloe
elmortero wrote:Ok Danny, this is a big help.
Looks good, as soon as I am alone in the house I can do a test (my partner would kick me out of the house if I test this while she is home (too loud)
:)

I'm not exactly sure if setState('Panic') is the way to go though. Let me know if it works.

Re: Help wanted with LUA/dzVents

Posted: Monday 27 June 2016 15:42
by elmortero
It makes sense that way, but if it is not, I'll use the json approach, or create scene and activate that one.

Re: Help wanted with LUA/dzVents

Posted: Monday 27 June 2016 15:47
by dannybloe
That should not be needed. As far as I know you can send all commands using dzVents without json. If not then I'll add it.

Re: Help wanted with LUA/dzVents

Posted: Friday 01 July 2016 16:54
by elmortero
Hi Danny,

Tried it today, but it failed.
Seems to be ineed something with the .setState('Panic') because when I replace that with a virtual switch it works.

In this topic http://www.domoticz.com/forum/viewtopic ... son#p44652
Eduard said it works with /json.htm?type=command&param=switchlight&idx=3&switchcmd=On
So I guess instead of setState('Panic') I should use switchOn()

Hope to find some alone time this weekend to test but I feel confident that this will be it.

Update: just tried it, not working, no error messages.

Re: Help wanted with LUA/dzVents

Posted: Sunday 03 July 2016 12:59
by dannybloe
Strange.I couldn't find much information regarding this kind of 'switch' It should be possible.

Re: Help wanted with LUA/dzVents

Posted: Tuesday 05 July 2016 12:32
by elmortero
It is odd indeed.
As soon as I can I will do some more tests.
For now, in order to have this working, I will have to use Blockly (thought I'd seen the last of that :-) )

Re: Help wanted with LUA/dzVents

Posted: Tuesday 05 July 2016 12:40
by dannybloe
Would be interesting to know what Blockly sends to Domoticz (internally) as the value.

Re: Help wanted with LUA/dzVents

Posted: Friday 05 August 2016 9:24
by elmortero
Finally been able to test.
The switchOn() does the trick, as for the check to the current status, 'Panic' is not how check.
This seems to work:

Code: Select all

  return {
   active = true, 
   on = {
      'smd-bedroom',
      'smd-hall',
      'smd-guest',
	  'smd-salon',
	  'smd-kitchen'
	  },
	  

   execute = function(domoticz)
		  local bedroom = domoticz.devices['smd-bedroom']
		  local hall = domoticz.devices['smd-hall']
		  local guest = domoticz.devices['smd-guest']
		  local salon = domoticz.devices['smd-salon']
		  local kitchen = domoticz.devices['smd-kitchen']
		  
      if (bedroom.changed and bedroom.status == 'On')
	        or (hall.changed and hall.status == 'On')
			      or (guest.changed and guest.status == 'On')
				        or (salon.changed and salon.status == 'On')
						      or (kitchen.changed and kitchen.status == 'On')

	  then
         
				 if kitchen.state == 'Off' then 
					kitchen.switchOn()
				 end
				 
				 if (hall.state ~= 'On') then 
					hall.switchOn()
				 end
				 
				 if (guest.state ~= 'On') then 
					guest.switchOn()
				 end
				 
				 if (salon.state ~= 'On') then 
					salon.switchOn()
				 end
				
				if (bedroom.state ~= 'On') then 
					bedroom.switchOn()
				end
		 
		end
		     --- end 
   end
}
Thanks a lot for the support

Re: Help wanted with LUA/dzVents

Posted: Friday 05 August 2016 9:35
by dannybloe
Great. Nice to see how the code is so much more readable than without dzVents. That was the main purpose of making it.

Re: Help wanted with LUA/dzVents

Posted: Friday 05 August 2016 9:43
by elmortero
That is why I like it.
I have no coding experience and I learn from examples (that is why I am sometimes lost in manuals..and ask for help).
So when I had to go from Blockly to Lua I was hesitating. But then just after that I you came with dzvents and I liked the posibilities (but had to overcome my resitance to start learning that)
Very glad you put the effort into making it and really grateful.
With the smokedetector thing solved I have everything converted to dzVents scripts.