Script examples (Lua)

asjmcguire
Posts: 139
Joined: Saturday 13 July 2013 2:45
Target OS: Linux
Domoticz version: 3.5877
Location: Scotland
Contact:

Script examples (Lua)

Post by asjmcguire » Monday 15 July 2013 2:30

I'm not seeing anywhere on the forum where people can post the LUA scripts they are using - so I thought I would start if that is OK with everyone else.
Please move this topic somewhere else if you feel it is in the wrong place.

OK so my situation is that I have a remote switch that has 2 code wheels - House: A-P and Unit 1-16. It supports the old style of Byron/Homeeasy sockets that have the code wheel but not the new style learning sockets. I have the remote switch in a public area in the cafe but I do not want it to switch on the light during the day - eg prevent the public switching the light on.

First I created a virtual device called "v_Daytime" which Domoticz controls using a timer. ON 2 hours AFTER sunrise and OFF 2 hours BEFORE sunset (this copes with dark/overcast mornings and dark/overcast evenings)

Next the remote switch is called "CafeSwitch" and has a 15 minute timer on it.
The light is called "CafeLight" and is a standard on/off device with no timer.

My script is called "script_device_CafeLightSwitch.lua"

Code: Select all

commandArray = {}
if (devicechanged['CafeSwitch'] == 'On' and otherdevices['CafeLight'] == 'Off' and otherdevices['v_Daytime'] == 'Off') then
	commandArray['CafeLight']='On'
  print('CafeLight was switched ON')
end

if (devicechanged['CafeSwitch'] == 'Off') then
  commandArray['CafeLight']='Off'
  print('CafeLight was switched OFF')
end

if (devicechanged['CafeSwitch'] == 'On' and otherdevices['v_Daytime'] == 'On') then
  commandArray['CafeSwitch']='Off'
  print('CafeLight was switched ON but it\'s daytime!')
end

return commandArray
If the switch is turned on when it's daytime (v_Daytime is ON) then nothing happens.
if the switch is turned off - day or night, the light is turned off
if the switch is turned on when it's night time - the light is turned on (and domoticz will turn it off after 15 minutes because of the timer on the switch)

Hope this helps someone get to grips with the LUA system.
AEOTEC ZStick, 11 ZWave Nodes, RFXCOMM, 50ish Byron Sockets.. HE851 (PIR), 2x HE852 (DoorContact)
WS2300, CM180, CC128, 2xTHGR122NX, 2xPiZeroW w/DS18B20, 8Ch 1W Relay Board.
8 Panasonic IP Cams, 1 16ch CCTV DVR + 15 CCTV Cams

User avatar
CopyCatz
Developer
Posts: 751
Joined: Thursday 11 July 2013 17:28
Target OS: -
Domoticz version:
Location: Outer Heaven
Contact:

Re: LUA Scripts

Post by CopyCatz » Monday 15 July 2013 8:22

This is what we need! Thanks for the example, I'll make this the official thread and add the script to the wiki. Now all we need is your Beer-Script.

asjmcguire
Posts: 139
Joined: Saturday 13 July 2013 2:45
Target OS: Linux
Domoticz version: 3.5877
Location: Scotland
Contact:

Re: LUA Scripts

Post by asjmcguire » Tuesday 16 July 2013 2:15

It's only taken most of the day to figure out the semantics of the LUA system - and I have finally got half way towards the requested FAN example listed in the TBD section of the demo script.
The main issue is the example listed on the wiki is incorrect - if you attempt to compare the temperature from a device, LUA complains that the temperature is a string. You also need to verify if the script even received a temperature - in much the same way you need to check with devices if the device you are interested in was actually triggered. OK - here we go:

Code: Select all

commandArray = {}
if (devicechanged['OldExt_Temperature']) then
print('OldExt is ' .. devicechanged['OldExt_Temperature'])
temp = tonumber(devicechanged['OldExt_Temperature'])

 if (temp >= 15.0 and otherdevices['TestBedroom'] == 'Off') then
  commandArray['TestBedroom']='On'
  print('Outside temperature is hot, switching fan on')
 end

 if (temp <= 15.0 and otherdevices['TestBedroom'] == 'On') then
  commandArray['TestBedroom']='Off'
  print('Outside temperature is too low so the fan was switched off')
 end
 
end
return commandArray
First we check is the value of the temperature sensor we are interested in actually has a value - if it doesn't - we don't need to do any processing - it was a different sensor.

Code: Select all

if (devicechanged['OldExt_Temperature']) then
this works because if there is no temperature - this statement evaluates to FALSE otherwise if it contains any value at all - it will evaluate to TRUE - and therfore process the following logic tests.

the other important point is to convert the temperature to a number - otherwise LUA will complain about trying to compare strings.

Code: Select all

temp = tonumber(devicechanged['OldExt_Temperature'])
Hope this helps someone - now we can either add more code to compare against the time - or create a virtual device that is switched on and off with timers in Domoticz and then just test if that device is active - eg:

Code: Select all

if (temp >= 15.0 and otherdevices['TestBedroom'] == 'Off' and otherdevices['isFanAllowedToBeOn']=='On') then
AEOTEC ZStick, 11 ZWave Nodes, RFXCOMM, 50ish Byron Sockets.. HE851 (PIR), 2x HE852 (DoorContact)
WS2300, CM180, CC128, 2xTHGR122NX, 2xPiZeroW w/DS18B20, 8Ch 1W Relay Board.
8 Panasonic IP Cams, 1 16ch CCTV DVR + 15 CCTV Cams

asjmcguire
Posts: 139
Joined: Saturday 13 July 2013 2:45
Target OS: Linux
Domoticz version: 3.5877
Location: Scotland
Contact:

Re: LUA Scripts

Post by asjmcguire » Tuesday 16 July 2013 2:31

Quick note to say - this quick modification works for me - change times to suit.

Code: Select all

commandArray = {}
if (devicechanged['OldExt_Temperature']) then
print('OldExt is ' .. devicechanged['OldExt_Temperature'])
temp = tonumber(devicechanged['OldExt_Temperature'])
time = os.date("*t")

 if (temp >= 20.0 and otherdevices['TestBedroom'] == 'Off' and (time.hour >= 22 or time.hour <= 14)) then
  commandArray['TestBedroom']='On'
  print('Outside temperature is hot, switching fan on')
 end

 if (temp <= 20.0 and otherdevices['TestBedroom'] == 'On') then
  commandArray['TestBedroom']='Off'
  print('Outside temperature is too low so the fan was switched off')
 end
 
end
return commandArray
AEOTEC ZStick, 11 ZWave Nodes, RFXCOMM, 50ish Byron Sockets.. HE851 (PIR), 2x HE852 (DoorContact)
WS2300, CM180, CC128, 2xTHGR122NX, 2xPiZeroW w/DS18B20, 8Ch 1W Relay Board.
8 Panasonic IP Cams, 1 16ch CCTV DVR + 15 CCTV Cams

jfcjfc
Posts: 51
Joined: Monday 15 July 2013 14:37
Target OS: -
Domoticz version:
Location: France
Contact:

Re: LUA Scripts (error - help please )

Post by jfcjfc » Thursday 18 July 2013 13:55

error
I don't understand in LUA, it work in python with json command all the time

(otherdevices or devicechanged)

error : attempt to compare two nil values => really 27.4 and 12.0

.....
--Tsal = tonumber(otherdevices['Tp-MS-Salon_Temperature'])
--Tbt = tonumber(otherdevices['Tp-Basse_Temperature'])
Tsal = tonumber(devicechanged['Tp-MS-Salon_Temperature'])
print(Tsal)sourceforge domoticz
Tbt = tonumber(devicechanged['Tp-Basse_Temperature'])
print(Tbt)
if Tsal > Tbt then
.........
Heating control : Dz / python json - http://sourceforge.net/p/domoticz/discu ... fe9d/#4e1f
Heating control : Dz / Blockly - http://www.domoticz.com/forum/viewtopic.php?f=15&t=66
Last edited by jfcjfc on Thursday 18 July 2013 15:00, edited 2 times in total.

asjmcguire
Posts: 139
Joined: Saturday 13 July 2013 2:45
Target OS: Linux
Domoticz version: 3.5877
Location: Scotland
Contact:

Re: LUA Scripts

Post by asjmcguire » Thursday 18 July 2013 14:14

The Wiki ( http://www.domoticz.com/wiki/Events ) says that to get the temperature for other devices - you need to use:
Temperature, humidity and barometer values for other devices can be found in otherdevices_temperature['yourdevice'],otherdevices_humidity['yourdevice'] and otherdevices_barometer['yourdevice'] tables.
So you would want to be trying:
Tsal = tonumber(otherdevices_temperature['Tp-MS-Salon'])

for instance...
AEOTEC ZStick, 11 ZWave Nodes, RFXCOMM, 50ish Byron Sockets.. HE851 (PIR), 2x HE852 (DoorContact)
WS2300, CM180, CC128, 2xTHGR122NX, 2xPiZeroW w/DS18B20, 8Ch 1W Relay Board.
8 Panasonic IP Cams, 1 16ch CCTV DVR + 15 CCTV Cams

jfcjfc
Posts: 51
Joined: Monday 15 July 2013 14:37
Target OS: -
Domoticz version:
Location: France
Contact:

Re: LUA Scripts

Post by jfcjfc » Thursday 18 July 2013 14:56

Thanks asjmcguire,
but no success, error :

"attempt to index global 'otherdevices_Temperature' ( a nil value)

....
Tsal = tonumber(otherdevices_Temperature['Tp-MS-Salon'])
print(Tsal)
Tbt = tonumber(otherdevice_Temperature['Tp-Basse'])
print(Tbt)
................
with Temperature ou temperature

=> IRC ?? #domoticz

.......
Heating control : Dz / python json - http://sourceforge.net/p/domoticz/discu ... fe9d/#4e1f
Heating control : Dz / Blockly - viewtopic.php?f=15&t=66

jfcjfc
Posts: 51
Joined: Monday 15 July 2013 14:37
Target OS: -
Domoticz version:
Location: France
Contact:

LUA SCRIPTS

Post by jfcjfc » Thursday 18 July 2013 18:37

RESOLVED ( previous posts with errors)

Tomorrow the LUA script of HEATING CONTROL ( version 1)


-- devicestatus table
-- Tp-Basse ; svalues column : 18.0 (virtual device - temperature) / devicestatut.type = 80)
-- Tp-MS-Salon : svalues column : 27.3;44;0 (temperature-humidity..) / devicestatut.type = 82)
...
Tms = string.sub(otherdevices_svalues['Tp-MS-Salon'],1,4)
--print('Tp-MS-Salon= ' .. Tms .. ' *$')
Tb = otherdevices_svalues['Tp-Basse']
--print('Tp-Basse= ' .. Tb .. ' $$')
Tv = otherdevices_svalues['Tp-Vacances']
--print('Tp-Vacances= ' .. Tv .. ' $$')
Th = otherdevices_svalues['Tp-Haute']
--print('Tp-Haute= ' .. Th .. ' $$')
Tr = otherdevices_svalues['Tp-radiateur-BUR-JF']
--print('Tp-Haute= ' .. Tr .. ' $$')
.....

User avatar
CopyCatz
Developer
Posts: 751
Joined: Thursday 11 July 2013 17:28
Target OS: -
Domoticz version:
Location: Outer Heaven
Contact:

Re: LUA Scripts

Post by CopyCatz » Thursday 18 July 2013 18:53

Woops, lua scripting is still using otherdevices_svalues instead of _temperature etc... will fix that.

User avatar
CopyCatz
Developer
Posts: 751
Joined: Thursday 11 July 2013 17:28
Target OS: -
Domoticz version:
Location: Outer Heaven
Contact:

Re: LUA Scripts

Post by CopyCatz » Thursday 18 July 2013 20:39

- v1.867 Temp, hum and baro values are passed to Lua as proper numbers instead of strings. otherdevices_svalues[] is still passed as a string to access other measurement devices for the time being.

User avatar
tommit01
Posts: 328
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi
Domoticz version: multiple
Location: Germany / SL
Contact:

Re: LUA Scripts

Post by tommit01 » Thursday 18 July 2013 21:57

CopyCatz wrote:- v1.867 Temp, hum and baro values are passed to Lua as proper numbers instead of strings. otherdevices_svalues[] is still passed as a string to access other measurement devices for the time being.
Can also calculated values be read like Dew Point or will we need a own function (Blockly element) with input temperature and humidty for this (later) ?
1 x RPi3
4 x RPI
3 x RPi2
1 x RPI2 + Win 10 IoT (experimental device)
2 x RPI2 Raspbian "Jessie" + Domoticz (for tenants)
2 x RPI OSMC @ 1GHz
1 RPi Zero W + Cam NOIR
1 RPi Zero W + Cam
RFXtrx 433 FW 1015
RFXtrx 433 E FW 1020

User avatar
CopyCatz
Developer
Posts: 751
Joined: Thursday 11 July 2013 17:28
Target OS: -
Domoticz version:
Location: Outer Heaven
Contact:

Re: LUA Scripts

Post by CopyCatz » Thursday 18 July 2013 22:38

dew point isnt in yet.. I'm slowly going to add more variables now that the events system is stable again.

User avatar
mbliek
Posts: 811
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Script examples (Lua)

Post by mbliek » Saturday 20 July 2013 15:21

Any idea why this isn't working?

Code: Select all

t1 = os.time()
s = otherdevices_lastupdate['Huiskamer']

year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)

commandArray = {}

t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))
if (otherdevices['Huiskamer'] == 'No Motion' and difference > 600 and difference < 700) then
	commandArray['Bank']='Off'
   commandArray['LED boven TV']='Off'
	print('Geen beweging, alle lichten uit.')
end 

return commandArray
Last edited by mbliek on Saturday 20 July 2013 17:45, edited 1 time in total.

jfcjfc
Posts: 51
Joined: Monday 15 July 2013 14:37
Target OS: -
Domoticz version:
Location: France
Contact:

Re: Script examples (Lua)

Post by jfcjfc » Saturday 20 July 2013 16:51

commandArray['Bank']='Off'
commandArray['LED boven TV']='Off'
print('Geen beweging, alle lichten uit.')


1 command, 2 command and 3 print => same identation ?

User avatar
CopyCatz
Developer
Posts: 751
Joined: Thursday 11 July 2013 17:28
Target OS: -
Domoticz version:
Location: Outer Heaven
Contact:

Re: Script examples (Lua)

Post by CopyCatz » Saturday 20 July 2013 17:46

spacing around equal signs?

User avatar
CopyCatz
Developer
Posts: 751
Joined: Thursday 11 July 2013 17:28
Target OS: -
Domoticz version:
Location: Outer Heaven
Contact:

Re: Script examples (Lua)

Post by CopyCatz » Tuesday 30 July 2013 23:40

Made a time script that determines whether the living room is empty, if there's no movement for an hour switch on dummy device "Huiskamer leeg" (livingroom empty).

Code: Select all

function timedifference (s)
	year = string.sub(s, 1, 4)
	month = string.sub(s, 6, 7)
	day = string.sub(s, 9, 10)
	hour = string.sub(s, 12, 13)
	minutes = string.sub(s, 15, 16)
	seconds = string.sub(s, 18, 19)
	t1 = os.time()
	t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
	difference = os.difftime (t1, t2)
	return difference
end

commandArray = {}

if (otherdevices['Bewegingsmelder voor'] == 'No Motion' and otherdevices['Bewegingsmelder achter'] == 'No Motion' and otherdevices['Bewegingsmelder zithoek'] == 'Off') then
	if (timedifference(otherdevices_lastupdate['Bewegingsmelder voor']) > 3600 and timedifference(otherdevices_lastupdate['Bewegingsmelder achter']) > 3600 and timedifference(otherdevices_lastupdate['Bewegingsmelder zithoek']) > 3600) then
		if (otherdevices['Huiskamer leeg'] == 'Off') then
			commandArray['Huiskamer leeg']='On'
		end
	end	
end

return commandArray
After which I can run other stuff based on the fact that the room is empty.

kevlard
Posts: 4
Joined: Sunday 14 July 2013 23:57
Target OS: -
Domoticz version:
Contact:

Re: Script examples (Lua)

Post by kevlard » Tuesday 30 July 2013 23:46

Nice work, that'll come in handy. Now I still need a switch to empty my room after a party ;-)

User avatar
mbliek
Posts: 811
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Script examples (Lua)

Post by mbliek » Wednesday 31 July 2013 8:59

@CopyCats thanks for that script. Will try it out when I'm home.

User avatar
mbliek
Posts: 811
Joined: Friday 12 July 2013 14:08
Target OS: Raspberry Pi
Domoticz version: beta
Location: the Netherlands
Contact:

Re: Script examples (Lua)

Post by mbliek » Thursday 01 August 2013 20:50

It work great.

The script will set al lights Off and Arm my alarm between 22:00 and 05:00.

Code: Select all

function timedifference (s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end
time = os.date("*t")
commandArray = {}

if (otherdevices['Huiskamer'] == 'No Motion' and time.hour >= 22 and time.hour < 05) then
   if (timedifference(otherdevices_lastupdate['Huiskamer']) > 3600 and timedifference(otherdevices_lastupdate['Huiskamer']) < 3780) then
      commandArray['Scene:Alle lichten']='Off'
      commandArray['Alarm']='Arm Home'
      print('Geen beweging, alle lichten uit en alarm ingeschakeld.')
   end   
end

return commandArray
Menno

User avatar
CopyCatz
Developer
Posts: 751
Joined: Thursday 11 July 2013 17:28
Target OS: -
Domoticz version:
Location: Outer Heaven
Contact:

Re: Script examples (Lua)

Post by CopyCatz » Friday 02 August 2013 11:12

I don't know who put the os command example in the wiki, but thanks! Opens up a whole new world :)

Post Reply

Who is online

Users browsing this forum: BakSeeDaa, Dropshot and 2 guests