Scripting in LUA for lights

Moderator: leecollings

Post Reply
jesses
Posts: 7
Joined: Tuesday 23 January 2018 11:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Scripting in LUA for lights

Post by jesses »

Hi,

I just started using Domoticz. The version and hardware I use:
Raspberry PI 2 B
Jessie lite Raspbarian
Domoticz: Version: 3.8153 / Build Hash: 494fff7
Fibaro Motion Sensor V2
AEON labs zwave USB gen5

I have almost no experience with LUA but I used the available examples I found online and the script is working for me.
There is just some things I would like to add.
The script I use need to turn on the light when movement is detected on specified time. For the specified time i use (2 diffirent) virtual switches, named "schema1" and "schema2"

In the bedrooms (slaapkamer) and hallways (gang) I only need to switch a single light based on a single motion detector. In the living room i have a group that I need to switch. I want that the script checks if the light is already on before it takes any action. I would also prefer that the script that turns the lights of checks if the lights are on before is switches off.
My first question: is it possible to check if a group is switched on (or mixed). And how can I do this?
My second question: is it a problem the script I use to switch off the light when no movement detected for xx minutes does not check if the lights are on?

The script I use to turn the lights (group) on (livingroom with more than 1 light):

Code: Select all

-- script_device_motion.lua
local motion_switch = 'WoonkamerPIR'
local status_switch = 'schema2'

commandArray = {}

if devicechanged[motion_switch] then
	if otherdevices[motion_switch] == 'On' and otherdevices[status_switch] == 'On' then
 	commandArray['Group:Woonkamer']='On'
 	end
end
return commandArray
And to switch the lights off in the living room:

Code: Select all

-- script_time_nomotion.lua

local motion_switch = 'WoonkamerPIR'
local nomotion_uservar = 'nomotionCounter'
local status_switch = 'Group:Woonkamer'

commandArray = {}

no_motion_minutes = tonumber(uservariables[nomotion_uservar])

if (otherdevices[motion_switch] == 'Off') then
	no_motion_minutes = no_motion_minutes + 1
	--print('<font color="red">No motion has been detected for: ' ..no_motion_minutes.. ' minutes</font>')
else 
	no_motion_minutes = 0	
end 

commandArray['Variable:' .. nomotion_uservar] = tostring(no_motion_minutes)

if no_motion_minutes > 30 then --change the 30 to the amount of minutes you prefer
 	commandArray['Group:Woonkamer']='Off'
end

return commandArray
Script I use in the hallway (this is the same for the other rooms with only 1 light) to turn on:

Code: Select all

-- gangb_motion_device.lua

local motion_switch = 'gangbPIR'
local status_switch = 'hue_gang_beneden'
local time_switch = 'schema1'

commandArray = {}

if devicechanged[motion_switch] then
	if otherdevices[motion_switch] == 'On' and otherdevices[status_switch] == 'Off' and otherdevices[time_switch] == 'On' then
 	commandArray[status_switch]='Set Level 66'
 	end
end
return commandArray
And to turn off:

Code: Select all

-- gangb_time_nomotion.lua

local motion_switch = 'gangbPIR'
local nomotion_uservar = 'gangbnomotion'
local status_switch = 'hue_gang_beneden'

commandArray = {}

no_motion_minutes = tonumber(uservariables[nomotion_uservar])

if (otherdevices[motion_switch] == 'Off') then
	no_motion_minutes = no_motion_minutes + 1
	--print('<font color="red">No motion has been detected for: ' ..no_motion_minutes.. ' minutes</font>')
else 
	no_motion_minutes = 0	
end 

commandArray['Variable:' .. nomotion_uservar] = tostring(no_motion_minutes)

if no_motion_minutes > 3 then --change the 30 to the amount of minutes you prefer
 	commandArray[status_switch]='Off'
end

return commandArray
I hope someone can help and advise me how to optimize this. Thanks in advance.

Regards,
Jesse
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Scripting in LUA for lights

Post by waaren »

@Jesses,

@first question: to check the state of a group in LUA check viewtopic.php?t=9859
@second question: this is not a problem; domoticz will just send the off signal.

I might we worthwhile for you to have a look at dzVents https://www.domoticz.com/wiki/DzVents:_ ... _scripting to simplify writing Lua scripts for your domoticz system.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jesses
Posts: 7
Joined: Tuesday 23 January 2018 11:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Scripting in LUA for lights

Post by jesses »

waaren wrote: Tuesday 23 January 2018 15:57 @Jesses,

@first question: to check the state of a group in LUA check viewtopic.php?t=9859
@second question: this is not a problem; domoticz will just send the off signal.

I might we worthwhile for you to have a look at dzVents https://www.domoticz.com/wiki/DzVents:_ ... _scripting to simplify writing Lua scripts for your domoticz system.
Thank you, really appreciated! DzVents already implemented for some other scripts but I still have to learn a lot.
I will first look into the group state:)

Regards,
Jesse
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest