Converting a (very) complex motion script help

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

Moderator: leecollings

Post Reply
DanM
Posts: 79
Joined: Thursday 23 October 2014 22:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Converting a (very) complex motion script help

Post by DanM »

Hi All,

Im thinking of making the step over to DzVents and would appreciate some help with the conversion. I'll also use this thread to document what I end up with in case anyone else is crazy and wants to end up with motion scripts as complicated as mine.

Im trying to convert over my existing LUA that governs motion sensor behaviour in my house. Needless to say its a little more complex than most. Broadly speaking this is how it works:

Each room has at least the following devices / variables:
  • Room Name Motion Sensor
    Room Name Lux Sensor
    Room Name Countdown Variable (integer)
    Room Name Active Scene (variable)
    Room Name Scene Selector
    5 Scenes named: Concentrate, Read, Relax, Nightlight and Red (lights go red as part of the alarm in our house).
    A hidden device named $Room Name Active to track whether scenes are on / off as Domoticz does not offer this functionality.
    A group that is used to turn off the devices in a room (as scenes can only be turned on), this is never turned on, only off.
When a motion sensor is triggered the basic logic is as follows:
1. Identify motion
2. Check the light level in the room is either below 5 lux or the outside lux sensor is below 200.
3. Look up the currently active scene for the room from the variable
4. Determine if the scene is already active - only turn on if it isnt
5. Set the countdown variable to 30 minutes
6. Loop through an array of other rooms, see which have lights on and reduce their countdowns to 4 minutes.

A second script counts down every minute - when it reaches zero it checks when motion was last detected and either turns off the light, or turns it back on (by retriggering the countdown reset).

***
As you can see its a little more complex than most. I can see my path to making it work, but I have a few questions:
1. Can dzVents call LUA functions in the same way as the exiting LUA. The code above comprises 10 or 11 different fuctions and Id like to be able to keep some of the existing structure.

Perhaps an example would help - this is the function that reduces the countdowns in none active rooms. Create Notification is a custom notification script that can be ignored for now.

Code: Select all

 function set_countdown(room_name,countdown)
 	   
	--set the countdown variable name
   countdown_name = room_name .. " Countdown"
   
	--start with a list of rooms/countdown variables
	room_countdowns = {'Living Room', 'Conservatory','Bedroom', 'Upstairs Hall', 'Kitchen'}
	
	for i, room in ipairs(room_countdowns) do
	
	   	--get the active device to determine if the group is on
	light_group = '$' .. room .. " Active"

	--set the countdown name
	room_countdown = room .. " Countdown"
   
	  if room_countdown == countdown_name then
	  --This is the active room, we dont want to do anything
	  else
	  --This is not the active room, we want to reduce the countdown
		  if  uservariables[room_countdown] > countdown then
		  if otherdevices[light_group] == 'On' then
		  create_notification ('Reducing ' .. room_countdown  .. ' to ' .. countdown , "set_countdown", room_name)
			commandArray[x]={['Variable:'.. room_countdown]= tostring(countdown)}
			x=x+1
			
		  end
		  end
	  end 
	end
end
Any and all help is much appreciated.

Many thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest