### About Lua

Some Hint & Tips for using Lua
Fileheader Example
--~/home/pi/domoticz/scripts/lua/script_device_windmeter.lua
-----------------------------------------------------------------------------
-- If the a value of the windmeter has changed, sent to the log
-- @project: haFuchsbau
-- @author: Robert W.B. Linn
-- @since: 20150719
-- @version: 1.0
-- @see: readme.txt
-----------------------------------------------------------------------------
Device Changed All Devices
----------------------------------------------------------------------------------------------------------
-- Get device changed information. This will capture information for any device
----------------------------------------------------------------------------------------------------------
tc=next(devicechanged);
v=tostring(tc);
if (v == 'mydevice') then
-- do something
end
Device Changed Specific Device
----------------------------------------------------------------------------------------------------------
-- Action for specific device idc the device with name Windmeter defined in a local WindmeterName
-- IMPORTANT: After the device has been added first time ensure to REBOOT the Domoticz server first and then use this LUA script
----------------------------------------------------------------------------------------------------------
local WindmeterName = 'Windmeter';
if devicechanged[WindmeterName] then
-- do something
end
Device get all Values
---------------------------------------------------------------------------------------------------------
- Get device changed information. This will capture information for any device
tc=next(devicechanged);
v=tostring(tc);
---------------------------------------------------------------------------------------------------------
print("v=" .. v)
- Print the device values for any device that changed value
if (v ~= 0) then
print('Script_Device_Values:' .. v .. ' :: Value changed: ' .. otherdevices_svalues[v]);
end
Include Function from external File
----------------------------------------------------------------------------------------------------------
-- Include external functions
-- Important: define the path to the lua file, load and compile the file and then define by calling
----------------------------------------------------------------------------------------------------------
local path = "/home/pi/domoticz/scripts/lua/mathutils.lua"
local mathsutils = loadfile(path)
mathsutils()
Update Device Value
----------------------------------------------------------------------------------------------------------
-- Update the virtual text device
----------------------------------------------------------------------------------------------------------
local DeviceIdx = 12
local NewText = 'Hello World'
commandArray['UpdateDevice']=DeviceIdx .. '|0|' .. NewText
Update User Variable Value
wsb = 12
commandArray['Variable:vWindgust']= tostring(wsb)
Update Device Value
----------------------------------------------------------------------------------------------------------
-- Update the virtual text device
----------------------------------------------------------------------------------------------------------
local DeviceIdx = 12
local NewText = 'Hello World'
commandArray['UpdateDevice']=DeviceIdx .. '|0|' .. NewText
Function Round Number
function roundnum(num, idp)
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
Print Time HH:MM:SS
time = os.date("*t")
ts = string.format("%02d:%02d:%02d", time.hour, time.min, time.sec);
print(string.format('Device %s changed @ %s', v, ts));
Calculate Days between two dates
----------------------------------------------------------------------------------------------------------
-- Calculate date difference between now and the target date
----------------------------------------------------------------------------------------------------------
-- Set the target date 31 March 2016
reference = os.time{day=1, month=3, year=2016}
-- Get the time diff between now and the target date in seconds in a day
daysfrom = os.difftime(reference, os.time()) / (24 * 60 * 60)
-- Convert into days diff
daysdiff = math.floor(daysfrom)
-- Define the text to display. for today it prints "1"
daysdiffs = daysdiff .. ' day(s) till 31.03.2016.'
print('Script_Device_' .. TimerSwitchName .. ' Update=' .. daysdiffs)
Send command using curl to a B4J Webserver
local B4JWebserverIP = 'http://192.168.0.2:51042'
----------------------------------------------------------------------------------------------------------
-- Build the URL to send to the B4J Server
-- The curl parameter are added using -d parameter=value
----------------------------------------------------------------------------------------------------------
cmd = 'curl '
cmd = cmd .. B4JWebserverIP
cmd = cmd .. ' -d WindmeterAllValues="' .. otherdevices_svalues[WindmeterName] .. '"'
cmd = cmd .. ' -d Winddir=' .. otherdevices_winddir[WindmeterName]
cmd = cmd .. ' -d Windspeed=' .. otherdevices_windspeed[WindmeterName]
cmd = cmd .. ' -d Windgust=' .. otherdevices_windgust[WindmeterName]
print('Cmd:' .. cmd)
----------------------------------------------------------------------------------------------------------
-- Send command to B4J Webserver
----------------------------------------------------------------------------------------------------------
os.execute (cmd)
Send Ping Command
ping_success=os.execute('ping -c1 192.168.0.20')
if ping_success then
print("ping success")
else
print("ping fail")
end
Capture Result Shell Command
-- Lua Reference: http://www.lua.org/pil/21.2.2.html
local handle = io.popen("ping -c1 192.168.0.20")
-- Read all chars from the file
local result = handle:read("*all")
handle:close()
print(string.format("Ping Result is %s", result))
Update GPIO Pin
commandArray = {}
-- gpio read pin 18. use command option -g for BCM mode
-- LUA Reference Manual = www.lua.org/manual/5.2/manual.html
local handle = io.popen("gpio -g read 18")
-- Read the number value from the file
local result = handle:read("*n")
handle:close()
print(string.format("State of PIN 18 is %d", result))
if result == 0 then
os.execute("gpio -g write 18 1")
print("LED ON")
commandArray['Ping']='ON'
else
os.execute("gpio -g write 18 0")
print("LED OFF")
commandArray['Ping']='OFF'
end
return commandArray

Changed: 2018-06-07 09:10, Category: Domoticz Home Automation, File: T6C186M1341.htm, ID: 1341