Re: Script examples (Lua)
Posted: Thursday 19 September 2013 19:30
Than you need to change 8080 to 80
I did that alreadymbliek wrote:Than you need to change 8080 to 80
Code: Select all
1 * * * * /ipresence/bluepresence.sh
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 = {}
print('evaluating if someone is upstairs')
if (otherdevices['topfloor_motion'] == 'Off') and (timedifference(otherdevices_lastupdate['topfloor_ceiling_lamp']) > 120) then
if (timedifference(otherdevices_lastupdate[topfloor_motion']) > 1800) then
if timedifference(otherdevices_lastupdate['top_stairs_motion']) < timedifference(otherdevices_lastupdate['topfloor_motion']) then
commandArray['Scene:topfloor_lights']='Off'
print('turning off, no movement upstairs for more than 30m')
end
end
end
Code: Select all
t1 = os.time()
s = otherdevices_lastupdate['Bedtime']
-- returns a date time like 2013-07-11 17:23:12
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 = {}
-- *** Get the difference between last Bedtime switch operate time and now
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))
-- *** Check to see if Bedtime switch is on
if (otherdevices['Bedtime'] == 'On') then
td = "Time Difference = " .. difference
print (td)
print ("Device - Bedtime Switch is On")
-- *** Check to see if all lights are off, if so reset the Bedtime switch
if (otherdevices['Kevin Lamp'] == 'Off') and (otherdevices['Sharon Lamp'] == 'Off') and (otherdevices['Kitchen Main'] == 'Off') and (otherdevices['Kitchen Kettle'] == 'Off') and (otherdevices['Conservatory Lamp'] == 'Off') and (otherdevices['Outside Lights'] == 'Off') then
print ("All the lamps are Off")
commandArray['Bedtime']='Off'
commandArray['SendNotification']='Bedtime Alert#All lights Off - Resetting Bedtime switch'
end
-- *** Turn off Front Room lights after 60 seconds [1 min] (check duration to 180 seconds [3 min])
if difference > 60 and difference < 180 then
commandArray['Kevin Lamp']='Off'
commandArray['Sharon Lamp']='Off'
print ("Front room lights Off - 1 minute after activation")
end
-- *** Turn off Conservatory light and Kitchen Main after 300 seconds [5 min] (check duration to 420 seconds [7 min])
if difference > 300 and difference < 420 then
commandArray['Conservatory Lamp']='Off'
commandArray['Kitchen Main']='Off'
print ("Conservatory and Kitchen Main lights Off - 5 minutes after activation")
end
-- *** Turn off Kitchen Kettle lights after 600 seconds [10 min] (check duration to 720 seconds [12 min])
if difference > 600 and difference < 720 then
commandArray['Kitchen Kettle']='Off'
print ("Kitchen Kettle light Off - 10 minutes after activation")
end
-- *** Turn off Outside lights after 10800 seconds [3 hrs] (check duration to 10920 seconds [3 hrs 2 min])
if difference > 10800 and difference < 10920 then
commandArray['Outside Lights']='Off'
print ("Outside lights Off - 3 hours after activation")
end
end
return commandArray
Code: Select all
if ((devicechanged['Elektriciteit'])) then
print ("Devicechanged elektriciteit")
print (otherdevices_svalues['Graaddagen'])
print (otherdevices_svalues['Gas'])
--commandArray['OpenURL']='192.168.1.24:8080/json.htm?type=command¶m=udevice&hid=3&did=gas2&dunit=1&dsubtype=0&dtype=113&nvalue=0&svalue=501'
commandArray['OpenURL']='192.168.1.24:8080/json.htm?type=command¶m=udevice&idx=64&nvalue=0&svalue=502'
--commandArray['OpenURL']='127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=64&nvalue=0&svalue=503'
--commandArray['UpdateDevice']='64|0|504'
end
Code: Select all
-------------------------------------------------------------------------------
--- Lua Time script v1.0
--- Runs every minute
---
--- Purpose:
--- Reads solar xray flux value from website and graphs it in temperature graph
--- Background: http://en.wikipedia.org/wiki/Solar_flare
-------------------------------------------------------------------------------
--- returns num rounded up to idp decimals
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
commandArray = {}
--- To have a 1MB /var/tmp directory in RAM add the line
--- tmpfs /var/tmp tmpfs nodev,nosuid,size=1M 0 0
--- to the /etc/fstab file
---
--- Read Solar data from http://sol24.net/m/ into xray_data_string
tmp_filename = '/var'..os.tmpname()
os.execute('curl "http://sol24.net/m/" > '..tmp_filename)
infile = io.open(tmp_filename,'r')
xray_data_str=infile:read('*all')
--and clean up temp file
infile:close()
os.remove( tmp_filename )
-- Process xray_data_string to retrieve current x-ray value near label "flux:" which looks like C3.7
xray_pos = string.find(xray_data_str, "X-ray flux:")
xray_power10 = string.sub(xray_data_str,xray_pos+88, xray_pos+88)
xray_value= string.sub(xray_data_str,xray_pos+89, xray_pos+91)
if xray_power10 == 'B' then xray_value_powered = xray_value * 10
elseif xray_power10 == 'C' then xray_value_powered = xray_value * 100
elseif xray_power10 == 'M' then xray_value_powered = xray_value * 1000
elseif xray_power10 == 'X' then xray_value_powered = xray_value * 10000
elseif xray_power10 == 'Y' then xray_value_powered = xray_value * 100000
end
--transform this value to be presented on a logaritmic 0~50 scale
xray_index = 10*round(math.log(xray_value_powered,10),2)
--show what we are doing in the log
print(string.format("Current GOES Xray flux is %s%s indexed to %s.",xray_power10,xray_value,xray_index))
--and put the value in the graph
commandArray['UpdateDevice']='66|0|'..xray_index
--done
return commandArray
Code: Select all
commandArray = {}
-- ***** Switch On Section *****
if (devicechanged['Spare Byron Switch (B1)'] == 'On') then
tNew = os.time()
-- Get current state of single click light
if (otherdevices['Ceiling Unit 15 (H15)'] == 'Off') then
singleclicklamp = "Off"
else
singleclicklamp = "On"
end
-- Get last time switch was operated
local file = io.open("SwitchB1Time.txt", "r")
TimeOnOld = file:read()
TimeOffOld = file:read()
LightStateOld = file:read()
file:close()
-- Compare this on time with last on time and get the difference
On_Difference = tNew - TimeOnOld
Off_Difference = tNew - TimeOffOld
DoubleClick = "Null"
pton = "Time difference in seconds since last On operation = " .. On_Difference
print(pton)
ptoff = "Time difference in seconds since last Off operation = " .. Off_Difference
print(ptoff)
-- Check for Double Click
if (On_Difference < 5) then -- switched on twice within 5 seconds
DoubleClick = "True"
else
DoubleClick = "False"
end
pdc = "Double click event = " .. DoubleClick
print(pdc)
if (DoubleClick == "False") then
commandArray['Ceiling Unit 15 (H15)']='On'
end
if (DoubleClick == "True") then
commandArray['Ceiling Unit 16 (H16)']='On'
if (LightStateOld == "Off") then
commandArray['Ceiling Unit 15 (H15)']='Off'
end
end
-- save new time to file
local file = io.open("SwitchB1Time.txt", "w")
file:write(tNew)
file:write("\n")
file:write (TimeOffOld)
file:write("\n")
file:write(singleclicklamp)
file:close()
print ("File SwitchB1Time.txt written")
end
-- ***** Switch Off Section *****
if (devicechanged['Spare Byron Switch (B1)'] == 'Off') then
tNew = os.time()
-- Get current state of single click light
if (otherdevices['Ceiling Unit 15 (H15)'] == 'Off') then
singleclicklamp = "Off"
else
singleclicklamp = "On"
end
-- Get last time switch was operated
local file = io.open("SwitchB1Time.txt", "r")
TimeOnOld = file:read()
TimeOffOld = file:read()
LightStateOld = file:read()
file:close()
-- Compare this on time with last on time and get the difference
On_Difference = tNew - TimeOnOld
Off_Difference = tNew - TimeOffOld
DoubleClick = "Null"
pton = "Time difference in seconds since last On operation = " .. On_Difference
print(pton)
ptoff = "Time difference in seconds since last Off operation = " .. Off_Difference
print(ptoff)
-- Check for Double Click
if (Off_Difference < 5) then -- switched on twice within 5 seconds
DoubleClick = "True"
else
DoubleClick = "False"
end
pdc = "Double click event = " .. DoubleClick
print(pdc)
if (DoubleClick == "False") then
commandArray['Ceiling Unit 15 (H15)']='Off'
end
if (DoubleClick == "True") then
commandArray['Ceiling Unit 16 (H16)']='Off'
if (LightStateOld == "On") then
commandArray['Ceiling Unit 15 (H15)']='On'
end
end
-- save new time to file
local file = io.open("SwitchB1Time.txt", "w")
file:write(TimeOnOld)
file:write("\n")
file:write (tNew)
file:write("\n")
file:write(singleclicklamp)
file:close()
print ("File SwitchB1Time.txt written")
end
return commandArray
Code: Select all
-- Script to print out the various time and date variables available in Domoticz for reference
-- Output is in the log file [Setup -> Log from the menu]
-- also loads the values into variables for use elsewhere in the script if needed
-- If using this script delete the lines that you do not need to save resources
-- Not all variables have been presented but the full available list is below;
-- Variable details (from http://www.lua.org/pil/22.1.html)
-- %a abbreviated weekday name (e.g., Wed)
-- %A full weekday name (e.g., Wednesday)
-- %b abbreviated month name (e.g., Sep)
-- %B full month name (e.g., September)
-- %c date and time (e.g., 09/16/98 23:48:10)
-- %d day of the month [01-31]
-- %H hour, using a 24-hour clock [00-23]
-- %I hour, using a 12-hour clock [01-12]
-- %M minute [00-59]
-- %m month [01-12]
-- %p either "am" or "pm"
-- %S second [00-61]
-- %w weekday [0-6 = Sunday-Saturday]
-- %x date (e.g., 09/16/98)
-- %X time (e.g., 23:48:10)
-- %Y full year (1998)
-- %y two-digit year [00-99]
-- %% the character `%ยด
print(" ")
print ("*******************")
print(" ")
-- Year
nowyear = os.date("%Y")
print(os.date("Current Year - %Y"))
-- Month
nowmonth = os.date("%m")
print(os.date("Current Month - %m and %b and %B"))
--Day
nowday = os.date("%d")
nowdow = os.date("%a") -- day of week truncated (%A full)
print(os.date("Current day - %d and %a and %A"))
-- Hour
nowhour = os.date("%H")
print(os.date("Current hour (24h) - %H and (12h) - %I"))
-- Minute
nowminute = os.date("%M")
print(os.date("Current minute - %M"))
-- Second
nowsecond = os.date("%S")
print(os.date("Current second - %S"))
-- Full Date
nowdate = os.date("%x")
print(os.date("Current full date - %x"))
-- Full Time
nowtime = os.date("%X")
print(os.date("Current full time - %X"))
-- Full Date and Time
nowdatetime = os.date("%c")
print(os.date("Current full date and time - %c"))
print(" ")
print ("*******************")
print(" ")
commandArray = {}
-- nothing to see here
return commandArray
Hi,alfred_j_kwak wrote:This is maybe more like bash related thing, but I wrote script to check once in a minute if my phone is in range of domoticz server bluetooth. Now I get event once in a minut. I liked to get event only when state changes. Do I have to write current status into file and read it back from file before checking my bluetooth so I could send JSON only when status is different than previous?
Sorry about bad english.![]()
-Jussi-