--[[ Monitor the light reading from a RoomNode and cause an action when it reaches a certain level --]] --[[ This is just a sample script demonstrating how to create a xAP filter and trigger, call a function and send a message to "do something" if an appropriate condition is met. It is currently setup to run from the command line on the HAH. The best way to do that is to copy it into the /etc/plugboard directory, telnet onto the HAH, change to the plugboard directory and then run it with Lua, ie: lua lightlevel.lua This script uses the "print" function to display output as a form of debugging. To run this script as an Applet I would suggest commenting out the print statements, then you need to un-comment the "--module(...,package.seeall)" line and then remove, or comment out, the last three line in this file. You will then need to remane the file from "lightlevel.lua" to "lightlevelApplet.lua" and restart the plugboard service. I hope you find this useful. ]]-- --module(...,package.seeall) require("xap") info={ version="1.0", description="Hahnode Monitor" } -- Set the lower and upper limit of light when you want the trigger to be actioned. -- If the sensor is near a light source, you might need to adjust these values -- to take that into consideration lightLevelLowTriggerValue = 200 -- Lower value is darker lightLevelHighTriggerValue = 220 -- Higher value is lighter triggerAction = "off" function lightLevelTrigger(frame) print("\nHAHnode Monitor - lightLevelTrigger") -- We only get to this routine if the light level when -- we get a new light level reported -- Create a local variable and put the light level in it local lightLevel = frame:getValue("input.state","text") print("The current light level is : " .. lightLevel) -- If the light level is below a specified value change -- the action appropriately if (tonumber(lightLevel) < lightLevelLowTriggerValue ) then triggerAction = "on" end if (tonumber(lightLevel) > lightLevelHighTriggerValue ) then triggerAction = "off" end print("lightLevelTrigger : " .. triggerAction ) -- Prepare an xap message to be broadcast -- We're going to turn relay 1 on if the light is below our trigger point -- and turn it off it's about local msg=string.format([[ xap-header { class=xAPBSC.cmd target=dbzoo.livebox.Controller:relay.1 } output.state.1 { id=* state=%s }]], triggerAction) -- Send the message xap.sendShort(msg) end function init() print("HAHnode Monitor - Init") -- Create a filter and the condition(s) needed to call our "action" routine f = xap.Filter() -- We want the light level to trigger our routine f:add("xap-header","source", "dbzoo.livebox.jeenode:test.light") -- This is the function we call when a match is made to the filter(s) f:callback(lightLevelTrigger) end -- Comment out or delete these line to run as an Applet xap.init("dbzoo.lua.lightlevel","FF00DE00") init() xap.process()