Code to push sensor value to pachube and sen.se

Hi,
Here is some code i’ve written to push sensors data (or anything from the vera box) to pachube and sen.se in order to graph some values.

At first, you’ll need to create an account and get a apikey.
For sen.se, it’s stil in beta, so you’ll need to ask to participate to the beta before getting your key.

  1. put the two global functions in ‘startup lua’ in ‘developpers’ widget, one for sending to sen.se and the other to pachube.

for sen.se:

function my_send_sense (feed, value)
  local apikey = "YOUR_API_KEY_HERE"
  local base_url = "http://api.sen.se/events/"
  local method = "POST"

  require('ltn12')
  local socket = require("socket")
  local http = require("socket.http")

  local json_data = "{ \"feed_id\": " .. feed .. ", \"value\": \"" .. value .. "\" }"
  local response_body = {}
  local response, status, header = http.request{
      method = "POST",
      url = base_url .. "?sense_key=" .. apikey,
      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

note: there is a bug in the current api, so the “sense_key” parameter in headers is not working. We need to pass the api key in the url. I’ve still leaved the header for memory.

for pachube:

function my_send_pachube (feed, datastream, value)
  local apikey = "YOUR_API_KEY_HERE"
  local base_url = "http://api.pachube.com/v2/feeds/"
  local method = "PUT"

  require('ltn12')
  local socket = require("socket")
  local http = require("socket.http")

  local json_data = '{ "version":"1.0.0","datastreams":[ {"id":"' .. datastream .. '", "current_value":"' .. value .. '"}]}'
  local response_body = {}
  local response, status, header = http.request{
    method = method,
    url = base_url .. feed,
    headers = {
      ["Content-Type"] = "application/json",
      ["Content-Length"] = string.len(json_data),
      ["X-PachubeApiKey"] = apikey
    },
    source = ltn12.source.string(json_data),
    sink = ltn12.sink.table(response_body)
  }
end
  1. you’ll need to create feeds (in pachube) or devices (in sens.se) and setup some parameters (type of data, etc…)
    and get a feed ID

  2. in Vera again, create a scene, triggered by a timer (ex: every 5 minutes) with a lua code like this one :
    (in this sample, I get the value of a EZMotion light sensor and send the info to both pachube and sen.se :

-- id of the monitored device
local deviceID = 34

local lightvalue = luup.variable_get("urn:micasaverde-com:serviceId:LightSensor1", "CurrentLevel", deviceID)

my_send_pachube ("12345", "LightLivingRoom", lightvalue)
my_send_sense("1234", lumsalon)

return true

note1: the values “12345” for pachube and “1234” for sen.se need to be replacer your own feed IDs
note2: in pachube, we can directly provide a ‘datastream’ within the feed, here I’ve used the name ‘LightLivingRoom’ for this feed.
in sen.se, the feed id is directly associated to the ‘datastream’, so we do not need to send the name.

It’s still simple, quick and dirty, but it works.
Next step : write a function wich is able to send multiple values at one time. (to send everything more quickly)

Tom

update : sen.se has corrected the authentication bug, so only providing the header should be ok now :

function my_send_sense (feed, value)
  local apikey = "YOUR_API_KEY_HERE"
  local base_url = "http://api.sen.se/events/"
  local method = "POST"

  require('ltn12')
  local socket = require("socket")
  local http = require("socket.http")

  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

Hello can you update this code (pachube) for Ui5 please :slight_smile:

This code is compatible with UI5, no changes needed.

pachube doesn’t work anymore, it was replaced by cosm

pachube doesn't work anymore, it was replaced by cosm
The code is still the same. Cosm kept the same API as pachube.