openhab / openhab-addons

Add-ons for openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.9k stars 3.59k forks source link

[mqtt] permit bracket in `transformationPattern=` after the name of the transformation #15544

Closed dilyanpalauzov closed 1 month ago

dilyanpalauzov commented 1 year ago

According to https://www.openhab.org/docs/configuration/transformations.html to have a transformation one types the name of the transformation and then in brackets the content of the transformations, like

DSL(stringlength.dsl)
DSL(|"String has " + input.length + " characters")

But after transformationPattern= and transformationPatternOut= using bracket after the transformation name is an error:

2023-09-03 12:35:31.510 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'b1.things'
2023-09-03 12:35:31.828 [WARN ] [rnal.handler.GenericMQTTThingHandler] - Configuration error for channel 'mqtt:topic:b1:z'
java.lang.IllegalArgumentException: The transformation pattern must consist of the type and the pattern separated by a colon
        at org.openhab.binding.mqtt.generic.ChannelStateTransformation.<init>(ChannelStateTransformation.java:50) ~[?:?]
        at org.openhab.binding.mqtt.generic.ChannelState.lambda$3(ChannelState.java:122) ~[?:?]        
        at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) ~[?:?]          
        at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179) ~[?:?]
        at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:992) ~[?:?]
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[?:?]
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[?:?]
        at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) ~[?:?]
        at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) ~[?:?]
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:?]
        at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596) ~[?:?]
        at org.openhab.binding.mqtt.generic.ChannelState.addTransformation(ChannelState.java:100) ~[?:?]
        at org.openhab.binding.mqtt.generic.internal.handler.GenericMQTTThingHandler.createChannelState(GenericMQTTThingHandler.java:135) ~[?:?]                                                                                                                                
        at org.openhab.binding.mqtt.generic.internal.handler.GenericMQTTThingHandler.initialize(GenericMQTTThingHandler.java:190) ~[?:?]
        at org.openhab.core.thing.binding.BaseThingHandler.thingUpdated(BaseThingHandler.java:155) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]            
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]                                                                      
        at org.openhab.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:147) ~[?:?]
        at org.openhab.core.internal.common.Invocation.call(Invocation.java:52) ~[?:?]
        at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
        at java.lang.Thread.run(Thread.java:833) ~[?:?]

So the .thing file has to use colon instead of bracket, e.g. transformationPattern="REGEX:(.*POWER.*)". This is implied by the examples at https://www.openhab.org/addons/bindings/mqtt.generic/ .

The problem report is that the spelling of the transformation formulae in openHAB is inconsistent: at some places one has to use brackets DSL(…), on others colon DSL: . This inconsistency is source of errors.

I use openHAB 4, but with OpenHAB 3.4 this discrepancy was also present.

lsiepel commented 2 months ago

Recently some PR’s were merged. The documentation has been adapted. Both ways should be possible, could you re-test with 4.3.0 M1 ?

dilyanpalauzov commented 1 month ago

I switched to OpenHAB 4.3.0.M2 and in one .thing file changed

Type number :termo "T" [stateTopic="a/b", transformationPattern="JSONPATH:$.AM2301.Temperature", unit="°C"]

with

Type number :termo "T" [stateTopic="a/b", transformationPattern="JSONPATH($.AM2301.Temperature)", unit="°C"]

It works!

dilyanpalauzov commented 1 month ago

The documentation has been adapted.

How is the documentation supposed to spell the delimiter between transformation name and formula? https://next.openhab.org/docs/configuration/transformations.html uses brackets, https://next.openhab.org/docs/configuration/transformations.html uses colon.

lsiepel commented 1 month ago

The documentation has been adapted.

How is the documentation supposed to spell the delimiter between transformation name and formula? https://next.openhab.org/docs/configuration/transformations.html uses brackets, https://next.openhab.org/docs/configuration/transformations.html uses colon.

Not sure if i underrstand your question, as the links lead to the page, not to a specific paragraph. Both syntax are supported. Maybe @jimtng added a PR to make the delimiter configurable. Please raise a new issue if you think this should be further adapted.

jimtng commented 1 month ago

The syntax is not configurable.

https://next.openhab.org/docs/configuration/transformations.html uses colon.

Can you copy paste or take a screenshot of where it uses colon?

dilyanpalauzov commented 1 month ago

The documentation has been adapted.

I tried to find this adaptation, but could not. I also do not see where it is spelled, that both syntax can be used. (Yes, both forms can be used!, but this is not articulated.)

Not sure if I underrstand your question, as the links lead to the page, not to a specific paragraph.

At https://next.openhab.org/docs/configuration/transformations.html I mean e.g. this line in the Usage section:

Number Kitchen_Temperature_C "Temperature [JSONPATH($.temperature):%s °C]" {/Some Binding/}

and the other link was supposed to be https://next.openhab.org/docs/configuration/things.html in Defining Bridges Using Files section with:

  Type string : Version [stateTopic="stat/sonoff_dual/STATUS2", transformationPattern="JSONPATH:$.StatusFWR.Version"]