Sorry, this is mostly useful for Swedish users. Simple QA that sets a variable if it's a working day or not (i.e. bank holidays). Uses the api from dryg.net.
Good to have to control some automation - avoid turning on the lamps too early on a non-working day...
Helgdagar-2.fqa
Code example with creating global variables and an update loop that runs every midnight and survives daylight savings....
version = "v0.1"
local format = string.format
local function getDay()
local t=os.date("*t")
net.HTTPClient():request(
"https://api.dryg.net/dagar/v2.1/"..format("%s/%d/%d",t.year,t.month,t.day),{
options= {
headers={['Content-type']='application/json; charset=utf-8'},
method="GET"
},
error=function(err) print(err) end,
success=function(status)
local data = json.decode(status.data)
local dag = data.dagar[1]
QA:updateView("version","text","Helgdagar "..version..", "..dag.datum)
QA:updateView("arbetsfri","text","Arbetsfri dag: "..dag["arbetsfri dag"])
QA:updateView("flagga","text","Flaggdag: "..dag.flaggdag)
QA:updateView("namnsdag","text","Namnsdag: "..table.concat(dag.namnsdag,","))
QA:setVariable("Arbetsfri",dag["arbetsfri dag"])
QA:setVariable("Flaggdag",dag.flaggdag)
QA:setVariable("Namnsdag",table.concat(dag.namnsdag,","))
QA:trace("Today is ",dag.datum)
end,
}
)
end
local function createVar(name,deflt)
local v = api.get("/globalVariable/"..name)
if v==nil then api.post("/globalVariables",{name=name,value=deflt}) end
end
function QuickApp:onInit()
QA = self
self:debug("Helgdagar",version)
createVar("Namnsdag","")
createVar("Flaggdag","")
createVar("Arbetsfri","Nej")
local function loop()
getDay()
local t = os.date("*t") -- allow us to get in sync if we pass daylight saving....
t.sec,t.min,t.hour=0
local time=os.time(t)
time=time+24*60*60 -- Next day
setTimeout(loop,1000*(time-os.time()))
end
loop()
end
Variables set:
"Namnsdag" - Name(s) of the day
"Flaggdag" - if flag dat - reason for flagging
"Arbetsfri" - "Ja"/"Nej"