I have a script which I wrote on Lua, that worked to get the prices and timestamps, but it won't work on fibaro HC2.
My Lua code:
local requests = require('requests')
local json = require('cjson')
local socket = require('socket.http')
local https = require('ssl.https')
Tomorrow = os.date("%Y-%m-%d",os.time() 24*60*60)
Yesterday = os.date("%Y-%m-%d",os.time()-24*60*60)
URL = "https://dashboard.elering.ee/api/nps/price?start=2021-12-09T20:59:59.999Z&end=2021-12-10T20:59:59.999Z"
first_url = "https://dashboard.elering.ee/api/nps/price?start="
mid_url = "T20:59:59.999Z&end="
end_url = "T20:59:59.999Z"
URL = first_url .. Yesterday .. mid_url .. Tomorrow .. end_url
print(URL)
Times = {}
Prices = {}
local body, code, headers, status = https.request(URL)
if body then
--print(body)
else
--print(code)
end
t = json.decode(body)
for _, v in ipairs(t.data.ee) do table.insert(Times, v.timestamp) end
for _, v in ipairs(t.data.ee) do table.insert(Prices, v.price) end
print(Times[1])
print(Prices[1])
Output:
1649278800.0
98.6
But on HC2 the output is:
[DEBUG] 08:06:18: 2022-04-08 08:06:18.658376 [ fatal] Unknown exception: /opt/fibaro/scenes/1.lua:7: attempt to call global 'require' (a nil value)
What am I doing wrong and how can I get it to work?
CodePudding user response:
As Egor suggested, Fibaro sandboxes their Lua environment. A full list of what is removed is listed at https://manuals.fibaro.com/knowledge-base-browse/blocked-lua-commands/, but it includes require
, dofile
, load
, loadfile
, loadstring
, several functions in the os
library, and the entire io
and package
library.
That effectively requires all of your code to be contained in a single file, with no access to modules, packages, or libraries other than the remaining parts of the standard library, which means you simply can't do what you're trying to do here.