String Channel to Number - RegEX replace

I have a String item

String Corona_Incidence "Incidence 7d [%s]" <line> (gCorona) [ "Corona","Measurement" ] {channel="http:url:inc:Channel_Corona_Incidence", expire="12h"}

the String contains a number but with a comma instead of a decimal point , e.g. “12,2”
Is there a way to use the channel with a number item and transform the string to “12.2” ?

Are you using OH3?
This method won’t work in OH2 - use a transform profile on the Item-channel link to substitute “.” for “,”, probably a REGEX. See if it works on a String, then use on Number type Item. Add units if you like for a Number:Quantity type.

I did what you suggested.

String Corona_Incidence " Incidence 7d [REGEX(s/,/\\./g):%s]" (gCorona) [ “Corona”,“Measurement” ] {channel=“http:url:in:Channel_Corona_Incidence”, expire=“12h”}

did indeed replace the “,” with a “.” but that seems for display purposes only.

As soon as I use a Number item and %.1f it just displays a “-” in the BasicUI
Error message in the log is:

Failed transforming the state ‘NULL’ on item ‘Corona_Incidence’ with pattern ‘REGEX(s/,/\./g):%.1f’: Cannot format state ‘NULL’ to format ‘%.1f’

Yes, that is exactly what the state presentation part in the [ ] is for. Giving a modified display without altering the real Item state.

This is neither a profile nor a channel based transformation.

I suggested either using a transform profile

which in OH3 will allow you to marry a channel to a different Item type

or

use a channel transformation in your binding setup

again that is for OH3 only

Thanks. I tried that but so far no luck.
OH Regex behaves very special. I do manage to get get the number from the html page but replace does not work. At least not as it is described in the link above.

my thing is


Thing http:url:covid "CoVid-19"   [baseURL="https://www.someurl.de/start/rathaus+und+service/",   refresh="600", timeout="3000"]   {
    Channels:
         Type string : Channel_Corona_Incidence    "Incidence 7d [%s]"         [ stateExtension="corona+7-tage-inzidenz.html", stateTransformation="REGEX:.*?<br>Stadt  ([+-]?([0-9]*[,])?[0-9]+)?.*"]
}

but that gives me e.g. 123,44

According tohttps://www.openhab.org/addons/transformations/regex/, I should use a format s/<regex>/result/g which would be

Type string : Channel_Corona_Incidence    "Incidence 7d [%s]"         [ stateExtension="corona+7-tage-inzidenz.html", stateTransformation="REGEX:/s.*?<br>Stadt  [+-]?([0-9]*[,])?([0-9]+)?.*/$1.$2/"]

but that is not doing it.
And the profiles: I personally don’t find them useful and I don’t see which of these could help me.

Yes, it is “greedy” and you have to match the whole string, which usually involves * at either end…

I don’t know much about regex at all, I don’t know if you can construct one expression to both select your field and replace the comma.
But you are allowed to chain transforms in HTTP binding -

so you can do it two steps with magic character ∩ like

stateTransformation="REGEX:.*?<br>Stadt  ([+-]?([0-9]*[,])?[0-9]+)?.*"∩"REGEX:s/,/\\./g"

Maybe you missed the generic transform profile, which can be used with REGEX.
The usage is described in the doc you reference,

for example you could apply a profile to your Item channel link like

[profile="transform:REGEX", function="s/,/\\./g"]

to a link between a string type channel and a Number type Item, and convert 1,23 from the binding into 1.23 for the Item state.

Thanks, I was actually missing the RegEx transformation. Although

...[profile="transform:REGEX", function="s/,/\\./g"]}

as item transformation seems pretty straight forward, it is not working on my end. Neither does it work when I use ∩ to add a second RegEx to the channel. It seems like I miss some crucial detail when it comes to the replace function.
Do you know if I need to place any parenthesis () like it is required for the RegEx capture ? Or match the whole string ? Not that I have not tried that…

I looked here: RegEx - Transformation Services - openHAB 2 - Empowering the Smart Home
and here RegEx - Transformation Services | openHAB
and these manuals are pretty clear about /s/find/replace/g so I start wondering if that is a bug. :pleading_face:

The very first question I asked was

It does matter here.

Link temporarily to a string Item and see what it is doing.

You used the REGEX expression before in the state presentation mode, and it did what you wanted then.

Yes I use OH3 and it already is a string. And yes, you’re right. In the state representation it works. Good point.

Okay, so if you have already linked your profile to a String type Item, the resultant state is … ?

String Corona_Incidence "incidence 7d [%s]" <line> (gCorona) [ "Corona","Measurement" ] {channel="http:url:inc:Channel_Corona_Incidence" [profile="transform:REGEX", function="s/,/\\./"] }

gives 123,4 as result

String Corona_Incidence "incidence 7d [%s]" <line> (gCorona) [ "Corona","Measurement" ] {channel="http:url:inc:Channel_Corona_Incidence"}

gives 123,4 as result

The channel itself updates nicely, if I change it so does the item state.
It seems like the regex transform is just ignored.

There’s some glitch where editing profiles does not take immediate effect. Seeing as you have been editing, try to rule this out.
Create a brand new String Item, link to same http channel with profile.

1 Like

That worked. Thanks.
so wrapping up, to fetch a comma separated float from a webpage, you would use as thing a string with this regex:

Thing http:url:covid "CoVid-19"   [baseURL="https://www.someurl.de/start/rathaus+und+service/",   refresh="600", timeout="3000"]   {
    Channels:
         Type string : Channel_Corona_Incidence    "Incidence 7d [%s]"         [ stateExtension="corona+7-tage-inzidenz.html", stateTransformation="REGEX:.*?starttag([+-]?([0-9]*[,])?[0-9]+)?.*"]
}

and then swap out the comma (123,4 → 123.4) with another regex while asserting it to a number item:

Number    Corona_Incidence      "incidence 7d [%s]"     <line>     (gCorona)    [ "Corona","Measurement" ]   {channel="http:url:covid:Channel_Corona_Incidence" [profile="transform:REGEX", function="s/,/\\./"] }

AND: use a fresh item now and again. I found that deleting the item and recreating it whith the same name worked too.

Note that ‘caching’ of profiles postponing immediate effects of edits should be fixed in >OH3.0

Thing edits can be more or less dynamic, very much depending on each binding. Small edits to polling services (like http) may be especially problematic. General advice is to restart the binding bundle in case of a Thing edit apparently being ignored.