Rémy Schumm blog ingénieur

Drinking Reminder with Philips Hue and OpenHAB

publiziert am 03. 01. 2023 um 11:05

This is a little an maybe overdesigned sample or try to have a blue blinking Philips Hue light on my sideboard using OpenHAB to remind me to drink water every hour.
Blaue Lampe

Idea

As many people working on computer, I forget to drink water all the time. So: This is a little an maybe overdesigned sample or try to have a blue blinking Philips Hue light on my sideboard using OpenHAB to remind me to drink water every hour.

Implementation

I added a Rule - which in fact is a schedule - that calls a Rule which actually is a Script. (There is a slight lack of sharpness in the terms that costed me some time.) The Rule looks like this:

rule

The Rule calls a Script that does following:

As you can see, this needed some really ugly «häcks»:


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); 
    
}

The Logger will not log into the OpenHAB GUI, you have to log into the server and have a look at:

Logs:

digitaltwin@kassandra:~ $ tail -f /var/log/openhab/*.log 

Documentation

The Hue Bridge Documentation can be found on OpenHAB, but was not very useful as the used JaveScript Language is not strongly typed and I had no idea what is what. Some guessing and asking in the the OpenHAB forums helped me, especially this my own thread.

Conclusion

I was suprised how difficult it was to accomplish such a simple task with this home automation system. Maybe I am completely on the wrong way of doing it - but I did not find the right way.
Most complexity came from the fact, that the abstractions are all done in non-typed languages that lack proper documentation: no idea what a «Command» is, or a «Color» - they are just randon, untyped strings.
Also, there is a system of «Autoupdate and Predictions» and a mix of async communications and state handling causing racing conditions and delayed updates that I could not get under control. My lamp still shows randomly (just sometimes) wrong colors or brightness after a blue bliniking run.
Another details was also the dimming behaviour when you send ON/OFF - you cannot properly blink. I did not solve this problem.
In a second attempt I would maybe just use the Philips Hue API directly without OpenHAP - or just use a Schedule in the Hue App 😀. But that would have be less fun… haha.

Hinweis: dieser Blog wiederspiegelt meine persönliche Meinung und hat nichts mit meiner Anstellung als Dozent der zhaw noch mit anderen Anstellungen zu tun.


zurück zum Seitenanfang