Recently I finally bought some Z-Wave equipment:

  • Aeon Labs Z-Stick Gen5
  • Aeon Labs Z-Wave LED Bulb with RGBW (2x)
  • Fibaro Door/Window Sensor
  • Fibaro Wall Plug (4x)

I've looked at several Open Source Home Automation projects before, but so far the only one that is, in my opinion, lightweight enough to run on embedded hardware is Domoticz. It's also relatively easy to configure. Can't say the same of OpenHAB, for example.

Anyway, I'm not going to go into much detail in this post, I'm just going to share my first Domoticz LUA script: a standby killer. It allows you to configure a usage threshold for the Fibaro Wall Plugs, and if the usage goes below the threshold, it will switch off the Wall Plug. I found similar scripts out there, but most of them were limited to hardcoding one or two devices.

So I made a script that allows you to add as many Wall Plugs as you want, with different thresholds, by putting stuff in a multi-dimensional array. Mind you, this is my first LUA script ever, so forgive me if it causes eye cancer!

Here goes:

-- lua_time_standbykiller

commandArray = {}

devices = {}

devices['WallPlug Door'] = {}
devices['WallPlug Door']['msuffix'] = ': Usage'
devices['WallPlug Door']['threshold'] = 10

devices['WallPlug L1'] = {}
devices['WallPlug L1']['msuffix'] = ': Usage'
devices['WallPlug L1']['threshold'] = 10

devices['WallPlug R1'] = {}
devices['WallPlug R1']['msuffix'] = ': Usage'
devices['WallPlug R1']['threshold'] = 10

devices['WallPlug R2'] = {}
devices['WallPlug R2']['msuffix'] = ': Usage'
devices['WallPlug R2']['threshold'] = 1

function log (msg)
    print('StandbyKiller: ' .. msg)
end

function timedifference (s)
    year = string.sub(s, 1, 4)
    month = string.sub(s, 6, 7)
    day = string.sub(s, 9, 10)
    hour = string.sub(s, 12, 13)
    minutes = string.sub(s, 15, 16)
    seconds = string.sub(s, 18, 19)
    t1 = os.time()
    t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
    difference = os.difftime (t1, t2)
    return difference
end

function toggle_after (dev, state, seconds)
    if (timedifference(otherdevices_lastupdate[dev]) > 300) then
        if (otherdevices[dev] ~= state) then
            log('toggling device: ' .. dev)
            if (seconds > 0) then
                state = state .. ' AFTER ' .. seconds
            end
            print('state: ' .. state)
            commandArray[dev] = state
        end
    end
end

-- loop through all devices
for deviceName,deviceValue in pairs(otherdevices) do
    if (devices[deviceName] and devices[deviceName]['msuffix']) then
        mdev = deviceName .. devices[deviceName]['msuffix']
        if (otherdevices[deviceName] == "On") then
            if (devices[deviceName]['threshold']) then
                if (otherdevices_svalues[mdev]) then
                    watt = tonumber(otherdevices_svalues[mdev])
                    if (watt < devices[deviceName]['threshold']) then
                        log('device below threshold: ' .. deviceName)
                        toggle_after(deviceName, "Off", 30)
                    end
                end
            end
        end
    end
end


return commandArray