need some help creating a module or helper

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

Moderator: leecollings

Post Reply
Gravityz
Posts: 583
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

need some help creating a module or helper

Post by Gravityz »

i currently have a couple of dummy switches which represend the on/off state of a device.

this way i can easy check if somebody is home or not(and do some automated stuff)

everything is working but i want to make the script more readably by using either a helper function or a module.
I really can not see how to do this (i do not understand dzvents enough i guess)

i simply want a module which i can call to see if somebody is home or not but do not know how to do this


here is the code
basically if every device is in an off state i asume nobody is home
if any of the devices are in an on state i assume somebody is home

so how does the module look and how does the code look to call that module in the standard script

Code: Select all

                    if (domoticz.devices('Samsung-TV').state == 'On' or
                        domoticz.devices('iPhone').state == 'On' or
                        domoticz.devices('iPad').state == 'On' or
                        domoticz.devices('iPod').state == 'On') then
                           domoticz.devices('Status').updateText('Alarm niet ingeschakeld. Er is nog iemand aanwezig')
                           domoticz.log('Alarm niet ingeschakeld. Er is nog iemand aanwezig')
                    elseif (domoticz.devices('iPhone').state == 'Off' and
   		                    domoticz.devices('iPad').state == 'Off' and
   		                    domoticz.devices('iPod').state == 'Off') then
   		                    domoticz.sendCommand('Security Panel', 'Arm Away')
dpcreel
Posts: 44
Joined: Friday 25 November 2016 14:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: need some help creating a module or helper

Post by dpcreel »

I have a bunch of bluetooth devices (cell phones) that get pinged every so often to determine if they are near. If they are near the shell script reports that the bluetoooth device.text is "Home". They are each named differently except I added a " OCC" to the end of each to separate which devices are used for the occupancy purpose. The code below finds all of the devices with that naming and if one of them is "Home" then occupied is true.

All of the OCC devices are dummy text devices, the occupancy device is a dummy switch. This seems to work well as long as bluetooth is on in the phones. I tried it with wifi but it was too intermittent (some phones shut off their wifi to save the battery).

Code: Select all

return {

	active = true,
	
	on = {
		devices = {
			'* OCC' -- all Occupancy text sensors
		}
	},

	execute = function(domoticz,device)
        local occupied = false
        
		domoticz.devices().forEach(function(device)   --check the state of all Occupancy sensors
            if string.find(device.name,' OCC',-4) then
                if device.text == 'Home' then
                     occupied = true
                end
             end
        end)    --return true if at least one sensor reports 'Home'
        
        --update bluetooth occupancy dummy switch if necessary
        if occupied and (not domoticz.devices('Occupancy by Phone Bluetooth').bState) then
            domoticz.log('Switching on occupancy switch',domoticz.LOG_INFO)
            domoticz.devices('Occupancy by Phone Bluetooth').switchOn().silent()
        elseif (not occupied) and domoticz.devices('Occupancy by Phone Bluetooth').bState then
            domoticz.log('Switching off occupancy switch',domoticz.LOG_INFO)
            domoticz.devices('Occupancy by Phone Bluetooth').switchOff().silent()
        end
    end
}
Just a note: the shell script runs on the pi through a cronjob. Sorry, may be too much information, you just wanted the module help.

The module or function is in the part of the code commented by --check the state of all Occupancy sensors.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: need some help creating a module or helper

Post by dannybloe »

In the documentation there is a section about helper functions. That should help.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Gravityz
Posts: 583
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: need some help creating a module or helper

Post by Gravityz »

nice script. some of it might be usefull.

@Dannybloe
I have read the helper function explanation abnd it is clear i can pass things to it and let it do some stuff.
it is however not clear how i can set something(like a variable) and do a check on that


in my case

helper function
return {
helpers = {
Aanwezigheid = function(domoticz)
if .. then variable=1
end
}
}

and then call it from a script

return {
...
execute = function(domoticz, device)
local results = domoticz.helpers.aanwezigheid()
end
}

how can i give Aanwezigheid either a 0 or 1
can i do a
if results =1 then
else

i do not see how i can get a helper to flag something
does it pass a variable back or do i need it to flag a global variable
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: need some help creating a module or helper

Post by dannybloe »

And what is 'variable' in your helper function? Is that a uservariable in Domoticz? Or a dzVents persistent variable?

Or do you want your helper function to return a value:

Code: Select all

return {
	helpers = {
		myHelperFunction(someParam)
			local someValue
			if (someParam == 1 ) then
				-- do something
				someValue = 10
			else
				someValue = 20
			end
			
			return someValue
		end
	}
}

Code: Select all

return {
	on = ..,
	execute = function(domotiz, item)
		local helperResult = domoticz.helpers.myHelperFunction(1)
		print(helperResult) -- will print 10
	end
}
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Gravityz
Posts: 583
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: need some help creating a module or helper

Post by Gravityz »

i want it to return a value an/or string which i can use in the main script

in your example i pass a someparam to the helper (this is clear)
you then define a local variable SomeValue and change it accoording to some tests (this is clear)

now how do i get that someValue in my main script, looks lioke someValue is passed to helperesult
if this is the case then i know what to do

i see the result is passed back using return SomeValue (Maybe update the wiki with this, because this is currently not clearly explained)

can you use more returns eg more values
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest