How to transform string from regex in http binding to number

  • openHAB version: 3.2

I try to read out a (number) value from a website via http binding. The response is only in html/xml available.
With the regex add-on I’m able to extract the desired value as a string.
However, now I’m stuck as how to convert this value into a number. I think I would like to do it by concatenating another state transformation as the cleanest solution, but can’t find how to do it. Most examples use json.
Another way should be to link additional items performing the transformation? But I’m not sure if this is a good solution, and where this is best implemented.
(Additional difficulty: The value string is “german”, the decimal delimiter is “,” and not “.”)

Snippet from the html request:

  <tr class="odd">
    <td class="key">VORLAUFTEMP.</td>
    <td class="value">26,9°C</td>
  </tr>
  <tr class="even">

Binding configuration:

UID: http:url:de9551bc18
label: ServiceWelt
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: false
  baseURL: http://servicewelt/?s=1,0
  delay: 0
  stateMethod: GET
  refresh: 20
  commandMethod: GET
  timeout: 3000
  bufferSize: 2048
channels:
  - id: Test
    channelTypeUID: http:string
    label: Vorlauftemperatur
    description: ""
    configuration:
      mode: READONLY
      stateTransformation: regex:.*VORLAUFTEMP\.\<\/td>\n    \<td class="value">(.*?)\°C.*

Okay.

Change to number type

You’re already using REGEX, which can replace that for parsing.

When concatenating transformations, it doesn’t matter what the transforms are. They all work the same.

But assuming you’ve extracted a parsable number from the HTML, why not just change the Channel type to a number and let OH worry about parsing the string? Or is the number not parsable and you need to do further modifications to it to make it parsable?

1 Like

Changing it to number parses it without the “,”. From 27,9 it becomes 279.

Regarding the regex: From the documentation I understand that I can substitute the regex by the result.

s/<regex>/<substitution>/g

In my case, I tried

      stateTransformation: regex:s/.*VORLAUFTEMP\.\<\/td>\n    \<td
        class="value">(.*?),(.*?)\°C.*/$1\.$2/g

But what that does is, substitute the matched regex
in this case

    <td class="key">VORLAUFTEMP.</td>
    <td class="value">26,9°C</td>

with

26.9

which leads to

  <tr class="odd">
26.9
  </tr>
  <tr class="even">

In that case I’m stripping the key “Vorlauftemp” and can not find the number anymore (there are more keys in the response)

How to strip away everything else than the capture groups and the dot?

Your REGEX has two matching groups. Only the first one gets returned. Regex doesn’t work quite like the standard in OH in that regard.

I would probably be easier to use a simple JS transform after the REGEX to replace the , with .. As of OH 3.2, JS supports “inline” configs and since 3.2. So if you used the regex to extract the number with the comma then you should be able to chain that to JS:|input.replace(',', '.')

regex:.*VORLAUFTEMP\..*value">(.*)\°C.*∩JS:|input.replace(',', '.')

I simplified the REGEX a tad to make it shorter and clearer. The first (.*) is the only thing that gets returned. If nothing matches nothing gets returned I think (though that might just be the MQTT binding).

Ok… now I’m sure I’m correct to ask in the beginners section.
When running your rule I get

Transformation service JS for pattern |input.replace(',', '.') not found!

Also with running only

JS:|input.replace(',', '.')

without the regex I get the error.

I installed the JSScripting Add-on. Am I missing something else?

Thank you all for your help, I found a working solution:

      stateTransformation: regex:.*VORLAUFTEMP\.\<\/td>\n    \<td
        class="value">(.*?)\°C.*∩regex:s/(\d+),(\d+).*/$1\.$2/g

However, your shortened regex works as well, at least for the key “VORLAUFTEMP” but not with “RÜCKLAUFTEMP”, which is another key. My version works with both… I’ll have a look into this later.
But thanks every one for setting me up

You are running 3.2 for sure? Based on the docs for the JS transform, that’s how to define an inline JS transform (using the | to tell it you’ve not passed it a file. There might be something going on with the way the HTTP binding interprets the string.

Yeah…at least that’s what my openhab tells me :wink:

“openHAB 3.2.0 - Release Build”

Yes. JS scripting has nothing to do with JS transformation, completely separate add-on.
The message speaks the truth

1 Like

You’re right, there seems to be a difference. Now it works also with JS transformation.
Unfortunately, a search for JS in the UI only finds “JSScripting”. You have to manually look for it in the “other” category…