Rule: Types of Light Commands, Phantom Event

  • Platform information:
    • openHAB version: 3.3.0

I have following script:

The aim of this script is to set a Philips Hue Lamp to Blue for 4 seconds and then go back to the state the lamp was before (ON or OFF, same color etc.)

The script works sometimes, sometimes not: the lamp does not switch off when it was off, or has the wrong color. No Idea why. Details see below in the logs.
I think I have typing errors, as it is JavaScript I have no idea if I have the right types. Or I have some fundamental knowledge gap about the Events in the Hue System.

Question:

  • What is wrong?

What I am seeing:

  • If the Lamp was switched ON before, everything goes ok.
  • If the Lamp was switched OFF before, it also works. Also the color gets restored to the state before. But :hot_face: when switching it ON from elsewhere in the Hue System, e.g. by a manual switch, the Lamp gets an Event from somewhere to set it to blue. I call this Event a «Phantom Event» .
var farbeVorher;
var statusVorher; 

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var thread = Java.type('java.lang.Thread’)/

// store previous state 
farbeVorher = itemRegistry.getItem('Glas_Farbe').getState();
statusVorher = itemRegistry.getItem("Glas_Switch").getState(); 

// set to blue for 4 s
logger.info(farbeVorher);
events.sendCommand('Glas_Switch', 'ON');
events.sendCommand('Glas_Farbe', '210,100,100');
thread.sleep(4000);

// restore previous state 
events.sendCommand('Glas_Switch', statusVorher); 
events.sendCommand('Glas_Farbe', farbeVorher);

Logs of the Error Path:

# gets reset as expected to old color 309,99,0... 
2022-11-24 07:23:59.624 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Glas_Farbe' received command 309,99,0
2022-11-24 07:23:59.628 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'Glas_Farbe' predicted to become 309,99,0
2022-11-24 07:23:59.641 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Glas_Farbe' changed from 210,98,43 to 309,99,0
2022-11-24 07:24:06.447 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Glas_Switch' changed from ON to OFF

# after manual switch ON: 

# ...unexpected event to change to blue 210,100,100 ... 
2022-11-24 07:24:56.678 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Glas_Farbe' changed from 309,99,0 to 210,98,100
2022-11-24 07:24:56.686 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Glas_Switch' changed from OFF to ON

ref. Philips Hue - Bindings | openHAB

Is that a typo or should one be Glas_Switch ?

…yes, I corrected it. (You were very quick, thanks!) Though - there is another Problem :frowning:
I update the description.

What we can’t see there is what the light really did.
By which I mean, autoupdate has as usual made a prediction for us.
We cannot be sure the lamp really went to 309,99,0

We can see a “real” OFF report seven seconds later - there was no command to that Item, so no autoupdate.

Alright, so you were expecting to turn on at the colour of the last command you sent - 309,99,0

There is an assumption in that - because 309,99,0 is the equivalent of OFF.
Are you sure your light takes any notice of colour components when instructed to turn off?
I’m guessing it doesn’t.

I think you’ll have to amend your rule to “restore old colour” at some non-zero brightness, and then a short pause before commanding OFF

1 Like

hi - thanks a lot for this answer. you learned me two things:

  • autoupdate and prediction
  • the insight, that 309, 99, 0 is probably ignoring the colors and is aequivalent to OFF
    I will have to make more research to understand how this all works. apparently this very easy thing I want to do is already very non-trivial without knowing how things are going on behind the scenes :frowning:

Hi - thanks to your input, @rossko57 , I understood a bit more clearly what is going on behind the scenes - and I tried to trick the system by switiching ON the lamp before blinking so I can save the original color.
I found a «häcky» solution that works - most of the times. There are still racing conditions.


var colorBefore;
var stateBefore; 

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var thread = Java.type('java.lang.Thread')

store(); 
blinkBlue(); 
restore(); 


function store() {

    stateBefore = itemRegistry.getItem("Glas_Switch").getState(); 
    thread.sleep(3000);
    //switch on in original color... 
    events.sendCommand('Glas_Switch', 'ON');
    thread.sleep(12000);
    //wait for racing condition, save original color while lamp is on... 
    colorBefore = itemRegistry.getItem('Glas_Farbe').getState();
    logger.info("Wasserlicht: Glaslampe on start was: " + colorBefore + " - " + stateBefore);
    
}


function blinkBlue() {
    //blue blink: 
    events.sendCommand('Glas_Farbe', '210,100,100');
    events.sendCommand('Glas_Switch', 'ON');

    //remark: the lamp does not blink properly, because some dimming effects are active 
    for(i = 0; i < 5; i++){
      thread.sleep(700);
      events.sendCommand('Glas_Switch', 'OFF');
      thread.sleep(700);
      events.sendCommand('Glas_Switch', 'ON');
    }
    thread.sleep(4000);
    
}


function restore() {

    //Lamp is on, reset to the orignial color... 
    logger.info("Wasserlicht: restore Color of Glaslampe " + colorBefore + " - " + stateBefore);
    events.sendCommand('Glas_Farbe', colorBefore);
    //fixed Bug: wenn die Lampe aus war, kommt irgendwoher ein event, das die Lampe wieder auf blau setzt, und zwar erst beim nächsten Anschalten. 
   
    //if (statusVorher == 'ON'){
    //events.sendCommand('Glas_Switch', statusVorher); 
    //}
  
    //wait for racing condition... 
    thread.sleep(12000);
    logger.info("Wasserlicht: restore State of Glaslampe " + colorBefore + " - " + stateBefore);
    events.sendCommand('Glas_Switch', stateBefore); 
    
}


Nervertheless, the fact that something so basic needs so much «tricks» make me think that I might be on a completely wrong way of doing what I want to do. Maybe speak to the Lamp API directly? Use other commands?
Maybe someone has an input.

Maybe you’re looking at it the wrong way.
If the bulb will not remember the “last” colour at turn-off time, your code can instead.
Then when you require to turn on, instead send a complete HSB with your remembered target colour.
Doesn’t seem to require any delays.

To experiment to begin with, you might use two Items - one for command by UI or rule, one linked to the “real” channel/device. Makes it easy to intercept commands like ON in a rule and convert to something else,

…I made a Blog post about this little project: Rémy Schumm Winterthur - Drinking Reminder with Philips Hue and OpenHAB