On this page you'll find code snippets I found and created myself. I want to share them and hope they help you creating an awesome smarthome.
- LUA: Load URL with basic authentication
- API: update a Quick App
- LUA: read condition/trigger event information
- LUA: How to read if Home, Away,Vacation or Night profile is active
- LUA: HC3 version of
if fibaro:countScenes() > 1 then fibaro:abort() end - LUA: Read variables from QA device
- LUA: Condition to run scene every minute between 09:00 and 11:00
- LUA: Condition to run at 04:00 and when profile VACATION goes active
- LUA: send a push message
- LUA: control ledstrip with native Hue API
- QA: add log text below device icon
- LUA: read Quick App variable
- LUA: control Sonos with Node-RED
- QA: send args and run function with HTTP REST API
- QA: read label/button/slider value
local Host = "192.168.199.134"
local Port = "80"
local tcpTimeout = 2000
local srvUser = "admin"
local srvPwd = "admin"
local texte = string.format("%s %s", tostring(os.date('%H:%m', os.time() )), "HC3 : This is a test.")
local mymsg = string.format("?color=%s&bg=%s&t=%s", "black", "white",texte)
function urlencode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w %-%_%.%~])", function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
function base64(data)
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
return ((data:gsub('.', function(x)
local r,b='',x:byte() for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
return r;
end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if (#x < 6) then return '' end
local c=0
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return b:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end
local bauth = 'Basic '..base64(srvUser..":"..srvPwd)
local url = "http://" .. Host .. ":" .. Port .. "/write" .. urlencode(mymsg)
net.HTTPClient():request(url, {
options={
method = 'GET',
headers = {
["Authorization"] = bauth
},
timeout = tcpTimeout,
},
success = function(response)
print(response.status)
print(response.data)
end,
error = function(message)
print("error:", message)
end
})POST /api/devices/<deviceID>/action/updateProperty
{ "args": [ "value", <value> ] }print("sourceTrigger", json.encode(sourceTrigger)
local event = sourceTrigger
if event.type == "location" and event.value == "enter" and event.property and event.property == 219 then
if between("$Sunset..$Sunrise") then
fibaro.call({780}, "turnOn")
end
endfunction getActiveProfileName()
local profiles = api.get("/profiles")
for _, profile in ipairs(profiles.profiles) do
if (profile.id == profiles.activeProfile) then
return profile.name
end
end
endSet Scene → Basic configuration → Allow to restart a running scene → No
function getVariable(id, name)
local device = api.get("/devices/".. id) or { properties = {} }
for _, v in ipairs(device.properties.quickAppVariables or {}) do
if v.name == name then return v.value end
end
return ""
end{
operator = "all",
conditions = {
{
type = "date",
property = "cron",
operator = "match",
value = {
date = {"*", "9", "*", "*", "*","*"},
},
isTrigger = true
},
{
type = "date",
property = "cron",
operator = "match",
value = {
date = {"*", "10", "*", "*", "*","*"},
},
},
}
}{
operator = "any",
conditions ={
{
type = "profile",
property = "activeProfile",
operator = "==",
value = 3,
isTrigger = true
},
{
type = "date",
property = "cron",
operator = "match",
value = { "30", "20", "*", "*", "*", "*" },
isTrigger = true
}
}
}
...
if (sourceTrigger.type == "date") then
-- CRON trigger
fibaro.debug("Scene6", "Read alarm clock time now...")
elseif (sourceTrigger.type == "profile") then
-- PROFILE changed!
fibaro.debug("Scene6", "Profile changed, do thing here...")
else
-- Manual started by USER
fibaro.debug("Scene6", "Manual triggered by user...")
endfibaro.alert("push", {2}, "Hi, it works!")
fibaro.alert("push", {2, 20}, "Hi, it works for 2 users!")Set the correct brightness and color with the Hue App and read the data with the official API:
http://192.168.2.1/api/<apikey>/lights/
http://192.168.2.1/api/<apikey>/lights/4
"4": {
"state": {
"on": true,
"bri": 114,
"hue": 5046,
"sat": 198,
"effect": "none",
"xy": [
0.5428,
0.3836
],
"ct": 500,
"alert": "none",
"colormode": "xy",
"mode": "homeautomation",
"reachable": true
},
...🌎 Official Hue API documentation
function controlLedstrip(id, state, bri, hue, sat)
local tcpTimeout = 2000
local hueBridgeId = 31
local hueConfig = api.get("/devices?id=" .. tostring(hueBridgeId))
local hueLight = api.get("/devices?id=" .. tostring(id))
local host = hueConfig.properties.ip
local user = hueConfig.properties.userName
local lightId = hueLight.properties.lightId
net.HTTPClient():request("http://" .. host .. "/api/" .. user .. "/lights/" .. lightId .. "/state", {
options={
method = 'PUT',
headers = {['Content-Type'] = 'application/json'},
timeout = tcpTimeout,
data = '{"on":' .. tostring(state) .. ',"bri":' .. tostring(bri) .. ', "hue":' .. tostring(hue) .. ',"sat":' .. tostring(sat) .. '}',
},
success = function(response)
fibaro.debug("Scene38", response.status .. " " .. response.data)
end,
error = function(message)
fibaro.debug("Scene38", "HTTPClient error: " .. message)
end
})
end
controlLedstrip(39, true, 29, 5147, 200)self:updateProperty("log", "insert text here")function getQAVariable(id, var)
local qaDevice = api.get("/devices/" .. id)
for _, quickAppVariables in ipairs(qaDevice.properties.quickAppVariables) do
if (quickAppVariables.name == var) then
return quickAppVariables.value
end
end
end
getQAVariable(43, "alarmTime") -- QA alarm clock sample (binairy switch type)function playMusic(host, players)
net.HTTPClient():request("http://" .. host .. "/sonos/play", {
options={
method = 'POST',
headers = {["Content-Type"] = "application/json"},
timeout = 2000,
data = players
},
success = function(response)
fibaro.debug("Scene6", response.status .. " " .. response.data)
end,
error = function(message)
fibaro.debug("Scene6", "HTTPClient error: " .. message)
end
})
end
local host = "192.168.2.101:1880"
local sleepingAudio = fibaro.getGlobalVariable("SleepingAudio")
playMusic(host, sleepingAudio)function wakeupMusic(host, player, station, volume)
-- payload: {"playerName": "Werkhoek", "station": "KINK", "volume": 10}
net.HTTPClient():request("http://" .. host .. "/sonos/wakeup", {
options={
method = 'POST',
headers = {["Content-Type"] = "application/json"},
timeout = 2000,
data = '{"playerName": "' .. player .. '", "station": "' .. station .. '", "volume": ' .. tostring(volume) .. '}'
},
success = function(response)
fibaro.debug("Scene6", response.status .. " " .. response.data)
end,
error = function(message)
fibaro.debug("Scene6", "HTTPClient error: " .. message)
end
})
end
wakeupMusic("192.168.2.101:1880", "Werkhoek", "NPO Radio 2", 5)function QuickApp:sendMessage(msg)
local message = string.gsub(decode(msg), '\\n', '\n')
fibaro.alert("push", {2,20}, message)
endhttp://192.168.2.55/api/devices/208/action/sendMessage
var scene = {};
var notification = "test"
scene = {
headers: { 'content-type':'application/json' },
payload: { 'args': [notification] }
};
return scene;Source
local function getView(deviceId,name,typ)
local function find(s)
if type(s) == 'table' then
if s.name==name then return s[typ]
else for _,v in pairs(s) do local r = find(v) if r then return r end end end
end
end
return find(api.get("/plugins/getView?id="..deviceId)["$jason"].body.sections)
end
getView(39,"myLabel","text")