Post to Pachube (need different feed from main HAH setup etc)

1 reply [Last post]
aivo
Offline
Tallinn, Estonia
Joined: 2 Mar 2011

Occasionally one might need to post data to more than one Pachube feed .. like me ;)

With this applet one can catch specific events / info and post data to Pachube, also to a different feed that is specified in main hah setup.

Note that applet below is not collecting events for bulk send like the main logic does.  For my own need this was quite ok as I knew events of interest would come up once per minute.

Using together with / data from CcSensorsApplet.lua

Enjoy,
Aivo


PachubeApplet.lua


require("xap")
module (...,package.seeall)
 
info = {version="1.0", description="Post to Pachube (when e.g. feed differs from main HAH setup)"}

ApiKey = "5dcd -- cut --30c79"
FeedID = "14380"
URL="http://api.pachube.com/v2/feeds/"..FeedID..".xml"
sourcefilter = "Plugboard"
classfilter = "cd>"             -- cd* as for "current (cost) digital" sensor; counts water, gas, .. meter pulses
classes = { "cdgas", "cdwat" }  -- classes - xAPBSC. suffixes - we want to filter; same used for stream tags
streams = { 51, 52 }        -- stream id-s corresponding to classes
class2stream = { }          -- this array will have class from "classes" id-s as indexes
xmlmain=[[<?xml version=\"1.0\" encoding=\"UTF-8\"?><eeml xmlns=\"http://www.eeml.org/xsd/005\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"5\" xsi:schemaLocation=\"http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd\">%s</eeml>]]
xmlenv=[[<environment id=\"%s\">%s</environment>]]         -- xmlmain contains env; env contains feed id, data
xmldata=[[<data id=\"%s\"><tag>%s</tag><value>%s</value></data>]]    -- data contains stream id, tag, value

function init()
    f = xap.Filter()
    f:add("xap-header", "source", "dbzoo.livebox."..sourcefilter)
    f:add("xap-header", "class", "xAPBSC."..classfilter)
    for i = 1, table.getn(classes) do
        class2stream[classes[i]] = streams[i]
    end
    f:callback(post)
end

function post()
    local class = string.sub(xap.getValue("xap-header","class"), 8)             -- here only "suffix", part after "xAPBSC."
    local value = xap.getValue("input.state","extra")    -- text is primary data: pulse count; extra is liters (gas/water)
    local data = string.format(xmldata, class2stream[class], class, value)    -- stream id, tag, value
    local env = string.format(xmlenv, FeedID, data)                                 -- feed id, data
    local xml = string.format(xmlmain, env)
    local cmd = 'curl --request PUT --header "X-PachubeApiKey: '..ApiKey..'" --data "'..xml..'" '..URL
--    print(cmd)
    os.execute(cmd)
end

kevin9
Offline
Lincolnshire, United Kingdom
Joined: 24 May 2010
Now in the software

From build 300, this capability is a standard feature of HAH

kevint

Hardware Info