Mobile Alerts and openHAB 2

regex
httpbinding
mobilealerts
Tags: #<Tag:0x00007f0e904b5b90> #<Tag:0x00007f0e904b5a28> #<Tag:0x00007f0e904b58e8>

(André) #1

The goal of this example is to show you how to use different Mobile Alerts readings with openHAB 2.
Since I only have the “non-PRO” devices I can’t use the new Mobile Alerts REST Api. But nevertheless it is still possible to integrate also the normal devices via the website “measurements.mobile-alerts.eu”.
In this tutorial I will show you how to read the temperature, humidity and CO2 values of a Technoline WL 2000 device.

Prerequisites:
If not already done, please install the “HTTP Binding” and the “RegEx Transformation” service via the Paper UI.

Step One:
Add the following lines in http.cfg in the folder services. Please replace the xxx with your phoneid from the Mobile Alerts App

mobileAlerts.url=http://measurements.mobile-alerts.eu/Home/SensorsOverview?phoneid=xxx
mobileAlerts.updateInterval=300000

Because we want to read several parameters out of the returned HTML page I prefer to cache the page and
update it only once every 5 Minutes

The sensor data in the HTML file looks like:

<div class="sensor-component">
    <h5>Zeitpunkt</h5>
    <h4>28.01.2017 19:56:05</h4>
</div>
<div class="sensor-component">
    <h5>Temperature Indoor</h5>
    <h4>22,5 C</h4>
</div>
<div class="sensor-component">
    <h5>Humidity Indoor</h5>
    <h4>43%</h4>
</div>
<div class="sensor-component">
    <h5>Air Quality</h5>
    <h4>950 ppm</h4>
</div>
<div class="sensor-component">
    <h5>Temperature Outdoor</h5>
    <h4>-0,7 C</h4>
</div>
    </div>
</div>

Unfortunatly the temperature data has a “,” instead of a “.” in the float value. We will have to transform that later.

Step Two:

Create a new or edit your existing items file and add the following items:

Number MobileAlerts_AirQuality "Air Quality[%d ppm]" { http="<[mobileAlerts:300000:REGEX(.*?([0-9]+) ppm.*)]" }

String MobileAlerts_Outside_TemperatureString "Outside Temperature [%s]"  { http="<[mobileAlerts:300000:REGEX(.*?Temperature Outdoor.*?\\s.*?(-*[0-9]+,[0-9]+) C.*)]" }
Number MobileAlerts_Outside_Temperature "Outside Temperature [%.1f °C]" 

String MobileAlerts_Indoor_TemperatureString "Indoor Temperature [%s]"  { http="<[mobileAlerts:300000:REGEX(.*?Temperature Indoor.*?\\s.*?(-*[0-9]+,[0-9]+) C.*)]" }
Number MobileAlerts_Indoor_Temperature "Indoor Temperature [%.1f °C]" 

Number MobileAlerts_Indoor_Humidity  "Indoor Humidity [%d %%]" <humidity> (Raumklima, Wohnzimmer) {http="<[mobileAlerts:300000:REGEX(.*?Humidity Indoor.*?\\s.*?([0-9]+)%.*)]"}

Change in the REGEX the names of your readings to you’re defined names in Mobile Alerts. The items MobileAlerts_AirQuality and MobileAlerts_Indoor_Humidity will directly contain values you can work with. For the temperature we first have to save them in a String item.

Step Three:

To convert the “,” to “.” in the temperature readings and save the values as numbers define two rules as follows:

rule "Convert MobileAlerts_Outside_TemperatureString to Float"
when
        Item MobileAlerts_Outside_TemperatureString received update
then
	var Number tempFloatOutside
        tempFloatOutside=Float::parseFloat(String::format("%s",MobileAlerts_Outside_TemperatureString.state).replace(',','.')) 
        MobileAlerts_Outside_Temperature.postUpdate(tempFloatOutside)
end


rule "Convert MobileAlerts_Indoor_TemperatureString to Float"
when
        Item MobileAlerts_Indoor_TemperatureString received update
then
	var Number tempFloatIndoor
        tempFloatOutside=Float::parseFloat(String::format("%s",MobileAlerts_Indoor_TemperatureString.state).replace(',','.')) 
        MobileAlerts_Indoor_Temperature.postUpdate(tempFloatIndoor)
end

That’s it. Now you can work with the four items MobileAlerts_AirQuality, MobileAlerts_Outside_Temperature, MobileAlerts_Indoor_Temperature and MobileAlerts_Indoor_Humidity as with every other item in openHAB.

Regards,

André


(Matthias Luger) #2

Hello,
thank you that a real good dokumentation.
I´m sorry about but I´v 2 problems.
in one of my sensors thereis a “ß” . I could not change this because thats the sensor directly connected to “base” and then the datas are send to the gateway…
I tryed some regex to get “Temperatur Außen” but nothing work…

could you give me a info about, how to get the value?


(Ned Sin) #3

Hey Guys,
i try to Seup this in my OpenHAB 2 Installation.

I only became an Output Like: Aussen Temperatur - °C

Is there any Changes from Mobile Alerts ? Did it still work on your System ?

I`m interessted to know: Did it Global run and the fail is in my system or is this Example / Tutorial out of run ???

Sorry for my bad English, I`m German :wink:

Greetings …


(Daniel Weisser) #4

Hello,
I used a different approach with the Mobile Alerts Homepage.
So I configured installed the HTTP service and the Javascript transformation.

Then the appropriate configuration for HTTP is in http.cfg

sensorCache.url=http://measurements.mobile-alerts.eu/Home/SensorsOverview?phoneid=xxx
sensorCache.updateInterval=60000

Then I configured an item as follows with a Javascript transformation

Number inTemperature "Temperatur innen [%.1f ℃]" { http="<[sensorCache:60000:JS(getTemperature.js)]" }

The Javascript transformation takes the HTML and parses the temperature (for label Temp In)

(function(i) {
    var re = new RegExp('Temp In<\/h5>\\s*<h4>([0-9]+,[0-9]+) C<\/h4>', 'm');
    var out = i.match(re)[1];
    return parseFloat(out.replace(',', '.'));
})(input)

Hope this helps!


(Mark) #5

I wish but it didn’t. :cry:

I have multiple sensors and I can’t figure out the regex to get the data out.

HTML looks like this for one of the sensors

<div class="sensor-header">
    <h3>
        <a href="/Home/MeasurementDetails?deviceid=xxxxxxxxx&amp;vendorid=xxxxx&amp;appbundle=eu.mobile_alerts.mobilealerts">T/H probe - Heating</a>
    </h3>
    <div class="sensor-component">
        <h5>ID</h5>
        <h4>SENSOR ID HERE</h4>
    </div>
</div>

<div class="nofloat"></div>

    <div class="sensor-component">
        <h5>Timestamp</h5>
        <h4>7/18/2018 2:50:25 PM</h4>
    </div>
    <div class="sensor-component">
        <h5>Temperature</h5>
        <h4>21.6 C</h4>
    </div>
    <div class="sensor-component">
        <h5>Temperature Probe</h5>
        <h4>22.4 C</h4>
    </div>
    <div class="sensor-component">
        <h5>Humidity</h5>
        <h4>69%</h4>
    </div>
        </div>
    </div>
</div>

So, I guess I could use the sensor ID to identify the sensor and adapt the regex for every other sensor I have. But… I do not know how to use regex


(Daniel Weisser) #6

Hi Mark,
the following Javascript transformation (and thus RegExp) should work (and in your example deliver the value 21,6:

(function(i) {
    var re = new RegExp('Temperature<\/h5>\\s*<h4>([0-9]+,[0-9]+) C<\/h4>', 'm');
    var out = i.match(re)[1];
    return parseFloat(out.replace(',', '.'));
})(input)

Do you see any errors in your log file?


(Mark) #7

Hi, Thanks for the reply. I think I need a step by step guide. I am using PaperUI. I have installed the Http binding and edited the config. Javascrpit transformation is installed.

Its the next steps and what goes where I am struggling with…I am just a beginner :frowning:

Also, reading the above I don’t see how each different sensors are identified. I have about 10 of various mobile alert types. Rain gauge, Temperature/humidty gauges, pro and non pro, Wind sensor.


(Daniel Weisser) #8

Hi Mark,
then I’ll try too provide a detailed description. As I hardly use PaperUI I’ll stick to the config files.

As a device I do have aPreformatted text TFA Dostmann Wetterstation

After the installation the values can be reached via https://measurements.mobile-alerts.eu/Home/SensorsOverview?phoneid=XXXXX

For the example here I’m using one sensor that reports the temperature and the humidity. So the relevant HTML snippet is:

<div class="nofloat"></div>

    <div class="sensor-component">
        <h5>Zeitpunkt</h5>
        <h4>19.07.2018 09:30:00</h4>
    </div>
    <div class="sensor-component">
        <h5>Temp In</h5>
        <h4>23,8 C</h4>
    </div>
    <div class="sensor-component">
        <h5>Hum In</h5>
        <h4>52%</h4>
    </div>

The labels to watch for are thus Temp In and Hum In.

First I configured a HTTP service that regularly grabs the HTML page (file services/http.cfg):

sensorCache.url=http://measurements.mobile-alerts.eu/Home/SensorsOverview?phoneid=XXX
sensorCache.updateInterval=60000

From this HTTP service (named sensorCache) I’ll fetch two different values from the sensors and assign them to items. The relevant configuration is in an item file (items/weather.items).

Number inTemperature "Temperatur innen [%.1f °C]" { http="<[sensorCache:60000:JS(getTemperature.js)]" }
Number inHumidity "Feuchtigkeit innen [%d %%]" { http="<[sensorCache:60000:JS(getHumidity.js)]" }

In order to get proper values from the HTTP service (and thus the HTML page) the result of the sensorCache HTTP service is read and transformed by a Javascript transformation. And for each sensor there is a different Javascript file.

For the inTemperature the file transform/getTemperature.js is used:

(function(i) {
    var re = new RegExp('Temp In<\/h5>\\s*<h4>([0-9]+,[0-9]+) C<\/h4>', 'm');
    var out = i.match(re)[1];
    return parseFloat(out.replace(',', '.'));
})(input)

So here you’ll see within the RegExp that it looks for Temp In and then takkes the according number and transforms it to a real number for further usage. The same goes for the humidity with the following Javascript transformation (transform/getHumidity.js), where Hum In is used:

(function(i) {
    var re = new RegExp('Hum In<\/h5>\\s*<h4>([0-9]+)%<\/h4>', 'm');
    var out = i.match(re)[1];
    return parseFloat(out);
})(input)

That’s the setup that works for me. And if there are more questions - just go ahead :wink:

Cheers
Daniel


(Mark) #9

Hi Daniel,

That helps a lot. I have not used transform before and didn’t know where the js goes.

I have exactly the same sensor so I will try it exactly as you have it.

Are you using the other probes as well Temp1,2,3?

<div class="sensor-header">
    <h3>
        <a href="/Home/MeasurementDetails?deviceid=device ID HERE&amp;vendorid=xxxxxxx&amp;appbundle=eu.mobile_alerts.mobilealerts">Pantry Garage Porch</a>
    </h3>
    <div class="sensor-component">
        <h5>ID</h5>
        <h4>device ID HERE</h4>
    </div>
</div>

<div class="nofloat"></div>

    <div class="sensor-component">
        <h5>Timestamp</h5>
        <h4>7/18/2018 2:47:30 PM</h4>
    </div>
    <div class="sensor-component">
        <h5>Temp In</h5>
        <h4>24.6 C</h4>
    </div>
    <div class="sensor-component">
        <h5>Hum In</h5>
        <h4>61%</h4>
    </div>
    <div class="sensor-component">
        <h5>Temp 1</h5>
        <h4>20.8 C</h4>
    </div>
    <div class="sensor-component">
        <h5>Hum 1</h5>
        <h4>75%</h4>
    </div>
    <div class="sensor-component">
        <h5>Temp 2</h5>
        <h4>21.4 C</h4>
    </div>
    <div class="sensor-component">
        <h5>Hum 2</h5>
        <h4>64%</h4>
    </div>
    <div class="sensor-component">
        <h5>Temp 3</h5>
        <h4>28.3 C</h4>
    </div>
    <div class="sensor-component">
        <h5>Hum 3</h5>
        <h4>44%</h4>
    </div>
        </div>
    </div>
</div>

My big problem will be when I try to use the other sensors as they all have the same ‘Temperature’ label.

To differentiate between them I would need to identify the id number or sensor name and then assign the temperature values to the appropriate item. As I showed in the html code I posted in my previous post.

The regex looks to be too daunting for me as a beginner; so either I try to learn how to do it somehow or an expert comes along and helps me,and others. Something generic that works for all sensors would be cool using either the device ID or the device name. The format is mainly the same for each.

MFG Mark


(Mark) #10

I am trying to deconstruct your regex using an online tester with your HTML, but its not working

I removed buts to see where its not working and its OK until

\\s*

this is the test for a line break, correct?


(Mark) #11

OK some progess

the test didn’t like

 \\s*

but is OK with

 \s*


(Daniel Weisser) #12

yes, the

\

that is used for escaping is a bit dependent on the programming language and testing tool that’s being used.


(Daniel Weisser) #13

for you the following Javascript transformation should give you the temperature for your sensor

(function(i) {
    var re = new RegExp('Temperature Probe<\/h5>\\s*<h4>([0-9]+,[0-9]+) C<\/h4>', 'm');
    var out = i.match(re)[1];
    return parseFloat(out.replace(',', '.'));
})(input)

as your description is Temperature Probe

and I would suggest to start with the first sensor and when this works add the others.


(Mark) #14

Thanks for the support,

It’s almost working now. I see in the log my item has a temperature value.

2018-07-21 09:28:25.092 [vent.ItemStateChangedEvent] - inTemperature changed from NULL to 23.8

:slight_smile:

I think I have understood the important parts of this useful method for mobile/alerts sensors inclusion into Openhab. Now I just need to figure out how to get in the PaperUI control panel. and add it to the temperature group I have not created used item file before so I will have to do some more reading. Also, need to think how best to adapt the code to get it working for all my sensors.

One thing to note: I didn’t need to transform the ‘,’ to a ‘.’. The js threw an exception when I tried. It turns out my mobile-alerts html is already in the English style for all decimals. I removed from the js the following code

replace(',', '.')

I guess decimal indicator ‘.’ or ‘,’ is set by the mobile-alerts phone app somewhere.

Thanks, Mark


(Mark) #16

I spent hours trying get my multiple mobile-alert sensor data into openhab uing the javascript method from @Daniel_Weisser. I have about 10 Temperature sensors and other sensors too.

First approach was to try to load the multiple temperature sensor data into an array and the use the array index to match to an item for that sensor. That failed. I couldn’t get Regex ‘exec’ and the global flag to work and ‘match’ only finds the first instance of ‘Temperature’

Second approach, and I think its better, was to identify with regex each sensor using its ID and then extract the sensor data. I didn’t know Regex at all when I started. After some hours I figured out what I needed and what works with Openhab2. See below

I post the final js code here in case someone else needs a more generic approach to extract sensor data from the mobile alerts sensor page. It should work with other sensor types by changing the sensor id and the name eg Humidity, Temp In, Temp 1, Hum 2, Rain etc and the regex digits and measurement units if necessary. Create a script for each sensor+ data combination and then assign that to an item as above.

eg

Temperature

(function(i) {
    var re = new RegExp('sensor id goes here[\\s\\S]*?Temperature[\\s\\S]*?<h4>([0-9]+.[0-9]+) C');
    var out = i.match(re)[1];
    return parseFloat(out);
})(input)

Humidty

(function(i) {
    var re = new RegExp('sensor id goes here[\\s\\S]*?Humidity[\\s\\S]*?<h4>([0-9]+)%');
    var out = i.match(re)[1];
    return parseFloat(out);
})(input)

Wind speed

(function(i) {
    var re = new RegExp('sensor id goes here[\\s\\S]*?Windspeed[\\s\\S]*?<h4>([0-9]+.[0-9]+) m\/s');
    var out = i.match(re)[1];
    return parseFloat(out);
})(input)

Wind Gust

(function(i) {
    var re = new RegExp('sensor id goes here[\\s\\S]*?Gust[\\s\\S]*?<h4>([0-9]+.[0-9]+) m\/s');
    var out = i.match(re)[1];
    return parseFloat(out);
})(input)

Rain

(function(i) {
    var re = new RegExp('sensor id goes here[\\s\\S]*?Rain[\\s\\S]*?<h4>([0-9]+.[0-9]+) mm');
    var out = i.match(re)[1];
    return parseFloat(out);
})(input)

ps my mobile-alerts data was already using ‘.’ for the decimal so I didn’t need to replace any ‘,’ german style decimal places

Cheers Mark


(Daniel Weisser) #17

Hi Mark,

thanks for the detailed scripts. As far as I known and after looking at several other discussions in the forum, there is currently no way of handing over a parameter to the script and thus the copying of the script is necessary.

If you have further questions concerning RegExps you may as well send me a direct message.

Cheers
Daniel


InfluxDB+Grafana persistence and graphing
(Mark) #18

Hi,

I took this a step further but with only partial success and I am now stuck. I noticed that for the rain gauge the returned data in the cached web page method described previously is rounded up and this leads to large differences between what the app reports and what I see in Openhab. So, I used the mobile-alerts rest API and it returns with a 3 decimal place accuracy. For rain gauge the bucket tip is equivalent to 0.258mm and this gets rounded to 0.3mm in the web page. When calculating 24hr rainfall etc these rounding errors add up.

http://www.mobile-alerts.eu/index.php/en/

Documenation:
Mobile-alerts REST API

This command works from openhab command line :

curl -d deviceids=xxxxxxx -d phoneid=xxxxxx https://www.data199.com/api/pv1/device/lastmeasurement

or

curl -d 'deviceids=xxxxxxx&phoneid=xxxxxx' https://www.data199.com/api/pv1/device/lastmeasurement

It works also from command line using an sh script and I can output the result to a file if needed

Output is like this:

{
  "phoneid": "xxxxxxx",
  "devices": [
    {
      "deviceid": "xxxxxxxx",
      "lastseen": 1534267127,
      "lowbattery": false,
      "measurement": {
        "idx": 155506,
        "ts": 1534267125,
        "c": 1534267127,
        "t1": 18.6,
        "r": 525.03,
        "rf": 2035
      }
    }
  ],
  "success": true

I tested the REGEX at regex101 and it extracts the data

"r":[\s\S]*?([0-9]+.[0-9]+)

I think I need extra \ escapes for openhab like this:

\"r\":[\\s\\S]*?([0-9]+.[0-9]+)

But I can’t use the REGEX because I think the openhab exec command doesn’t want to work properly. I tried exec binding both as a command and as a Thing and a Run channel item and the result is the same

eg without any regex I get this, If use regex the item value is always null unless its REGEX((.*)) then its the same as the log below

String RainTest "Test" { exec="<[curl@@-d@@'deviceids=xxxxx&phoneid=xxxx'@@https://www.data199.com/api/pv1/device/lastmeasurement:60000]" }

or this if using the exec Thing

String RainTest "Test" { channel="exec:command:58831f93:run" }

In logs I see this and item state shows a string in openhab REST:

2018-08-15 09:58:56.690 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '{'

2018-08-15 09:58:56.692 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '  "phoneid": "xxxxxx",'

2018-08-15 09:58:56.693 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '  "devices": ['

2018-08-15 09:58:56.702 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '    {'

2018-08-15 09:58:56.705 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '      "deviceid": "xxxxxxxx",'

2018-08-15 09:58:56.710 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '      "lastseen": 1534316461,'

2018-08-15 09:58:56.712 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '      "lowbattery": false,'

2018-08-15 09:58:56.714 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '      "measurement": {'

2018-08-15 09:58:56.715 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '        "idx": 155522,'

2018-08-15 09:58:56.717 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '        "ts": 1534273161,'

2018-08-15 09:58:56.719 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '        "c": 1534273164,'

2018-08-15 09:58:56.720 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '        "t1": 17.1,'

2018-08-15 09:58:56.722 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '        "r": 525.804,'

2018-08-15 09:58:56.724 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '        "rf": 2038'

2018-08-15 09:58:56.726 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '      }'

2018-08-15 09:58:56.727 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '    }'

2018-08-15 09:58:56.729 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '  ],'

2018-08-15 09:58:56.731 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '  "success": true'

2018-08-15 09:58:56.732 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: '}'

2018-08-15 09:58:56.736 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current'

2018-08-15 09:58:56.738 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '                                 Dload  Upload   Total   Spent    Left  Speed'

2018-08-15 09:58:56.740 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: ''

2018-08-15 09:58:56.741 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0'

2018-08-15 09:58:56.743 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0'

2018-08-15 09:58:56.744 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [ERROR]: '100   400  100   357  100    43    356     42  0:00:01  0:00:01 --:--:--   356'


Log then shows as below for REGEX((.*)), but for anything else is always null;

Transformed response is

2018-08-15 09:58:56.751 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Transformed response is '{

  "phoneid": "xxxx",

  "devices": [

    {

      "deviceid": "xxxxxxxx",

      "lastseen": 1534316461,

      "lowbattery": false,

      "measurement": {

        "idx": 155522,

        "ts": 1534273161,

        "c": 1534273164,

        "t1": 17.1,

        "r": 525.804,

        "rf": 2038

      }

    }

  ],

  "success": true

}

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

100   400  100   357  100    43    356     42  0:00:01  0:00:01 --:--:--   356

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

100   400  100   357  100    43    356     42  0:00:01  0:00:01 --:--:--   356'

I was hoping to get it to work something like this. That would be neat.

Number MobileAlertsWeatherStationRainTest "MS RainTest [%.3f mm]" <rain> { exec="<[curl -d deviceids=xxxx -d phoneid=xxxx https://www.data199.com/api/pv1/device/lastmeasurement:60000:REGEX(\"r\":[\\s\\S]*?([0-9]+.[0-9]+))]" }

Any help in getting this work would be appreciated. Thanks Mark.


(Mark) #19

@adb76 FYI

I got it to work with the mobile-alerts REST API using following method. There may be neater ways but it works.

The main advantage to using the API data is that the API rainfall data is more accurate than that shown on the web page, which is a rounded total amount. Using the web page data for calculating rainfall rates leads to differences between Openhab charts and the Mobile-Alerts charts. In principal the REST API method should also work with the other ‘Pro’ sensors.

I used EXEC binding with Thing pointing to a shell script with a curl command to extract the JSON data.
File:
var/lib/openhab2/Mobile-Alerts_rain_REST_API.sh

containing:

curl -d deviceids=<yourdevice ID's> -d phoneid=<your phone ID> https://www.data199.com/api/pv1/device/lastmeasurement

In the EXEC Thing config I couldn’t get the regex to to work at all but I did get a JS transform to work instead.

JS(get_RainAPIvalue.js)

with get_RainAPIvalue.js like this:

(function(i) {
    var re = new RegExp('"r":[\\s\\S]*?([0-9]+.[0-9]+)');
    var out = i.match(re)[1];
    return parseFloat(out);
})(input)
  • Created String Item EXECOutputRainAPI to contain the JSON output string linked to the EXEC output channel
  • Created Number Item RainAPIvalue to store the number from the transformed JSON API output
  • Created Rule to transform String Item to the Number Item
rule "Convert Mobile-Alerts rain API string EXEC output result to Number type Item"
//var/lib/openhab2/Mobile-Alerts_rain_REST_API.sh run from EXEC Thing
when
    Item EXECOutputRainAPI changed
then
// post the new value to the Number Item
    RainAPIvalue.sendCommand( EXECOutputRainAPI.state.toString )
    logInfo("Rain" , 'Rain API value '+ RainAPIvalue.state)
end
  • Added the Number Item RainAPIvalue to Influx persist file.
RainAPIvalue  : strategy = everyMinute
  • Created chart in Grafana using ‘mean’ and ‘difference’ transform to show changes to the total rainfall and mm/minute… Left chart is the API total rainfall value left axis and rainfall rate on the right axis. Left chart is just rainfall rate.

image


(Mark) #20

Building on the work I did already… I now have One EXEC thing pulling all the mobile-alerts data using the mobile-alerts REST API. I then use Javascript transforms and Rules to extract each item of the data I need. The relevant Openhab Item is updated only if the new API value is different to the old value. Items are added to my influx persistence list.

Now have Wind gust, speed and direction. Using these I set limits so that I get notifications when there are high winds at home and or if starrted to rain ie… storm warning from my own cheapo weather station. I am happy to share the code… I have Rain, and Wind data using the API method and for the temperature humidity sensors I am using the http cache method… but I plan to move the pro sensors over to API as the preferred method to get the sensor data.

Has nice animation when the needle changes