Page 1 of 37

ClimateDataCalculation

Posted: Thu Nov 06, 2014 1:25 pm
by krambriw
This plugin can be used to collect and calculate climate data like rain level, temperature and humidity from wireless sensors.

- Rain, temperature and humidity data can be collected with a RFXtrx
- Temperature and humidity data can be collected with a TellStick Duo (I am lacking a TellStick Duo log sample from a rain sensor to complete the code)
- Data from other devices like z-wave and 1-wire can easily be added and is on the 'ToDo list' but I need sample events from the EG log.

You can download the plugin from here (check the section 'Current plugins just ready for prime time'):
http://krambriw.net

The plugin supports the following features:
- You can configure how frequently you want to save readings (default every 10 minutes)

REVISED:
- For rain, the plugin calculates two moving averages, one fast and one slow. This is then used to create events when the lines are crossing each other. The more readings you select, the slower the response to changes. This is a very useful function to when you are looking for trend shifts (in this case, if rain is increasing or decreasing during time).
- In addition you can add a hysteresis value that will set the upper and lower band (+/- hysteresis/2) for the fast moving average before it triggers an event

- For temperature and humidity, the plugin calculates one moving average. This is then used in combination with the set-point and optionally the hysteresis to create events when the lines are crossing each other. The more readings you select, the slower the response to changes. This is a very useful function to avoid too many status changes when for instance controlling heaters with on/off control equipment (you might temporary open a door or window but the heater should not react immediately)
- In addition you can add a hysteresis value that will set the upper and lower band (set-point +/- hysteresis/2) for the moving average before it triggers an event

- You can select if you want to use the rule evaluation that will generate those events or not. Currently I have only one rule 'equal-less-greater' that will generate events when moving average gets equal, less or greater than its reference.
- All this works for rain level, temperature and humidity data

To collect data I use actions in macros. This simplifies your configuration, you just drag & drop the sensor event in the macro you created.

Sensor data is collected on a per sensor exclusive sqlite3 database table. The more frequent you have selected to store data, the quicker the size of your database will grow.

For all sensor types, there are two main actions
- A capture action to capture sensor data
- A reset action to clear all readings captured from the sensor

Currently only available for rain level data you also have the following actions

- RainReport action reporting the total rain between configured date & time stamps
- RainRequestSearch event triggered action searching for total rain between in event payload provided date & time stamps
- RainRequestQuery action searching for total rain between provided date & time stamps and returning the result as variable, foreseen to be used in scripts or other plugins using rain data
- Sending data search results via web-sockets to update web pages with current rain data (supports both Pako's Websocket Suite and Tornado)

The plugin handles also the known situation when you need to change batteries in the sensor. In this case the sensor id will most likely change. A rain sensor will also start counting from zero again. If you update the sensor id of the existing sensor database, the collecting of readings to the database will continue as before.

In the following I will give some configuration examples.

1) Start to collect sensor data
You just add a 'DataCapture' macro, name the database table and put in the sensor id. Configure the rest of the parameters if you do not accept the default. If you check the box '..use a rule' you will receive events when conditions are met as described above.
Image1.jpg
Then drag & drop the event from the sensor to the macro. You are done, data will be collected as soon as new sensor events arrives.


Once you have some collected (rain) data you can start to query the tables

2) Simplest is to use the 'RainReport' action. Add a new macro for it, select the sensor database table, the start and end date&time stamps and check the boxes if you want to send the result using web-sockets. By default it will just be printed in the log.

3) To request data from a script or similar, use the 'RainRequestSearch' or the 'RainRequestQuery' macro.

As example using the 'RainRequestSearch' macro:
Configure it and select the sensor database table. To trigger it, create an event with a payload specifying the start and end date&time stamps similar to this:

Code: Select all

pl = ['2014-11-07', '2014-11-09']
eg.TriggerEvent(
    'ClimateDataCalculation',
    payload = pl,
    prefix='ClimateDataCalculation'
)
In the example above, the result will be calculated based on the first found records for '2014-11-07' and '2014-11-07'. You can also set the pl similar to the following examples:
['2014-11-07 10', '2014-11-09 22'] #will use records first found 2014-11-07 at 10 and 2014-11-09 at 22
['', '2014-11-08'] #will use from beginning until first record found 2014-11-08
['2014-11-07', ''] #will use from first record found 2014-11-07 until end
[] or ['', ''] #will use all records in the table

Re: ClimateDataCalculation

Posted: Thu Nov 06, 2014 1:28 pm
by krambriw
There is a need for a plugin that can save climate data in a database and send queries for all kind of calculations.

Considering various thoughts I think we are looking at using a db (MySQL for instance). The plugin will save readings from sensors at specified intervals. I think saving every reading is a bit overkill but lets say it should be configurable down to a minute level. In reality maybe once every hour is enough. Once we have the readings in a db we can create all kind of actions with queries for whatever.

I will start with rain data but I have a feeling there will appear some requests also for wind, temperature, humidity and light levels as well so I better design it to be prepared...

If we use web-sockets we can have actions that query the db at defined intervals and pushes the result to web pages that gets automatically updated.

Re: ClimateDataCalculation

Posted: Thu Nov 06, 2014 1:32 pm
by Mastiff
Sounds brilliant, thanks! :D I'm glad I asked the question! The only thing I'm a bit unsure of is the databaseformat. It means that I have to install another thing on my automation machine, right? Do you think that will affect stability?

Oh, and wind and temperature would of course be great too! Not to mention humidity. And barometer pressure...

Do you think it's possible to make this a "ready made kit", with the calls and a sample webpage with the possibilities? That would be even better, I'm not much of a programmer, I'm afraid... :oops:

Re: ClimateDataCalculation

Posted: Thu Nov 06, 2014 4:20 pm
by krambriw
The only thing I'm a bit unsure of is the databaseformat. It means that I have to install another thing on my automation machine, right? Do you think that will affect stability?
I plan to include the MySQL library files. Regarding performance, I do not think it will be a problem. However size is an issue and it will depend how frequent you log sensors and how many. But if you have a reasonable disk size it should not be a problem.

It will be easy to give examples with web pages that are working with websockets. It is already somewhere some examples around in this forum. Also at SwitchKings site I think I once put up a complete sample and description.

Best regards

Re: ClimateDataCalculation

Posted: Fri Nov 07, 2014 9:40 am
by Mastiff
Sounds brilliant. As for disk size, are there limits? :lol: I think I have 6 terabyte in the server, and I would guess at least 3 of them are free (I have my movies on a separate HDPC with another 6 TB). So storage doesn't cost anything anymore. Not like in the late 80's where I finally decided against replacing one of the two diskette drives in my Amstrad PPC512 (my very first portable pc at a time when almost nobody had one, with 10 C-batteries!) since 7000 Norwegian kroner (about 7500 Swedish pesetas... :mrgreen: ) for 8 megabyte was a bit more than I could afford...

So what would be the ETA of the first version of this plug-in?

Re: ClimateDataCalculation

Posted: Fri Nov 07, 2014 9:58 am
by krambriw
:D ETA
Well, working on it right now...maybe something initial next week

Could you provide some EG log samples from your rain sensor?

Re: ClimateDataCalculation

Posted: Fri Nov 07, 2014 10:09 pm
by Mastiff
I can do you one better. :lol: Here's first the rain sensor:

Code: Select all

23:06:23   RFXtrx.Type: PCR800 id: 25856 ' rainrate: 0 mm/hr rainTotal: 34 mm signal: 7 battery: 100%'
And then the wind sensor:

Code: Select all

23:07:37   RFXtrx.Type: WGR800 id: 28928 ' direction: SSE average speed: 2 m/s gust: 1 m/s signal: 6 battery: 10%'
The 10 % battery is constant, I'm using a solar panel with built in rechargable battery, and no matter how much it's charged the sensor only shows 10 %.

Re: ClimateDataCalculation

Posted: Sun Nov 09, 2014 2:37 pm
by krambriw
To publish and automatically update data on a web page, web sockets is an excellent choice. The attached demo.html is a simple example that demonstrates the functionality.
Just edit the ip address and port number in the document and put it in your web server root folder. The picture below shows how it typically looks like when it is properly configured.
demo.zip
(1.38 KiB) Downloaded 242 times
Image2.jpg

Re: ClimateDataCalculation

Posted: Sun Nov 09, 2014 7:24 pm
by Mastiff
I'm afraid you will have to hold my hand here... I have edited the IP and port, and I have installed (I think...) Pako's Websocket plugin. But I can't find the websocket plugin itself in EG and add it to the tree. I'm sure there's something here that I have messed up.

Sorry, moron alert... I had forgotten to configure Winrar correctly, so I had made a subdirectory for the package and then put the plug-ins in there. But I still don't get any output, I get this:

Code: Select all

20:30:35   Server 0.0.0.0:1235 is not running !
20:30:35            Websocket broadcast error...check the websocket configuration 
I have tried both with localhost:1235 and 192.162.2.100 (which is the address to the auomation computer). I'm trying the demo webpage from another computer, not the automation computer. I assume I may have to add something in the Websocket plugin (Server classes), but I really don't know what to put there to make it read my sensors and broadcast that.

A suggestion for the ClimateDataCalculation plug-in: The ability to fire an event if sensor x has had a value equal to, equal to and larger than, larger than, equal to or less then, less than y for z minutes. Is that possible? Is it even understandable? ;)

Re: ClimateDataCalculation

Posted: Sun Nov 09, 2014 8:08 pm
by paalkr
Hi

I'm very excited to see such a plugin for EventGhost being developed. Will Tellstick be supported as a data source as well, and not only RFXtrx443?

I while ago i created some python scripts to calculate various rain statistics inside EventGhost, and forwarded the calculated values to SwitchKing trough their REST API. But the approach taken in this thread is much better than mine though, to just store the "raw" values in a database, and be able to pull data and calculate statistics on the fly. Just make sure that you have good database performance, there can easily be millions of records ;)

The statistics I created from the Oregon PCR800 total rain payload was:
* Accumulated rain today
* Total rain yesterday
* Total rain last 7 days
* Total rain last 30 days
* Total rain last moth

I also created a small script to convert the wind direction into wind sectors, like N, NE, E, SE, S, SW, W, NW

Regards,
PK

Re: ClimateDataCalculation

Posted: Sun Nov 09, 2014 10:14 pm
by krambriw
but I really don't know what to put there
As usual, it would help if you provide screenshots of your settings...

Here are mine and they do work:

1) Since you are running the WebsocketSuite plugin, I assume you have the Webserver plugin added as well? First thing is to drop the demo.html file in the web server root folder. Try to open it like http://your_ip:port_if_not_80/demo.html

2) Check that you have similar settings like below and that the websocket server is running on port 1235 (and listed as such)
Image1.jpg
Image1.jpg (10.4 KiB) Viewed 10710 times
Image2.jpg
Image3.jpg

Re: ClimateDataCalculation

Posted: Sun Nov 09, 2014 10:30 pm
by Mastiff
Got it, thanks! :) I was almost correct, I was only missing the "start server" action. Now I see the stuff coming in. :D So I assume I'm ready to use it for something, but I'm not quite sure what. I guess that will surface with your plugin. :) As for my regular webpage with temperatures and stuff I have actions like this one:

Code: Select all

import time
eg.event.payload_copy = eg.event.payload
my_data = eg.event.payload_copy.split(' ')

td = my_data[2] #temperature

timeStamp = str(
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
)
eg.globals.Stua_time = timeStamp
eg.globals.Stua_temp = td

eg.globals.Stua_time_formatert = "%s:%s, %s.%s." % (eg.globals.Stua_time[11:13], eg.globals.Stua_time[14:16], eg.globals.Stua_time[8:10], eg.globals.Stua_time[5:7])
And then I have the simple webpage at the cabin:
Simpleweb.jpg
And a bit more complex one at home, which shows if there's a heater on with an icon - the one missing the icons haven't had the heater scripts activated since the last reboot of the server:
homeweb.png

Re: ClimateDataCalculation

Posted: Wed Nov 12, 2014 4:36 pm
by Mastiff
Oh, sh... I have been refreshing the bottom post to look for an answer to that! And now I see that you have edited the first post and put up the plug-in! Well, I can tell you that it will be a good testing opportunity here, because from tomorrow and until next week it's supposed to rain so much that I may find that elusive Russian sub in my garden! :lol:

Re: ClimateDataCalculation

Posted: Wed Nov 12, 2014 6:18 pm
by krambriw
:lol: :lol: :lol: :lol:
Thats really a good one! The swedish navy is so incompetent that they would need all the help they can get from our fellow norwegians with fishing boats and nets (maybe someone will knock on our doors very soon if echelon is listening to this...)

Just one thing related to the topic, I am working on the plugin and I cannot guarantee a backward compatibility so it might be that the plugin needs to be re-installed later so please do not built up a very large system based on this yet. See this topic here (btw as you might have seen, the user paalkr from Norway published the scripts finally and he is also in this forum):
http://eventghost.net/forum/viewtopic.php?f=2&t=6388

I am anyway grateful if you can test the plugin 'as is' even if I feel sad for all that rain (it isn't much better here either, I would feel better with a pinacolada in the shadow of a palm tree)

BestR

Re: ClimateDataCalculation

Posted: Thu Nov 13, 2014 9:41 pm
by Mastiff
Sorry it took a bit of time answering that one, mate. Just came back from my interview with the Norwegian Säpo... :roll: But seriously, it seems to work nicely. One question, though: Instead of me messing around for a few hours before giving up and asking you, I'll start in the correct end: Can you please give me the script (sure it's a few lines) that I need to put the rain into three variables, eg.globals.Regn_dag, eg.globals.Regn_uke and eg.globals.Regn_maaned? (Uke = vecka, but you knew that, right?) I would just like to have those variables available on the simple cabin webpage. The one I have now says that it's been 85 mm rain the last day... I originally thought that the second number the rain meter sendt was the last day, not since it was last reset! :oops: