OH3: HTTP-Binding RegExp / JS

Hello,

in OH2.5 I used the HTTP-Binding to get the values from a website:

Http.config OH2.5:

sensorCache.url=http://measurements.mobile-alerts.eu/Home/SensorsOverview?phoneid=xxxxxxxx
sensorCache.updateInterval=12000

Item OH2.5:

Number ArbeitTemp “Arbeitszimmer [%.1f °C]” { http=“<[mobilealerts:60000:JS(getTempArbeit.js)]” }

JS Datei:

(function(i) {
    var re = new RegExp('120DF8FF7516[\\s\\S]*?Temperatur[\\s\\S]*?<h4>([0-9]+,[0-9]+) C<\/h4>', 'm');
    var out = i.match(re)[1];
    return parseFloat(out.replace(',', '.'));
})(input)

After several hours and various attempts to get this to work in OH3 with the new HTTP binding and the GUI, I have to ask here if someone can give me a hint how I can get this to work in OH3.

KR
Michael

Did you install the new http binding and did you add a thing there? Add a number channel, then put the JS(xxx) in the stateTransformation input field.

Hi Jan,

I have tried it as you describe. Unfortunately without success:

Code:

UID: http:url:bf05b1d758
label: MobileAlerts
thingTypeUID: http:url
configuration:
authMode: BASIC
ignoreSSLErrors: false
baseURL: http://measurements.mobile-alerts.eu/Home/SensorsOverview?phoneid=xxxxxxxxxx
refresh: 60
commandMethod: GET
timeout: 12000
bufferSize: 2048
channels:

  • id: Test
    channelTypeUID: http:number
    label: Test
    description: “”
    configuration:
    stateTransformation: JS(getTempArbeit.js)

Error in openhab.log:

2020-12-23 16:46:48.122 [ERROR] [nal.common.AbstractInvocationHandler] - An error occurred while calling method ‘ThingHandler.thingUpdated()’ on ‘org.openhab.binding.http.internal.HttpThingHandler@7879c816’: The transformation pattern must consist of the type and the pattern separated by a colon
java.lang.IllegalArgumentException: The transformation pattern must consist of the type and the pattern separated by a colon
at org.openhab.binding.http.internal.transform.CascadedValueTransformationImpl.lambda$1(CascadedValueTransformationImpl.java:38) ~[?:?]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) ~[?:?]
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177) ~[?:?]
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[?:?]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[?:?]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[?:?]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[?:?]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:?]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[?:?]
at org.openhab.binding.http.internal.transform.CascadedValueTransformationImpl.(CascadedValueTransformationImpl.java:39) ~[?:?]
at org.openhab.binding.http.internal.HttpHandlerFactory.getValueTransformation(HttpHandlerFactory.java:107) ~[?:?]
at org.openhab.binding.http.internal.HttpThingHandler.createItemConverter(HttpThingHandler.java:357) ~[?:?]
at org.openhab.binding.http.internal.HttpThingHandler.createGenericItemConverter(HttpThingHandler.java:365) ~[?:?]
at org.openhab.binding.http.internal.HttpThingHandler.createChannel(HttpThingHandler.java:247) ~[?:?]
at java.util.ArrayList.forEach(ArrayList.java:1541) ~[?:?]
at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1085) ~[?:?]
at org.openhab.binding.http.internal.HttpThingHandler.initialize(HttpThingHandler.java:181) ~[?:?]
at org.openhab.core.thing.binding.BaseThingHandler.thingUpdated(BaseThingHandler.java:152) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
at org.openhab.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:154) [bundleFile:?]
at org.openhab.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
at java.lang.Thread.run(Thread.java:834) [?:?]

Anything I did wrong?

Michael

Hello Jan,

I found my mistake:

wrong:
JS(getTempArbeit.js)

correct:
JS:getTempArbeit.js

UID: http:url:bf05b1d758
label: MobileAlerts
thingTypeUID: http:url
configuration:
authMode: BASIC
ignoreSSLErrors: false
baseURL: http://measurements.mobile-alerts.eu/Home/SensorsOverview?phoneid=xxxxxxx
refresh: 60
commandMethod: GET
timeout: 12000
bufferSize: 2048
channels:

  • id: Test
    channelTypeUID: http:number
    label: Test
    description: “”
    configuration:
    stateTransformation: JS:getTempArbeit.js

thanks for your help!

1 Like

Hi, i run into these problems too, migrating from OH2.5 to OH3
Where do I have to store the transformation JS ? what is the full path to this directory (on a raspberrypi) ?
So if I a have a JS in my openhab 2.5 containing the following code and named badezimmertemp.js

(function(i) {
    var re = new RegExp('120DF8FF7516[\\s\\S]*?Temperatur[\\s\\S]*?<h4>([0-9]+,[0-9]+) C<\/h4>', 'm');
    var out = i.match(re)[1];
    return parseFloat(out.replace(',', '.'));
})(input)

I have to create a channel with channelType ‘NumberChannel’ and in State Transformation i just add JS:badezimmertemp.js

Is this correct ?

And where do I format the result ?

Thanks for any help

I tried to change the content of State Transformation with
JS:getBuedZemmerTemperature.js. (the js is stored in /etc/openhab/transform/)

and now I get the following errors :

2021-01-11 18:06:52.920 [INFO ] [nding.http.internal.HttpThingHandler] - Using the secure client for thing 'http:url:mobileAlerts'.
2021-01-11 18:06:52.922 [ERROR] [nal.common.AbstractInvocationHandler] - An error occurred while calling method 'ThingHandler.thingUpdated()' on 'org.openhab.binding.http.internal.HttpThingHandler@2561e5': The transformation pattern must consist of the type and the pattern separated by a colon
 java.lang.IllegalArgumentException: The transformation pattern must consist of the type and the pattern separated by a colon

Can any one help me in solving this, I am not a Java programmer, but a simple user.
Basoiically, I want to display the value returned in a dashboard as a temperature

Regards
Marc

That’s the way I implemented mobilealerts in OH3:

  1. Create thing from http binding

  2. Create a channel and set state transformation

  3. Create a new item linked to above channel

  4. getIDTemperature.js

(function(i) { 
    var re = new RegExp('xxxxxxxxxxx[\\s\\S]*?Temperatur Innen[\\s\\S]*?<h4>([0-9]+.[0-9]+) C');
    var out = i.match(re)[1]; 
    return parseFloat(out.replace(',', '.')); 
})(input)

Hope this helps some of you who are still struggeling with this in OH3…

Hey,
thanks. Real geat und easy. Every sensor works. :slight_smile:

Hey,
did you ever try to get battery status of mobile alerts?

I don’t think that battery status is reported to the gateway in Mobile Alerts.
At least I cannot find any such info, neither in the mobile alerts app nor on the web page.

I have allways an error with my rain.
Tis is my getrain.js:

(function(i) {
    var re = new RegExp('08112E6CE182[\\s\\S]*?Regen[\\s\\S]*?<h4>([0-9]+.[0-9]+) mm');
    var out = i.match(re)[1];
    return parseFloat(out.replace(',', '.'));
})(input)

and this is the error at the log:

[.transform.SingleValueTransformation] - Executing transformation ChannelStateTransformation{pattern=‘getrain.js’, serviceName=‘JS’} failed: An error occurred while executing script. TypeError: Cannot get property “1” of null in at line number 3

How can I fix it?

Thanks.

If the regex doesn’t match the input string, i.match(re) returns null and you can’t get element 1 from null.

Thanks. But how do I have To change the .ja file?

Since I don‘t know the raw data, it‘s impossible to say what the regex should look like.

OK und where can I find the raw data?

You could link the channel to a string item (without transformation).

Hello everbody,
since wendsday I have a problem:
The log shows:


But thing shows:

Any idea?
it works fine until wendsday…

Probably not an openHAB problem: looks like you’re no longer allowed access to mobile-alerts.eu. I’d first check there to see if anything has changed.

Thanks.
Mobil Alerts app Android works.
And if I use link at browser it also works.

Perhaps you’ve hit a limit, used up your allowed queries for month/day.