Page 1 of 7
Script examples (Lua)
Posted: Monday 15 July 2013 2:30
by asjmcguire
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.
Re: LUA Scripts
Posted: Monday 15 July 2013 8:22
by CopyCatz
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.
Re: LUA Scripts
Posted: Tuesday 16 July 2013 2:15
by asjmcguire
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
Re: LUA Scripts
Posted: Tuesday 16 July 2013 2:31
by asjmcguire
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
Re: LUA Scripts (error - help please )
Posted: Thursday 18 July 2013 13:55
by jfcjfc
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
Re: LUA Scripts
Posted: Thursday 18 July 2013 14:14
by asjmcguire
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...
Re: LUA Scripts
Posted: Thursday 18 July 2013 14:56
by jfcjfc
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
LUA SCRIPTS
Posted: Thursday 18 July 2013 18:37
by jfcjfc
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 .. ' $$')
.....
Re: LUA Scripts
Posted: Thursday 18 July 2013 18:53
by CopyCatz
Woops, lua scripting is still using otherdevices_svalues instead of _temperature etc... will fix that.
Re: LUA Scripts
Posted: Thursday 18 July 2013 20:39
by CopyCatz
- 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.
Re: LUA Scripts
Posted: Thursday 18 July 2013 21:57
by tommit01
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) ?
Re: LUA Scripts
Posted: Thursday 18 July 2013 22:38
by CopyCatz
dew point isnt in yet.. I'm slowly going to add more variables now that the events system is stable again.
Re: Script examples (Lua)
Posted: Saturday 20 July 2013 15:21
by mbliek
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
Re: Script examples (Lua)
Posted: Saturday 20 July 2013 16:51
by jfcjfc
commandArray['Bank']='Off'
commandArray['LED boven TV']='Off'
print('Geen beweging, alle lichten uit.')
1 command, 2 command and 3 print => same identation ?
Re: Script examples (Lua)
Posted: Saturday 20 July 2013 17:46
by CopyCatz
spacing around equal signs?
Re: Script examples (Lua)
Posted: Tuesday 30 July 2013 23:40
by CopyCatz
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.
Re: Script examples (Lua)
Posted: Tuesday 30 July 2013 23:46
by kevlard
Nice work, that'll come in handy. Now I still need a switch to empty my room after a party

Re: Script examples (Lua)
Posted: Wednesday 31 July 2013 8:59
by mbliek
@CopyCats thanks for that script. Will try it out when I'm home.
Re: Script examples (Lua)
Posted: Thursday 01 August 2013 20:50
by mbliek
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
Re: Script examples (Lua)
Posted: Friday 02 August 2013 11:12
by CopyCatz
I don't know who put the os command example in the wiki, but thanks! Opens up a whole new world
