Skip to content

Instantly share code, notes, and snippets.

@ErikDeBruijn
Last active December 17, 2019 20:04
Show Gist options
  • Save ErikDeBruijn/88646b06389be62de9bd5f6bc8275de5 to your computer and use it in GitHub Desktop.
Save ErikDeBruijn/88646b06389be62de9bd5f6bc8275de5 to your computer and use it in GitHub Desktop.
HC2 lua scripts
--[[
%% autostart
%% properties
11 value
1002 value
1003 value
1004 value
1005 value
1006 value
1007 value
958 value
1018 value
%% globals
--]]
-- sensor in garage: 13
-- sensor in mancave: 1334 --
-- Aeon labs light sensor: 960
luxSensor1_id = 13
luxSensor1_gain = 1.0
--[[ CHANGELOG:
2017-11-13 Made it multi-light per room!
2016-11-06 Simplified turning off/permanent on function
2016-11-06 Save last activity to globals that can be accessed for context
aware actions (e.g. disable lights in to room I'm probably in)
2016-10-24 Allow lights to be off again after turning on permanently
2016-10-24 Work with both HUE and Fibaro lights
2016-10-24 Better debugging
2016-10-24 pirSensorId is now determined, not a variable anymore!
--]]
local sensorLights = {
[1018]= {lamps = {{id=84,brightness = "100"}}, timeout = 180, lux = 50}; -- garage
[1002]={lamps = {{id=992,brightness = "6"}}, timeout = 30, lux = 70}; -- hal plafondlamp 2x1
[1003]={lamps = {
{id=976,brightness = "100"}
-- {id=1308,brightness="100"}
}, timeout = 60, lux = 50}; -- mancave
[1004]={lamps = {{id=22,brightness = "100"}}, timeout = 180, lux = 50}; -- bijkeuken lux
[1005]={lamps = {{id=1378,brightness = "20"}}, timeout = 180, lux = 50}; -- keukenspots
[1006]={lamps = {{id=972,brightness = "50"}}, timeout = 120, lux = 50}; -- woonkamer wandlampen woonkamer
[1007]={lamps = {{id=23,brightness = "100"}}, timeout = 600, lux = 50}; -- badkamer
}
-- [958]= {lamps = {{id=1300,brightness = "100"}}, timeout = 15, lux = 10000}; -- inloopkast
function brightness(lampId,brightness)
fibaro:call(lampId,"setVolume",tostring(brightness))
fibaro:call(lampId,"setValue",tostring(brightness))
end
function turnOn(lamps)
for k,lamp in pairs(lamps) do
fibaro:debug("Lamp "..fibaro:getName(lamp.id).. " ON, brightness: " .. lamp.brightness.."%.")
brightness(lamp.id, lamp.brightness)
fibaro:call(lamp.id,"turnOn")
end
end
function turnOff(lamps)
for k,lamp in pairs(lamps) do fibaro:call(lamp.id, "turnOff") end
end
function isIsBrightEnough(threshold)
local luxValue1 = tonumber(fibaro:getValue(luxSensor1_id, "value"))
local luxValue = luxValue1 * luxSensor1_gain
if(tonumber(luxValue) > threshold) then
fibaro:debug("Licht: "..luxValue.." lux > threshold: "..threshold..". Stopping.")
return true
else
fibaro:debug("Licht: "..luxValue.." lux <= threshold: "..threshold..". Continuing...")
return false
end
end
if (fibaro:getSourceTrigger()["type"] ~= "property") then return fibaro:debug("Ignoring manual triggers. Bye.") end
local pirSensorId = fibaro:getSourceTrigger()['deviceID']
local lamps = sensorLights[pirSensorId].lamps
local targetBrightness = lamps[1].brightness --sensorLights[pirSensorId].brightness
local inactivityTimeout = sensorLights[pirSensorId].timeout
local pirValue = fibaro:getValue(pirSensorId, "value")
fibaro:debug ("Scene in "..fibaro:getRoomName( fibaro:getRoomID(pirSensorId) ) .." triggered.")
fibaro:debug("PIR sensor "..pirSensorId.." has value: "..pirValue .. ". Last breached: " .. (os.time() - fibaro:getValue(pirSensorId, "lastBreached")) .. " seconds ago.")
if (pirValue == "1") then
fibaro:setGlobal('lastTriggeredPirID',tostring(pirSensorId))
fibaro:setGlobal('ActiveRoomID',tostring(fibaro:getRoomID(pirSensorId)))
fibaro:setGlobal('lastAutoLightID',tostring(lamps[1].id))
fibaro:setGlobal('autolight',fibaro:getName(lamps[1].id))
if(isIsBrightEnough(sensorLights[pirSensorId].lux) == true) then return end
if(fibaro:getValue(lamps[1].id, "value")=="0" or fibaro:getValue(lamps[1].id, "on")=="0") then
turnOn(lamps)
local originalBreach = fibaro:getValue(pirSensorId, "lastBreached")
while((os.time() - fibaro:getValue(pirSensorId, "lastBreached")) < inactivityTimeout) do
if(originalBreach ~= fibaro:getValue(pirSensorId, "lastBreached")) then
originalBreach = fibaro:getValue(pirSensorId, "lastBreached")
brightness(lamps[1].id, lamps[1].brightness)
fibaro:debug("inactivityTimeout(before)="..inactivityTimeout)
if((os.time() + 10 - fibaro:getValue(pirSensorId, "lastBreached")) > inactivityTimeout) then
inactivityTimeout = inactivityTimeout * 1.5 + 10
fibaro:debug("inactivityTimeout(after)!="..inactivityTimeout)
end
inactivityTimeout = inactivityTimeout * 1.1 + 5
fibaro:debug("inactivityTimeout(after) ="..inactivityTimeout)
end
-- keep on for at most 30 minutes
inactivityTimeout = math.ceil(math.min(inactivityTimeout, 30*60))
if((os.time() + 10 - fibaro:getValue(pirSensorId, "lastBreached")) > inactivityTimeout) then
brightness(lamps[1].id, math.ceil(lamps[1].brightness/4))
end
fibaro:debug("Sleep 1s. "..(os.time() - fibaro:getValue(pirSensorId, "lastBreached")).." < ".. inactivityTimeout .. ". "..fibaro:getName(lamps[1].id))
fibaro:sleep(1000)
if(fibaro:getValue(lamps[1].id, "value")=="0") then
return
end
end
turnOff(lamps)
end
end
@ErikDeBruijn
Copy link
Author

Hi @gormnass,

I didn't see your comment until now.

Best to first run a simple script manually and then work with events. And use lots of debug lines to make sure you know what it's doing. Otherwise a running script is like a black box you put stuff in and get stuff out of.

I used functions that I defined myself: turnOff() and turnOn(). Note that this also sets the setVolume and setValue through the brightness() function:

function brightness(lampId,brightness)
  fibaro:call(lampId,"setVolume",tostring(brightness))
  fibaro:call(lampId,"setValue",tostring(brightness))
end
function turnOn(lamps)
  for k,lamp in pairs(lamps) do
    fibaro:debug("Lamp "..fibaro:getName(lamp.id).. " ON, brightness: " .. lamp.brightness.."%.")
    brightness(lamp.id, lamp.brightness)
    fibaro:call(lamp.id,"turnOn")
  end
end

What you could try is this:

local lampeID = 290 --hue lamp
fibaro:call(lampeID, "turnOn")
fibaro:call(lampeID,"setVolume","100")
fibaro:call(lampeID,"setValue","100")

If that indeed turns your Hue lamp on, you should also be able to use my entire script, but obviously with your own configuration at the top.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment