Rule for reading TFA WeatherHub / Mobile Alerts values

Hi community!
I have created a rule to read the values from my TFA WeatherHub sensors into openHAB3.

This rule is run every 7 minutes and executes the script below. The values are only updated every 7 minutes by the TFA WeatherHub.

This script needs to be adapted with:

  • your “Phone ID” from the WeatherHub App
  • the openHAB item names of your manually created items to store the values
  • the sensor names of your connected sensors (which you can manually edit in the app)

For me, this code worked stable up to now, but I am happy to get improvements from you.

I would recommend to test the regex results as indicated in the code.
You do not need to install a binding for this.

Greetings, Karsten

Code:

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.WeatherHub');
var client = java.net.http.HttpClient.newBuilder().build();

// Enter uri with your phoneid, can be found in the settings of the TFA WeatherHub App
var uri = "https://measurements.mobile-alerts.eu/Home/SensorsOverview?phoneid=000111222333";

// get data from mobile-alerts
var request = java.net.http.HttpRequest.newBuilder()
  .GET()
  .uri(java.net.URI.create(uri))
  .header("Content-Type", "text/html")
  .build();
var htmlResponseBody = client.send(request, java.net.http.HttpResponse.BodyHandlers.ofString()).body();

// function to extract and format data
this.getMeasurement = function(regExPattern, itemName) 
{ 
  var regEx1 = new RegExp(regExPattern, 'm');
  var regRes = regEx1.exec(htmlResponseBody);
  var measurement = null;
  if((regRes != null) && (regRes[1] != null)) 
  {
    measurement = regRes[1]
    // Remove units
    measurement = measurement.replace(" C", "").replace("%", "").replace(",", ".")
  }
  // replace unavailable values with null
  if(measurement == "---") {measurement = null}
 
  // Log and save measurements
  logger.info("Measurement for item \"" + itemName + "\" is: " + measurement);
  if (measurement != null) 
  {
    // normal values sent as command
    events.sendCommand(itemName, measurement);
  }
  else
  {
    // null values cannot be commands
    events.postUpdate(itemName, NULL);
  }
 
}

// Names must be adapted to your sensor names and language
// regex Expressions can be tested at https://regex101.com/
// typical regular expression syntyx is: "Location[\\s\\S]Sensorname[\\s\\S]*?<h4>(.+)<\/h4>"

this.getMeasurement("ZuHause[\\s\\S]*?Temp In[\\s\\S]*?<h4>(.+)<\/h4>", "itTemperaturBad");
this.getMeasurement("ZuHause[\\s\\S]*?Temp 1[\\s\\S]*?<h4>(.+)<\/h4>", "itWeatherHubTemp1");
this.getMeasurement("ZuHause[\\s\\S]*?Temp 2[\\s\\S]*?<h4>(.+)<\/h4>", "itWeatherHubTemp2");
this.getMeasurement("ZuHause[\\s\\S]*?Temp 3[\\s\\S]*?<h4>(.+)<\/h4>", "itWeatherHubTemp3");
this.getMeasurement("ZuHause[\\s\\S]*?Hum In[\\s\\S]*?<h4>(.+)<\/h4>", "itHumidityBad");
this.getMeasurement("ZuHause[\\s\\S]*?Hum 1[\\s\\S]*?<h4>(.+)<\/h4>", "itWeatherHubHum1");
this.getMeasurement("ZuHause[\\s\\S]*?Hum 2[\\s\\S]*?<h4>(.+)<\/h4>", "itWeatherHubHum2");
this.getMeasurement("ZuHause[\\s\\S]*?Hum 3[\\s\\S]*?<h4>(.+)<\/h4>", "itWeatherHubHum3");
this.getMeasurement("ZuHause[\\s\\S]*?Zeitpunkt[\\s\\S]*?<h4>(.+)<\/h4>", "itWeatherHubTime1");
this.getMeasurement("MitWassersensor[\\s\\S]*?Zeitpunkt[\\s\\S]*?<h4>(.+)<\/h4>", "itWeatherHubTime2");
//this.getMeasurement("MitWassersensor[\\s\\S]*?Temperatur[\\s\\S]*?<h4>(.+)<\/h4>", "itWeatherHubTempS1");
//this.getMeasurement("MitWassersensor[\\s\\S]*?Luftfeuchte[\\s\\S]*?<h4>(.+)<\/h4>", "itWeatherHubHumS1");
this.getMeasurement("MitWassersensor[\\s\\S]*?Wassersensor[\\s\\S]*?<h4>(.+)<\/h4>", "itWeatherHubWassersensor");
3 Likes

Hi,

I´ve got a question, don’t know if you’re seeing my reply :smiley:
What if I want to access to a specific sensor? The uri would look different then. Would it be possible to justify just the uri and it would work?
And my last question is about the syntax for the .getMeasurement. You’ve wrote that the syntax is Location / Sensorname but in your example what is:
“MitWassersensor[\s\S]?Wassersensor[\s\S]?

(.+)</h4>”, “itWeatherHubWassersensor”
“MitWassersensor” and “itWeatherHubWassersensor”? I guess that “wassersensor” is the Sensorname but what are the two other names?

Thanks already!