Skip to content

Instantly share code, notes, and snippets.

@kevintyll
Last active January 19, 2024 14:47
Show Gist options
  • Save kevintyll/631772a9dfe7d9b53e4f36473d9709d2 to your computer and use it in GitHub Desktop.
Save kevintyll/631772a9dfe7d9b53e4f36473d9709d2 to your computer and use it in GitHub Desktop.
Vera luup startup functions
local rblt = require("RBLuaTest")
rbLuaTest = rblt.rbLuaTest
luup.register_handler("rbLuaTest","LuaTest")
HOUSE_MODE_PLUGGIN = 40
HOME = '1'
AWAY = '2'
NIGHT = '3'
VACATION = '4'
SS_SID = "urn:micasaverde-com:serviceId:SecuritySensor1" -- Security Sensor Service ID
SP_SID = "urn:upnp-org:serviceId:SwitchPower1" -- Switch Power Service ID
D_SID = "urn:upnp-org:serviceId:Dimming1"
HD_SID = "urn:micasaverde-com:serviceId:HaDevice1"
HM_SID = "urn:micasaverde-com:serviceId:HouseModes1"
TS_SID = "urn:upnp-org:serviceId:TemperatureSensor1"
MOTION_DELAY_PERIOD = 300 -- 5 min.
function split(pString, pPattern)
local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table,cap)
end
last_end = e+1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end
function toggleDimmer(device)
local switchOnOff = luup.variable_get(SP_SID, "Status", device)
if (switchOnOff == "1") then
-- Switch is on
luup.call_action(D_SID, "SetLoadLevelTarget", {newLoadlevelTarget = "0"}, device)
else -- switch is off
luup.call_action(D_SID, "SetLoadLevelTarget", {newLoadlevelTarget = "100"}, device)
end
return true
end
function motionLight(sensorDeviceNo, light, period)
if (luup.variable_get(SP_SID, 'Status', light) == '0') then
args = sensorDeviceNo .. ',' .. light .. ',' .. period
return luup.call_delay ("motionLightDelay", period, args)
else
return false
end
end
function motionLightDelay(args)
list = split(args, ",")
sensorDeviceNo = tonumber(list[1])
light = tonumber(list[2])
period = tonumber(list[3])
local lastTrip = luup.variable_get (SS_SID, "LastTrip", sensorDeviceNo) or os.time()
if (os.difftime (os.time(), tonumber (lastTrip)) >= period) then
-- Turn off the lights.
luup.call_action (SP_SID, "SetTarget", {newTargetValue = "0"}, light)
else
luup.call_delay ("motionLightDelay", period, args) -- Check when the sensor was last tripped every <period> seconds.
end
end
function setNightMode()
luup.call_action(HM_SID, "SetHMode", {newHModeValue = NIGHT}, HOUSE_MODE_PLUGGIN)
end
function pollAlarmInterface()
-- Since the dimming device I am using as an interface with my go control panel does not send it's status
-- to vera instantly, I have to manually poll it to get a timely status update.
-- The system does not seem to like polling the device every 2 seconds forever, so I will only poll it when
-- the motion sensor has been tripped, which is next to the alarm pad. If someone is at the alarm pad, the
-- motion sensor will be tripped calling this function.
-- There is a variable_watch in the startup that will call checkAlarmEvent when the alarmInterface device
-- has it's status changed.
local alarmInterface = 107
local laundryMotionSensor = 28
local isTripped = luup.variable_get (SS_SID, "Tripped", laundryMotionSensor) == '1'
luup.call_action(HD_SID, "Poll", {}, alarmInterface)
if (isTripped) then
luup.call_timer("pollAlarmInterface", 1, "2", "") -- 1=Interval timer 2=every 2 seconds
end
end
function checkAlarmEvent(alarmInterface, _lul_service, _lul_variable, _lul_value_old, alarmStatus)
local siren = 129
if alarmStatus == '0' then -- Disarm
-- Home
luup.call_action(HM_SID, "SetHMode", {newHModeValue = HOME}, HOUSE_MODE_PLUGGIN)
-- Turn off Siren
luup.call_action(SP_SID,"SetTarget",{newTargetValue= '0'}, siren)
elseif alarmStatus == '8' then -- Arm Stay
-- Night
luup.call_delay ("setNightMode", 900) -- wait 15 min.
elseif alarmStatus == '17' then -- Arm Away
-- Away
luup.call_action(HM_SID, "SetHMode", {newHModeValue = AWAY}, HOUSE_MODE_PLUGGIN)
elseif alarmStatus == '25' then -- Alarm
-- Sound Siren
luup.call_action(SP_SID,"SetTarget",{newTargetValue= '1'}, siren)
end
end
function keepACInRange(ac_unit, energy_mode)
local HVAC_UOM_SID = 'urn:upnp-org:serviceId:HVAC_UserOperatingMode1'
local TSC_SID = 'urn:upnp-org:serviceId:TemperatureSetpoint1_Cool'
local TSH_SID = 'urn:upnp-org:serviceId:TemperatureSetpoint1_Heat'
local HVAC_FOM_SID = 'urn:upnp-org:serviceId:HVAC_FanOperatingMode1'
local COMFORT_HEAT_TEMP = 73
local COMFORT_COOL_TEMP = 76
local MIN_HEAT_TEMP = 65
local MAX_COOL_TEMP = 85
local heat = nil
local cool = nil
currentEnergyMode =luup.variable_get(HVAC_UOM_SID, 'EnergyModeStatus', ac_unit)
currentHouseMode = luup.variable_get(HM_SID, 'HMode', HOUSE_MODE_PLUGGIN)
if ((currentHouseMode == HOME) or (currentHouseMode == NIGHT)) then
-- Don't change it if it's not in AutoChangeOver. Someone has manually adjusted it.
if (luup.variable_get(HVAC_UOM_SID, 'ModeStatus', ac_unit) ~= 'AutoChangeOver') then
return false
end
else
energy_mode = 'EnergySavingsMode'
end
if (energy_mode == 'EnergySavingsMode') then
heat = MIN_HEAT_TEMP
cool = MAX_COOL_TEMP
else
print('set heat')
heat = COMFORT_HEAT_TEMP
print(heat)
cool = COMFORT_COOL_TEMP
end
if (currentEnergyMode ~= energy_mode) then
luup.call_action(HVAC_UOM_SID, 'SetEnergyModeTarget', {NewModeTarget = energy_mode}, ac_unit)
luup.call_action(HVAC_FOM_SID, 'SetMode', {NewMode = 'Auto'}, ac_unit) -- Fan
luup.call_action(TSH_SID, 'SetCurrentSetpoint', {NewCurrentSetpoint = heat}, ac_unit) -- Heat
luup.call_action(TSC_SID, 'SetCurrentSetpoint', {NewCurrentSetpoint = cool}, ac_unit) -- Cool
end
end
function checkMasterBedroomAC()
local thermostat = 14
local currentHouseMode = luup.variable_get(HM_SID, 'HMode', HOUSE_MODE_PLUGGIN)
local energyMode = nil
if (currentHouseMode == HOME) then
local currentTime = os.time()
local timeTable = os.date("*t", currentTime)
local year = timeTable['year']
local month = timeTable['month']
local day = timeTable['day']
-- From 9:00 am to 11:00 pm
if ((currentTime > os.time{year=year, month=month, day=day, hour=9}) and (currentTime < os.time{year=year, month=month, day=day, hour=23})) then
energyMode = 'EnergySavingsMode'
else
energyMode = "Normal"
end
elseif (currentHouseMode == NIGHT) then
energyMode = "Normal"
else
energyMode = 'EnergySavingsMode'
end
keepACInRange(thermostat, energyMode)
luup.call_timer("checkMasterBedroomAC", 1, "1m", "") -- 1=Interval timer 1m=every 1 minute
end
function checkFamilyRoomAC()
local thermostat = 11
local currentHouseMode = luup.variable_get(HM_SID, 'HMode', HOUSE_MODE_PLUGGIN)
local energyMode = nil
if (currentHouseMode == HOME) then
local currentTime = os.time()
local timeTable = os.date("*t", currentTime)
local year = timeTable['year']
local month = timeTable['month']
local day = timeTable['day']
local dayOfWeek = timeTable['wday']
if ((dayOfWeek == 6) or (dayOfWeek == 0)) then
-- Saturday or Sunday
energyMode = "Normal"
else
-- From 10:00 am to 2:00 pm
if ((currentTime > os.time{year=year, month=month, day=day, hour=10}) and (currentTime < os.time{year=year, month=month, day=day, hour=14})) then
energyMode = 'EnergySavingsMode'
else
energyMode = "Normal"
end
end
elseif (currentHouseMode == NIGHT) then
energyMode = "Normal"
else
energyMode = 'EnergySavingsMode'
end
keepACInRange(thermostat, energyMode)
luup.call_timer("checkFamilyRoomAC", 1, "1m", "") -- 1=Interval timer 1m=every 1 minute
end
function checkTheaterAC()
local thermostat = 16
local windowCurtain = 113
local theaterTemp = luup.variable_get(TS_SID, 'CurrentTemperature', thermostat)
local curtainStatus = luup.variable_get(SP_SID, 'Status', windowCurtain)
if (tonumber(theaterTemp) > 80) then
if (curtainStatus == '1') then -- curtain is open
toggleDimmer(windowCurtain)
end
end
if (tonumber(theaterTemp) < 75) then
if (curtainStatus == '0') then -- curtain is closed
toggleDimmer(windowCurtain)
end
end
luup.call_timer("checkTheaterAC", 1, "1m", "") -- 1=Interval timer 1m=every 1 minute
end
function startUp()
local alarmInterface = 107
luup.variable_watch('checkAlarmEvent', D_SID, 'LoadLevelStatus', alarmInterface)
checkMasterBedroomAC()
checkFamilyRoomAC()
checkTheaterAC()
end
startUp()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment