logging temperature

I’ve got some 3-in-1 motion sensors that report temperature. If I want to grab the temperature value remotely from vera, what URL should I use? Will it give me the last value reported when the sensor last woke up? or cause a poll to take place? Thanks.
–Jim

Try this Lua function (devnum is the device id of your temperature sensor and devnum_parent is the device id of its parent):

function gtemp(devnum, devnum_parent)

current_temp = luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”,“CurrentTemperature”,devnum)

last_wakeup = luup.variable_get(“urn:micasaverde-com:serviceId:ZWaveDevice1”,“LastWakeup”,devnum_parent)

last_wakeup = tonumber(last_wakeup)

age_in_seconds = os.time()-last_wakeup

return current_temp, age_in_seconds

end

BTW, please note bug #1244:

http://bugs.micasaverde.com/view.php?id=1244

If you want to be sure that the temperature reading is correct, you must check that
current time - LastWakeup <= WakeupInterval

I can’t believe that such a bug gets ignored that long.

sorry for the noob question, but…

Where do i stick that lua function and what do i wrap it in if i want to log that temp to a file every hour? thanks.

Here’s how to get the temperature via curl:
[tt]

curl test

ip=‘192.168.1.100’
user=‘foo:bar’
deviceId=26
serviceId=‘urn:upnp-org:serviceId:TemperatureSensor1’
var=‘CurrentTemperature’

curl -v --user $user http://:3480$ip/data_request?id=lu_variableget\&DeviceNum=$deviceId\&serviceId=$serviceId\&Variable=$var\&output_format=xml
[/tt]

The output_format parameter seems to have no effect.

Basically, there are two ways to control Vera’s devices:

[ul][li]
Luup Lua extensions: http://wiki.micasaverde.com/index.php/Luup_Lua_extensions[/li]
[li]http requests: http://wiki.micasaverde.com/index.php/Luup_Requests
[/li][/ul]

For a Lua/Luup centric / single point of configuration approach, create a scene, add Lua code (and a timer): Use my function, add Lua calls to my function for your devices and add Lua code to store the values in Vera’s filesystem. If required, you can run the scene from Vera’s GUI and via http (http://ipadress:3480/data_request?id=action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=).

See attachment for a complete Lua implementation of temperature logging.

FYI… that attachment looks to be in ms-rtf format… so if you try to pull it up in vi or notepad it will not look too pretty… Just rename to rtf and let wordpad snag it…

Attachment should be fixed now.

thanks ap15e,

I’ve tested that I can ‘pull’ the values I need from vera via http requests, but
if I want to ‘push’ the values from vera to a logging system then it looks like i’ll
need to dig into the lua code.

I created a scene, copied and pasted your lua code into the scene luup code box, set a timer for each hour, changed the deviceNums and saved. If I click on Run, i get a Success alert: “the command to test the scenario was sent.”

problems

  1. If I ssh to my vera, I don’t see the file being created in /etc/cmh/, so i’m not getting any output.

  2. If I edit the code in the scene, and click “save lua”, it looks like the edits are saved, but if I then do a SAVE on the main UI4 page, the changes are lost.

questions

  1. Does vera come with the “socket.http” package? I’d like to POST the data to an ajax function on my logging system.
  2. Is there a way to debug the lua code in the shell via ssh? require “luup” fails.

Thanks

1) Does vera come with the "socket.http" package? I'd like to POST the data to an ajax function on my logging system.

Yes, it includes [tt]LuaSocket[/tt]. It also includes [tt]LuaSec[/tt] if the “target” Website is using [tt]HTTPS[/tt].

2) Is there a way to debug the lua code in the shell via ssh? require "luup" fails.

The luup stuff is included automatically when you’re running code under Vera’s framework (Scenes, Plugins etc). There’s no need to use [tt]require(“luup”)[/tt] at all for this.

You end up writing code in the places that Vera provides, and using [tt]luup.log(…)[/tt] calls to log output along the way. Any errors, along with output from [tt]luup.log[/tt], go to the Log file called:

[tt] /var/log/cmh/LuaUPnP.log[/tt]

This gets rotated, and trucated/removed periodically so you generally watch them as they’re generated (using [tt]tail[/tt])

There’s a Wiki post out there on most of these items.

@jgc94131

Just checked that my code does work for my system (from the ‘Test Luup code’ window).
temp_light.log does exist.

Are you sure that you are using the correct device ids?

When I use the MiOS developer widget, Test Luup code (Lua) tab, and cut and paste the code.
Here’s what I get in the log when I hit GO. I’ve left the Device Number field blank.

01	10/19/10 9:24:46.552	JobHandler_LuaUPnP::RunLua can't find device interface <0x2c0c>
08	10/19/10 9:25:01.446	JobHandler_LuaUPnP::HandleActionRequest device: 0 service: urn:micasaverde-com:serviceId:HomeAutomationGateway1 action: RunLua <0x2c0c>
08	10/19/10 9:25:01.447	JobHandler_LuaUPnP::HandleActionRequest argument Code=function gtemp(devnum, devnum_parent)

	local current_temp   = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",devnum)
... rest of code follows

It looks to me from the log that the code needs to implement an interface rather than be free form. Is this documented somewhere?

Again i’m running version: 1.1.1047
The file “/tmp/log/cmh/testscene.log” is not getting created.

Please replace the device ids in my code with the device ids of your devices and try my code without further modifications. Check for the file /etc/cmh/temp_light.log

I assigned the bug mentioned above to one of the developers so he can fix it.

OK, so here’s what I’m using to log temperature, light level, battery level etc. from various hsm100 3-in-1 sensors at various locations around the US.

The files “http_util.lua” and “print_r.lua” have been scp’d to /usr/shared/lua on each vera that
I want to run this code on. This location is fine for lua includes, and may or may not survive through an upgrade cycle.

For each sensor that I want to log, I set up a scene, create a timer with the sample interval, say 1 hour, and paste the file “log_sensor_scene.lua” into the luup code text box and save.

Right now the POST is only over http, not https, but it’s a fairly simple change to support https (i.e. SSL). I also have not implemented authentication, since I don’t need it.

The logging is being done for me by some simple PHP scripts and a MySQL database. I’m happy to share that code if anyone is interested.

–Jim

Thanks Jim, this is very useful indeed! :smiley:

Consider grabbing the status of all your devices using this

for XML formatted output:
http://<ip_address_of_vera>:3480/data_request?id=lu_sdata&output_format=xml

for JSON formatted output:
http://<ip_address_of_vera>:3480/data_request?id=lu_sdata&output_format=json

then parsing out the temperatures (or other values) you need for your log.

Hi Jim,

I would very much appreciate if you could share the code for your simple PHP scripts and a MySQL database.

I am not a php and sql shark ;D

Thanks.

/Henrik

I use this method to log temps, then every few weeks I SSH into to Vera and pull the logs out.
I can then take these temps and plop them into the google chart API and chart everything
Eventually I would love to get these put into a local SQL DB to automate all this.

Setup a Scene, add a timer to this scene to run every hour, or whatever interval you wish
Then in the LUUP tab paste in this code below, change the .log filename and the device number of the temp sensor


local outf = io.open('/etc/cmh/temp-mainbasement.log', 'a')

outf:write(os.date('%Y-%m-%d %H:%M:%S '..os.time()..' ')..luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",56))
outf:write('\n')
outf:close()