Im getting null as the state of an item from a fibaro rgbw dimmer. This is expected, but impossible to work with or i am completely useless with rules.
First i couldn’t read the state to a number since it was null, so i introduced a new variable for that (v), this however only moves the problem to reading that variable instead so that becomes impossible instead.
This is part of my script that reads the value of the dimmer used for blue (i want that as percent in the variable b)
var Number v = 0;
var Number b = 0;
var s = Fib_Rgbw1_Blue.state ?: 0; //<-- completely ignores the elvis operator, also i cant assign state to a number right away since it fails. even though its a number..
if(s!=null){ //<- completely ignores a direct nullcheck
logInfo("RGB", "Apparently not null..");
v = s; //<-- after using 3 variables and various failsafes it still fails here since it cant set the number s to the number v since its null!..
b = v / 100.0; //<- here is where my original error was since it cant divide when something is null.
}
this gives:
00:35:48.033 [INFO ] [g.eclipse.smarthome.model.script.RGB] - Apparently not null…
00:35:48.035 [ERROR] [.script.engine.ScriptExecutionThread] - Error during the execution of rule ‘Update RGB value’: Could not invoke method: org.eclipse.smarthome.model.script.lib.NumberExtensions.operator_equals(java.lang.Number,java.lang.Number) on instance: null
So, how the am i supposed to handle something that is null?
I have also tried s+0, s | 0 and so on, but i cant come within a mile of it before it dies.
From any half-normal language you’d expect that “var Number b = (Fib_Rgbw1_Blue.state ?: 0) / 100.0;” would be enough, but here i have used all the tricks i can muster up and still cant managed something so simple as a null check, so either im completely useless or something is seriously wrong =)
rule "NullCheck"
when
Item fibaro_rgbw_dimmer changed
then
if ((Fib_Rgbw1_Blue.state as DecimalType).intValue() == 0) {
...
}
else if ((Fib_Rgbw1_Blue.state as DecimalType).intValue() > 0) {
...
}
end
Awesome! Im not sure if it worked since my value isn’t null anymore, but either way i atleast learned how to read the value in a proper way.
I have a new problem now though. I have a fibaro rgbw dimmer that i use 3 channels to control a rgb ledstrip with (the blue was in the example above).
Along with those i have a an item for color. so the conf is this one.
then i have a rule for onchange on Fib_Rgbw1_Color that grabbes the r,g,b values and send to each dimmer.
The script i struggled with was to keep the colorpicker synced if i changed the dimmer in some other way (just dimming one of the channels for ex). That script triggers on change for any of the r,g,b dimmers and creates a HSBvalue and sends to Fib_Rgbw1_Color using sendCommand.
However, now when i change a slider my second rule will trigger and create a new hsbvalue and send to Fib_Rgbw1_Color. That in turn triggers a change for that item and triggers my second rule that updates all three dimmers, and that triggers my first rule and so on…
Right now i have a boolean just flagging it not to update the dimmers and a delay for 100ms to get a bit of margin there. BUT, is there a better way to do this? can i pause events for an item or send the value without triggering a change for it?
So basically, changing the value of an item without triggering a change,. Is that possible?
when
item r changed or
item b changed or
item g changed or
item c changed
then
if ((r.state == r.previousState) && (b.state==b.previousState) && (g.state==g.previousState))
then
postUpdate(c, value)
sendCommand(c, value) // either postUpdate or send Command
else if (c.state==c.previousState)
then
…
// value is null (on first launch after reboot for example)
if(wanIpAddress.state == NULL) {
....
}
It’s easier to write and less heavy than @Extrabannies solution, but I’m not sure it does the same. I wrote my condition after the log output, and I’m not sure it checks the null value, but perhaps the automatic casted “NULL” string.
Mine is like @nbonniot solution
When the system starts the following rule is triggered that sets the values I want to one switch and one contact. You can obviously add more
rule "Set item values when system starts"
when
System started
then
if (Presence.state==NULL) Presence.sendCommand(ON)
if (MainDoor.state == NULL) MainDoor.sendCommand(CLOSED)
end
Very simple and does what I want it to do for now. In the future I may go for persistence but for now I am happy with it