Lua generic function library
Moderator: leecollings
-
- Posts: 14
- Joined: Thursday 24 November 2016 12:14
- Target OS: Linux
- Domoticz version:
- Contact:
Re: Lua generic function library
True, only from a file.
-
- Posts: 536
- Joined: Friday 23 December 2016 16:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Stable
- Location: Netherlands Purmerend
- Contact:
Re: Lua generic function library
Nice work , many thanks for sharing!
-
- Posts: 497
- Joined: Friday 22 May 2015 12:21
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.11083
- Location: Asten NB Nederland
- Contact:
Re: Lua generic function library
great work
Your mind is like a parachute,
It only works when it is opened!
RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
It only works when it is opened!
RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
- Siewert308SW
- Posts: 288
- Joined: Monday 29 December 2014 15:47
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Stable
- Location: The Netherlands
- Contact:
Re: Lua generic function library
It aint much and some snippets are well known wile others i scrapped or wrote myself to help my event script look cleaner.
Most of them i use for a long, long time
But below your'll find some functions i use for all my Lua events...
Most of them i use for a long, long time
But below your'll find some functions i use for all my Lua events...
Code: Select all
--[[
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
@ functions.lua
@ author : Siewert Lameijer
@ since : 1-1-2015
@ updated : 14-11-2018
@ Global Functions
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
--]]
--
-- **********************************************************
-- Function to execute os commands and get output
-- **********************************************************
-- Example: os.capture(curl 'http://127.0.0.1:8080/json.htm?type=command¶m=getSunRiseSet')
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[{}]+', ' ')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
--
-- **********************************************************
-- Time Difference
-- **********************************************************
-- Example: and timedifference(otherdevices_lastupdate['light.living_standing_light']) >= 15
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 Between X hours and X hour
-- **********************************************************
-- Example: and timebetween("03:00:00","11:59:59")
function timebetween(s,e)
timenow = os.date("*t")
year = timenow.year
month = timenow.month
day = timenow.day
s = s .. ":00"
e = e .. ":00"
shour = string.sub(s, 1, 2)
sminutes = string.sub(s, 4, 5)
sseconds = string.sub(s, 7, 8)
ehour = string.sub(e, 1, 2)
eminutes = string.sub(e, 4, 5)
eseconds = string.sub(e, 7, 8)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=shour, min=sminutes, sec=sseconds}
t3 = os.time{year=year, month=month, day=day, hour=ehour, min=eminutes, sec=eseconds}
sdifference = os.difftime (t1, t2)
edifference = os.difftime (t1, t3)
isbetween = false
if sdifference >= 0 and edifference <= 0 then
isbetween = true
end
return isbetween
end
--
-- **********************************************************
-- Get Zwave Power Plug current Watts
-- **********************************************************
-- powerusage['zwave_wattmeter'] >= 15 then
function powerusage(powerplug)
local powerplug = powerplug
local reading
local usage
reading = otherdevices[powerplug]
_,_,usage = string.find(reading, "(.+)")
current_usage = tonumber(reading)
return current_usage
end
--
-- **********************************************************
-- Check for motion in home overall for specific time (seconds)
-- **********************************************************
-- Example: if motion('true', 600) then
--
function motion(input, minutes)
input = input
minutes = minutes
if input == 'true' or input == 'false' then
IsMotion = false
if input == 'false' then
if timedifference(otherdevices_lastupdate[door.living]) > minutes
and timedifference(otherdevices_lastupdate[door.front]) > minutes
and timedifference(otherdevices_lastupdate[door.back]) > minutes
and timedifference(otherdevices_lastupdate[door.garden]) > minutes
and timedifference(otherdevices_lastupdate[door.scullery]) > minutes
and timedifference(otherdevices_lastupdate[door.pantry]) > minutes
and timedifference(otherdevices_lastupdate[motion_sensor.living]) > minutes
and timedifference(otherdevices_lastupdate[motion_sensor.downstairs]) > minutes
and timedifference(otherdevices_lastupdate[motion_sensor.upstairs]) > minutes
and timedifference(otherdevices_lastupdate[motion_sensor.toilet]) > minutes
and timedifference(otherdevices_lastupdate[motion_sensor.dinner1]) > minutes
and timedifference(otherdevices_lastupdate[motion_sensor.dinner2]) > minutes
and timedifference(otherdevices_lastupdate[motion_sensor.kitchen]) > minutes
and timedifference(otherdevices_lastupdate[motion_sensor.natalya]) > minutes
and timedifference(otherdevices_lastupdate[motion_sensor.hallway]) > minutes
and timedifference(otherdevices_lastupdate[motion_sensor.porch]) > minutes
then
IsMotion = true
end
end
if input == 'true' then
if timedifference(otherdevices_lastupdate[door.living]) <= minutes
or timedifference(otherdevices_lastupdate[door.front]) <= minutes
or timedifference(otherdevices_lastupdate[door.back]) <= minutes
or timedifference(otherdevices_lastupdate[door.garden]) <= minutes
or timedifference(otherdevices_lastupdate[door.scullery]) <= minutes
or timedifference(otherdevices_lastupdate[door.pantry]) <= minutes
or timedifference(otherdevices_lastupdate[motion_sensor.living]) < minutes
or timedifference(otherdevices_lastupdate[motion_sensor.downstairs]) <= minutes
or timedifference(otherdevices_lastupdate[motion_sensor.upstairs]) <= minutes
or timedifference(otherdevices_lastupdate[motion_sensor.toilet]) <= minutes
or timedifference(otherdevices_lastupdate[motion_sensor.dinner1]) <= minutes
or timedifference(otherdevices_lastupdate[motion_sensor.dinner2]) <= minutes
or timedifference(otherdevices_lastupdate[motion_sensor.kitchen]) <= minutes
or timedifference(otherdevices_lastupdate[motion_sensor.natalya]) <= minutes
or timedifference(otherdevices_lastupdate[motion_sensor.hallway]) <= minutes
or timedifference(otherdevices_lastupdate[motion_sensor.porch]) <= minutes
then
IsMotion = true
end
end
return IsMotion
end
end
--
-- **********************************************************
-- Phones Online
-- **********************************************************
-- Example: if phones_online('true') then
--
function phones_online(input)
input = input
if input == 'true' or input == 'false' then
IsPhonesOnline = false
if otherdevices[phone.jerina] == 'On' or otherdevices[phone.siewert] == 'On' or otherdevices[phone.natalya] == 'On'
then
if input == 'true' then
IsPhonesOnline = true
end
end
if otherdevices[phone.jerina] == 'Off' and otherdevices[phone.siewert] == 'Off' and otherdevices[phone.natalya] == 'Off'
then
if input == 'false' then
IsPhonesOnline = true
end
end
return IsPhonesOnline
end
end
--
-- **********************************************************
-- Laptops Online
-- **********************************************************
-- Example: if laptops_online('true') then
--
function laptops_online(input)
input = input
if input == 'true' or input == 'false' then
IsLaptopsOnline = false
if otherdevices[laptop.jerina] == 'On' or otherdevices[laptop.siewert] == 'On' or otherdevices[laptop.natalya] == 'On' then
if input == 'true' then
IsLaptopsOnline = true
end
end
if otherdevices[laptop.jerina] == 'Off' and otherdevices[laptop.siewert] == 'Off' and otherdevices[laptop.natalya] == 'Off' then
if input == 'false' then
IsLaptopsOnline = true
end
end
return IsLaptopsOnline
end
end
--
-- **********************************************************
-- Media PowerUsage
-- **********************************************************
-- Example: if media_powered('true') then
--
function media_powered(input)
input = input
if input == 'true' or input == 'false' then
IsMediaPowered = false
if powerusage(watt.tvcorner) > watt.media_usage or powerusage(watt.natalya) > watt.media_usage then
if input == 'true' then
IsMediaPowered = true
end
end
if powerusage(watt.tvcorner) <= watt.media_usage and powerusage(watt.natalya) <= watt.media_usage then
if input == 'false' then
IsMediaPowered = true
end
end
return IsMediaPowered
end
end
-- **********************************************************
-- Media ON? if on then do certain light scene
-- **********************************************************
-- Example: if media('true') then
--
function media(input)
input = input
if input == 'true' or input == 'false' then
IsMedia = false
if powerusage(watt.tvcorner) > watt.media_usage or powerusage(watt.siewert) > watt.media_usage or powerusage(watt.jerina) > watt.media_usage then
if input == 'true' then
IsMedia = true
end
end
if powerusage(watt.tvcorner) <= watt.media_usage and powerusage(watt.siewert) <= watt.media_usage and powerusage(watt.jerina) <= watt.media_usage then
if input == 'false' then
IsMedia = true
end
end
return IsMedia
end
end
--
-- **********************************************************
-- Laptop PowerUsage
-- **********************************************************
-- Example: if laptops_powered('true') then
--
function laptops_powered(input)
input = input
if input == 'true' or input == 'false' then
IsLaptopPower = false
if powerusage(watt.siewert) > watt.laptop_usage or powerusage(watt.jerina) > watt.laptop_usage then
if input == 'true' then
IsLaptopPower = true
end
end
if powerusage(watt.siewert) <= watt.laptop_usage and powerusage(watt.jerina) <= watt.laptop_usage then
if input == 'false' then
IsLaptopPower = true
end
end
return IsLaptopPower
end
end
--
-- **********************************************************
-- Is weekend?
-- **********************************************************
-- Example: if weekend('true') then
-- weekday [0-6 = Sunday-Saturday]
--
function weekend(input)
input = input
if input == 'true' or input == 'false' then
Isweekend = false
local dayNow = tonumber(os.date("%w"))
if dayNow == 5 and timebetween("00:00:00","21:59:59") and uservariables[var.holiday] == 0 then
if input == 'false' then
Isweekend = true
end
end
if dayNow == 5 and timebetween("22:00:00","23:59:59") and uservariables[var.holiday] == 0 then
if input == 'true' then
Isweekend = true
end
end
if dayNow == 5 and timebetween("00:00:00","23:59:59") and uservariables[var.holiday] == 1 then
if input == 'true' then
Isweekend = true
end
end
-- **********************************************************
if dayNow == 6 and timebetween("00:00:00","23:59:59") then
if input == 'true' then
Isweekend = true
end
end
-- **********************************************************
if dayNow == 0 and timebetween("00:00:00","21:59:59") and uservariables[var.holiday] == 0 then
if input == 'true' then
Isweekend = true
end
end
if dayNow == 0 and timebetween("22:00:00","23:59:59") and uservariables[var.holiday] == 0 then
if input == 'false' then
Isweekend = true
end
end
if dayNow == 0 and timebetween("00:00:00","23:59:59") and uservariables[var.holiday] == 1 then
if input == 'true' then
Isweekend = true
end
end
-- **********************************************************
if (dayNow == 1 or dayNow == 2 or dayNow == 3 or dayNow == 4) and uservariables[var.holiday] == 0 then
if input == 'false' then
Isweekend = true
end
end
if (dayNow == 1 or dayNow == 2 or dayNow == 3 or dayNow == 4) and uservariables[var.holiday] == 1 then
if input == 'true' then
Isweekend = true
end
end
return Isweekend
end
end
--
-- **********************************************************
-- Blink Light IsNotDimmer
-- **********************************************************
-- Example: blink('light_living', 3)
function blink(light, times)
times = times or 2
cmd1 = 'Off'
cmd2 = 'On'
pause = 7
if (otherdevices[light] == 'Off') then
cmd1 = 'On'
cmd2 = 'Off'
end
for i = 1, times do
commandArray[#commandArray+1]={[light]=cmd1..' AFTER '..pause }
pause = pause + 1
commandArray[#commandArray+1]={[light]=cmd2..' AFTER '..pause }
pause = pause + 1
end
end
--
-- **********************************************************
-- Get otherdevices_svalues
-- **********************************************************
-- Example: device_svalue('outside_temp_sensor') > 5
function device_svalue(device)
device = tonumber(otherdevices_svalues[device])
devices_svalues = device
return devices_svalues
end
--
-- **********************************************************
-- Scrap Thermostat and outside temperatures
-- **********************************************************
-- Example: nesttemp_svalue('nest.room_temp') > 5
function nesttemp_svalue(device)
local thermostat = device
sNestTemp, sNestHumidity = otherdevices_svalues[thermostat]:match("([^;]+);([^;]+)")
nest_temp = tonumber(sNestTemp)
nest_hum = tonumber(sNestHumidity)
return nest_temp
end
Setup:
- RPi4 - Domo Stable / Aeotec Z-stick7 / PiHole Unbound Gemini
- RPi4 - PiHole / PiVPN Unbound Gemini
- Synology DS923+ / DS218j
- P1 Gas/Power, SmartGateway watermeter
- Fibaro switches, contacts, plugs, smoke/Co2 ect
- rootfs @ USB HDD
- RPi4 - Domo Stable / Aeotec Z-stick7 / PiHole Unbound Gemini
- RPi4 - PiHole / PiVPN Unbound Gemini
- Synology DS923+ / DS218j
- P1 Gas/Power, SmartGateway watermeter
- Fibaro switches, contacts, plugs, smoke/Co2 ect
- rootfs @ USB HDD
Who is online
Users browsing this forum: No registered users and 1 guest