OH3 and HTTP regex ... items not refreshed

index 0 is the full match of your expression, index 1 is the first capturing group (i.e. is part between the braces. Enter the regex into https://regex101.com/ to get an explanation what each part does.

1 Like

thank you very much :slight_smile:

@J-N-K short question … how can I say in Javascript

if value is 2,3 or 4

instead of saying “different from null or undefined”?

|| means OR also in JS?

thanks
Andrea

Like “more than 1 and less than 5” ?

yep exactly … the same

I googled “how javascript more than and less than”

https://www.w3schools.com/js/js_comparisons.asp

It seems the topic is not solved yet :frowning: I’ve tried with JS, using !=null or !=undefined, or even with a value between 1 and 5, but nothing … the items are not refreshed in case there is no alarm (0 is not applied if there is a value previously set).

The first time it works, then from second time the values reported in all items are not reflecting what the website is publishing :frowning:

here my latest JS script version:

(function(i) {
    var value = /.*aw1([1-4])\..*/.exec(i);
    if (value > 2 < 5) {
       return value[1];
    } 
    return 0;    
})(input)

the goal I’m trying to achieve: if the value of the regex is 2, 3 or 4, set that value. Otherwise set the value “0”.

Any suggestion?
Andrea

This is complete nonsense. If you’d read the link suggested,it begins with

This is what I’m trying:

if (score > 0 < 8) {

and the important thing is that it doesn’t work, and so they posted looking for right answer.
You have to read the answers to find the suggestion

if (score > 0 && score < 8){
1 Like

thank you, fixed:

(function(i) {
    var value = /.*aw1([1-4])\..*/.exec(i);
    if (value > 2 && value < 5) {
       return value[1];
    } 
    return 0;    
})(input)

but if an item was reporting 2, and now it’s reporting nothing, I was expecting to see 0, and I see 2 again :frowning:

it seems if the regex doesn’t match anything, the script is not returning 0 to the item

to me the issue is “value” from the regex is a string, and not a number. Am I wrong?

It‘s neither. You need to check if value is defined and then use value[1] I’m your comparison.

as far as I can understand, if the value is reporting nothing (no alarms), I can’t overwrite with 0 an item with a different value already :frowning:

But that is exactly what my code above is doing.

Note - that 2 is not “more than 2” , so
value > 2 && value < 5
will be true for 3 and 4, but false for 2.

The next thing to remember is why you’re using

It’s because the variable value is an array.
So … you can’t compare an array to a number, shouldn’t you be looking at just one element of this array for the comparison?

if (value[1] > 1 && value[1] < 5) {

Finally, that is only going to work if the Regex is successful.
If your Regex gets no match, it will return null in javascript.
null isn’t an array, and trying to get null[1] is going to throw an error.
So you must take care to avoid that, and test for null first.
Here you can return your “default”.

    if (value == null) {
        return 0;
     }
1 Like

why we are using an array?

the website is reporting multiple alarms (aw1 aw2 … aw12), but I have one channel per alarm, so the value in the script will be always one, and not an array.

Am I wrong?
Andrea

You have no choice, that’s what you get back when you use a regex in javascript.
The has nothing to do with openHAB REGEX transformation service, apart from doing a similar string manipulation job, and works under different rules.

Them’s the javascript rules.

2 Likes

Ok, now it’s clearer :slight_smile: thank you both for your help here.

here the script now:

(function(i) {
    var value = /.*aw1([1-4])\..*/.exec(i);
    if (value[1] > 1 && value[1] < 5) {
       return value[1];
    } 
    if (value === null) {
        return 0;
     }   
})(input)

I’ve tried with a site with no alarms for Wind, and now Wind is “0” (it was “2” previously).

But if I try with a site with alarm 2 for Wind, now the Wind is still “0” :smiley:

it was a typo in my things file … now it works like a charm.

:slight_smile: :slight_smile: :slight_smile:
thank you very much.

I’ll do some checks and test. But the feeling I’m on the right track. And this is because of you both :slight_smile: thank you @rossko57 and @J-N-K

again :frowning: if I revert to a site with no alarms for Wind, I’m not able to set ‘0’ instead of ‘2’.

that is a bit frustrating :slight_smile: I’ve tried with null, with undefined … sometimes it works, sometimes is not.

How can I understand how the JS is interpreting when a regex is reporting nothing (no match)?

To reiterate

Test for null before attempting numeric comparison

Continue to return your “default” after the comparison, because you might get a non-null regex return that does not satisfy your numeric comparison.

To see what’s going in your javascript, add logging.