State formatter for REGEX

Hi,

I am struggling with something that should be so obvious.

I have configured http biding to scrap webpage to read data into String Channel.

Then Channel extracts value via REGEX into Item Number:Length. This all works. Now units of measurement come to play.

My weather station shows values in rain values in mm (e.g. 54.2mm), but it ends up transformed into (54.2m).

So I try to define State Formatter, to say that I need to read this value as float with two decimals and unit of measurement is “mm”. I guess that should be %.2f mm
It throws error (only %s works).

2022-04-12 13:02:53.432 [WARN ] [.profiles.RegexTransformationProfile] - Could not transform state '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<...>
' with function '.*?rainofyearly.*?value=\"(.*?)\".*' and format '%.2f mm'

Please help.

Firstly, I don’t think that the state formatter does what you think it will do. It will not divide the number by 100 to convert mm to m. All it will do is, if there are two or more decimal places in the number already it will round to the nearest hundredths. That’s not what your problem is.

Secondly, pay attention. to the description under the State Formatter. That formats the value before the transformation. It makes no sense to use %.2f mm with a whole web page. That’s why only %s works. That field is trying to format the full web page. Your REGEX hasn’t extracted the number yet.

I don’t use this binding but it’s very similar to the MQTT binding. Looking at the docs, a number type Channel will have a unit field. That’s where you would put mm to tell the binding what units this number is in.

Alternatively you can apply a State Description pattern and tell OH that by default the value should be treated as mm instead of m.

1 Like

Dear Rich,

I checked, http binding is clearly less sophisticated than mqtt, so it does not have “unit” field.

Could you elaborate with few words on State Description patter? I gather, that if I put “%.2f mm” in pattern, it just displays 52.4 metres in milimetres. while I wanted the item to be 52.4mm value

The docs says it does.

Did you create a number type Channel?

1 Like

This post covers the same issues

Thanks Rich,

you are absolutely right, I reconfigured and it works like charm,

My problem was that I originally made a string channel and made transfomation while linking to item.

What I had to do and what works, to implement transformtion in channel (making number channel and putting unit there). And then linking does no transformation.

Super, thanks,

rimantas