Ok, so I wrote a little test Rule to try out what the REGEX is doing behind the scenes.
The example looks like this, it’s performing the same basic REGEX
Transform, but via a Rule instead of being attached directly to an Item.
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
rule "REGEX Test"
when
Time cron "0/10 * * * * ?"
then
val VAL1 = 'aTBTMPA21.25'
val VAL2 = 'aTATMPA21.06'
logInfo("regex", 'VAL1: ' + transform("REGEX", "aTBTMPA(.+)", VAL1))
logInfo("regex", 'VAL2: ' + transform("REGEX", "aTBTMPA(.+)", VAL2))
end
and the DEBUG-enabled output in openhab.log
is:
10:13:00.425 DEBUG o.o.m.r.i.e.ExecuteRuleJob[:53]- Executing scheduled rule 'REGEX Test'
10:13:00.429 DEBUG o.o.c.t.i.s.RegExTransformationService[:42]- about to transform 'aTBTMPA21.25' by the function 'aTBTMPA(.+)'
10:13:00.432 INFO org.openhab.model.script.regex[:53]- VAL1: 21.25
10:13:00.436 DEBUG o.o.c.t.i.s.RegExTransformationService[:42]- about to transform 'aTATMPA21.06' by the function 'aTBTMPA(.+)'
10:13:00.438 DEBUG o.o.c.t.i.s.RegExTransformationService[:48]- the given regex '^aTBTMPA(.+)$' doesn't match the given content 'aTATMPA21.06' -> couldn't compute transformation
10:13:00.440 INFO org.openhab.model.script.regex[:53]- VAL2: aTATMPA21.06
'now it becomes a little clearer… so I read the source code, so the observed behavior now makes a lot more sense.
Basically, if the REGEX
Pattern doesn’t match anything form the incoming data, the return the original source string.
In your case, the pattern matches one of the values it’s presented, so it correctly extracts the Temperature. In the other case, it matches nothing, so the original/unmodified string is returned by the transform.
Probably not the behavior you’re looking for…
Technically, because you’ve declared your temp1
and temp2
Items as Number
, the values shouldn’t make it much past the Transform, but your events.log
will be full of crud, and your openhab.log
probably has a bunch of failed conversion errors type in it when attempts to convert the bad String
values to Number
values occur.
If you want more control, you can write a Rule to do it. In this case, have a new Item that’s bound to the MQTT State, and then then parse it apart in a Rule instead (using conditional calls to place the value into the correct Item once parsed out)