Selection item doesn't work the first time

openhab2
Tags: #<Tag:0x00007fe06b4dd4f8>

(JustAProgrammer) #1

So I configured this Selection item:
Everything works perfectly, (so don’t waste your time analysing my Code ;P)
except for the change of a item.
When I change from one selection item to another, it doesn’t change the LED strips (so therefore the rules doesnt get activated) and I have to select the item again.
Am I missing something here?

.sitemap:

Selection item=gLEDcolor label="Farbe wählen" mappings=[0="Aus", 1="Rot", 2="Blau", 3="Grün", 4="Magenta", 5="Cyan", 6="Gelb", 7="Weiß", 8="Epilepsie" ] icon=script

.rules:

rule "7 Farben auswählen" when Item gLEDcolor received command then switch ((gLEDcolor.state as DecimalType).intValue){ //Aus case 0: { gLight.sendCommand(OFF) } //Rot case 1: { gLight.sendCommand(ON) gLEDpr.sendCommand(38) grLEDps.sendCommand(99) } //Blau case 2: { gLight.sendCommand(ON) gLEDpr.sendCommand(40) grLEDps.sendCommand(99) } //Grün case 3: { gLight.sendCommand(ON) gLEDpr.sendCommand(39) grLEDps.sendCommand(99) } //Magenta/Violett case 4: { gLight.sendCommand(ON) gLEDpr.sendCommand(43) grLEDps.sendCommand(99) } //Cyan case 5: { gLight.sendCommand(ON) gLEDpr.sendCommand(42) grLEDps.sendCommand(99) } //Gelb case 6: { gLight.sendCommand(ON) gLEDpr.sendCommand(41) grLEDps.sendCommand(99) } //Weiß case 7: { gLight.sendCommand(ON) gLEDpr.sendCommand(44) grLEDps.sendCommand(99) } case 8: { gYeelight.sendCommand(OFF) gLEDpr.sendCommand(55) grLEDps.sendCommand(96) gArilux.sendCommand(ON) } } if (timer === null) { timer = createTimer(now.plusMinutes(90)) [| sendCommand(gArilux, OFF) sendCommand(gYeelight, OFF) timer = null ] } end


(Harry) #2

The problem is in the line

if (timer === null) { timer = createTimer(now.plusMinutes(90)) [| sendCommand(gArilux, OFF) sendCommand(gYeelight, OFF) timer = null ] }

You check timer before define.


(JustAProgrammer) #3

Hey Harry,
Why would the Timer be the problem?
Thank you


(Udo Hartmann) #4

I’m pretty sure your rule does not work perfectly (well, you wouldn’t have asked…)

Please consider to type the code in a more readable way.
Even after not analyzing your code :wink:
Was it your intention that gArilux and gYeelight is switched off exactly 90 Minutes after switching it on, regardless what you are doing meanwhile?

What do you mean by

When I change from one selection item to another

which “other selection item”?

Please be aware that you don’t need to set the mapping in an ascending order, so your rule would be way more readable when changing the selection slightly:

Selection item=gLEDcolor label="Farbe wählen" mappings=[8="Aus", 0="Rot", 2="Blau", 1="Grün", 5="Magenta", 4="Cyan", 3="Gelb", 6="Weiß", 7="Epilepsie" ] icon=script

and this would be the rule (note the very first line):

var Timer timer = null

rule "7 Farben auswählen" 
when
    Item gLEDcolor received command 
then
    if (!gLEDcolor.state instanceof Number) 
        return()
    val int iCommand = (gLEDcolor.state as Number).intValue
    if (iCommand < 7) {                                     //rot=0, grün=1, blau=2, gelb=3, cyan=4, magenta=5, weiß=6
        gLight.sendCommand(ON)
        gLEDpr.sendCommand(38 + iCommand)
        grLEDps.sendCommand(99)
    }
    else if (iCommand = 7) {                                //Epilepsie
        gYeelight.sendCommand(OFF) 
        gLEDpr.sendCommand(55) 
        grLEDps.sendCommand(96) 
        gArilux.sendCommand(ON)
    }
    else if (iCommand = 8) {                                //Aus
        gLight.sendCommand(OFF)
    }
    if (timer === null) {
        timer = createTimer(now.plusMinutes(90)) [| 
            gArilux.sendCommand(OFF) 
            gYeelight.sendCommand(OFF) 
            timer = null 
        ] 
    }
end

(JustAProgrammer) #5

There’s always something you could improve, even if it works like it should! :slight_smile: )
The Code was pretty readable but the code fences (´) made them look like this.
Yes, they should turn off after 90 minutes, after every selection change.

I’m sorry, it happens after every change of THIS selection item.
If I choose “case 1” the first time, rules don’t trigger and I have to repeat choosing “case 1”.
It’s not that big of a Problem, but I noticed this quiet often even with other Selection items.

Thank you, but isn’t the Timer instance set to null anyway?
I already set it to null in my file, just didnt copied it, sorry my bad!

Very clean and readable code, thank you!
I really have to invest more time into this programming language. XBase is not that hard, but it’s a pain to find out what works and what doesn’t.


(Udo Hartmann) #6

Well, they don’t. They turn off 90 Minutes after the first change…

This one will switch off 90 Minutes after the last change:

if (timer !== null) 
    timer.cancel
timer = createTimer(now.plusMinutes(90)) [| 
    gArilux.sendCommand(OFF) 
    gYeelight.sendCommand(OFF) 
    timer = null 
] 

In question of need to double select the value, I think it depends on the UI and has nothing to do with the rule at all.


(JustAProgrammer) #7

Yeah, I have those Problems on many rules. I already even noticed it when my lights go out too early.
I’m just too lazy to change it ;).
And It’s not a matter if they go out too soon.
Oh okay, thank you Udo.


(Rich Koshak) #8

Those are not correct code fences or else something else went wrong. See How to use code fences

You probably need to use receivedCommand instead of gLEDcolor.state in the Rule. This could explain the multi-selection problem @Nico111 is seeing. With received command triggers, the Rule triggers before the Item registry is updated. So it is best to use the receivedCommand implicit variable.

Check out the Design Pattern postings.


(JustAProgrammer) #9

Hey Rich
I’ve used the code fences from this sentence you’ve wrote:
This is a sentence with some code in it.
Thanks, I already know your post :slight_smile:
Allright I will update my rules and add some feedback to this Post:stuck_out_tongue:


(Rich Koshak) #10

Yep, that is a way to embed a tiny little bit of code inline in a sentence. You need to use the

```
// code goes here
```

type fences to preserve spacing and new lines. Add the type of code after the opening of the code block to get some syntax highlighting.