--[[ This Lua script posts data to a Sen.se device feed. Replace the apikey and feedid with yours. It's a basic script, showing the HTTP POST method. I didn't write it from scratch, I found the basic code here http://forum.micasaverde.com/index.php?topic=7396.0 and then changed it to work in a HAH Applet, I'm now sharing it in case anyone else would like to do something similiar. The public output of this can be seen here :- http://open.sen.se/sensemeters/tab/2295/?preview=on --]] --_DEBUG=1 module(...,package.seeall) require("xap") require('ltn12') socket = require("socket") http = require("socket.http") info={ version="1.2", description="Sense" } function send_to_sense(feed, value) -- print("Send to Sense") local apikey = "YOUR_API_KEY" local base_url = "http://api.sen.se/events/" local method = "POST" local json_data = "{ \"feed_id\": " .. feed .. ", \"value\": \"" .. value .. "\" }" local response_body = {} local response, status, header = http.request{ method = "POST", url = base_url, headers = { ["Content-Type"] = "application/json", ["Content-Length"] = string.len(json_data), ["sense_key"] = apikey }, source = ltn12.source.string(json_data), sink = ltn12.sink.table(response_body) } end function elec_usage(frame) -- print("Elec_usage") local feedid = "YOUR_FEED_ID" local elec = tostring(frame:getValue("input.state","text")) -- print(string.format("Feed = %s, and elec is %s", feedid, elec)) send_to_sense(feedid, elec) end function init() -- print("System Starting") --[[ Setup trigger for electricity usage --]] local e = xap.Filter() e:add("xap-header","source","dbzoo.livebox.CurrentCost:ch.1") -- e:add("xap-header","class","xAPBSC.event") e:add("input.state","state","on") e:callback(elec_usage) end --xap.init("dbzoo.lua.sense","FF00DD00" ) --init() --xap.process()