Difference of === state operand in JS rules


I am currently migrating all my rules from DSL to JS (JSR223).

I have a couple of rules which check the state of dimmer and switch items:

Dimmer    FF_Bad_Licht_Decke         "Bad Decke [%d %%]"      <light>              (FF_Bad, gLight, gPersistence)      [ "Lighting" ]      {channel="knx:device:bridge:generic:FF_Bad_Licht_Decke", autoupdate="false"} 
Switch    SZ_Bad_Nachtbeleuchtung   "Szene SZ Bad Nachtbeleuchtung"      <light>      (Szenen, gPersistence)           //{channel="knx:device:bridge:generic:SZ_Bad_Nachtbeleuchtung_Control"}

My rule is the following:

var BadLichtDecke = itemRegistry.getItem("FF_Bad_Licht_Decke");
var BadLichtZentral = itemRegistry.getItem("FF_Bad_Licht");
var AlarmanlageStatus = itemRegistry.getItem("Alarmanlage_Aussenhuelle");

function bad_nachtbeleuchtung(event) {
    if (event.itemState === ON && Number(BadLichtDecke.state) === 0) {
        if (AlarmanlageStatus.state === ON) {
            events.sendCommand(BadLichtDecke, 10);
    if (event.itemState === OFF) {
        if (AlarmanlageStatus.state === ON) {
            events.sendCommand(BadLichtDecke, OFF);
            // Wenn Bad Licht Zentral an war -> ebenfalls ausschalten
            if (BadLichtZentral.state === ON) {
                events.sendCommand(BadLichtZentral, OFF);
        // Wenn Alarmanlage zwischenzeitlich ausgeschalten wurde und Nachtbeleuchtung an war (=10%) diese ebenfalls ausschalten
        if (AlarmanlageStatus.state === OFF) {
                if (Nummber(BadLichtDecke.state) === 10) {
                    events.sendCommand(BadLichtDecke, OFF);
when("Item SZ_Bad_Nachtbeleuchtung changed")(bad_nachtbeleuchtung);
    "Bad Nachtbeleuchtung",
    "Rule um das Bad nachts gedimmt zu beleuchten"

The event.itemState and item.state is an JS typeof=object.

So why is event.itemState === ON working and BadLichtDecke.state === 0 is not working.

In case of the second statement In do have to do a type cast with Number() to get the statement run?

Number(BadLichtDecke.state) converts to a js Number object. In JavaScript, Number objects and literals are not the same type, so can’t be tested for equality using ===.

See JavaScript Numbers for more info (scroll down to “Numbers Can be Objects”)

1 Like

If moving to JS rules it would be useful to take a javascript tutorial to learn about the language constructs. This forum is not a JavaScript tutorial.

Another option I use quite frequently is a Google search fot r the explanation & answer.