How can I escape percent (%) in transformationpattern?

Hi!

I’m trying to get the RSSI value from a MQTT topic but I don’t know how to escape the % sign.

Current configuration:

mqtt value:

{"StatusWifi": {"RSSI(%)": "80" }}

things file:

Type number 	: signal 	"Signal Strenght"	[ stateTopic = "stat/Bath/STATUS",		transformationPattern = "JSONPATH:$.StatusWifi.RSSI(%)" ]

the error that I’m getting:
2024-07-19 10:17:39.838 [WARN ] [t.generic.ChannelStateTransformation] - Executing the JSONPATH-transformation failed: An error occurred while transforming JSON expression.

I did some searches on Google but I was unable to find something OH specific. Do you guys have any idea on how to escape the % sign?

Thx!

Try to escape with :

Type number : signal "Signal Strenght" [ stateTopic="stat/Bath/STATUS", transformationPattern="JSONPATH:$.StatusWifi.RSSI(\%)" ]

Thx! Tried already and I’m getting:

2024-07-19 10:29:21.349 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘ClimSen.things’ has errors, therefore ignoring it: [5,103]: mismatched character ‘%’ expecting set null
[5,133]: no viable alternative at input ‘)’
[9,36]: mismatched input ‘Climate’ expecting ‘]’
[9,51]: missing EOF at ‘-’
[59,134]: mismatched character ‘’ expecting ‘"’

Did you try ‘%%’ with two % characters?

Yes, I did. Still not working, Andrew:

2024-07-19 15:35:52.875 [WARN ] [t.generic.ChannelStateTransformation] - Executing the JSONPATH-transformation failed: An error occurred while transforming JSON expression.

If you have control over what’s publishing this eliminating the (%) from the key would probably be the easiest. While technically allowed by the spec, lots of parsers will have problems dealing with special characters like that in the key.

Since this is in a .things file, you might need to double escape the percent and the ( and ).

First try single escaping them all:

$.StatusWifi.RSSI\(\%\)

If that doesn’t work try double escaping:

$.StatusWifi.RSSI\\(\\%\\)

Sometimes a double is escape is required because the first escape is consumed by the file reader/parser and the second one gets consumed by the JSONPATH itself. With only a single escape, the file parser consumes it and an unescaped string goes to the transform.

In the UI these sorts of complications are handled so you only ever single escape in the UI.

It’s going to be a challenging evening as indeed I can remove that percentage, but first, I need to get the code to compile again. The last time I flashed that Arduino device was three years ago and now I need to reinstall all the libraries in order to make it compile and work again.

Thx, Rich! I have tried both methods and none worked. :slight_smile:

Did you try:

$.StatusWifi['RSSI(%)']

JSONpath should accept arbitrary keys as strings like that.

3 Likes

This worked!

2024-07-19 19:11:40.613 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘LCDBath_Bath_Arduino_signal’ changed from -57 to 82

Thx a lot!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.