Haiku onUnitLevelChange behavior

snarkyninja

Member
Background: I have two units that I want to link the states of, i.e. Unit1 ON means Unit2 ON, Unit1 OFF means Unit2 OFF and vice versa. In PC Access, the code was pretty simple:

WHEN Unit1 ON
THEN Unit2 ON

WHEN Unit1 OFF
THEN Unit2 OFF

WHEN Unit2 ON
THEN Unit1 ON

WHEN Unit2 OFF
THEN Unit1 OFF

I tried to do the same in HaikuHelper with the onUnitLevelChange like so:

function onUnitLevelChange(unit) {
if (unit.name == 'Unit1') controller.unitWithName('Unit2').setLevel(unit.level);
if (unit.name == 'Unit2') controller.unitWithName('Unit1').setLevel(unit.level);
}

Unfortunately I found that onUnitLevelChange was being called for every singe step during the dim/brighten process, and whenever a command was issued my lights would set themselves to random levels between 0 and 100 every second (change from 2 to 78 to 53 to 100 to 2 to 12 etc. etc.).
 
I also tried it with the following:

function onUnitOn(unit) {
if (unit.name == 'Unit1') controller.unitWithName('Unit2').setLevel(100);
if (unit.name == 'Unit2') controller.unitWithName('Unit1').setLevel(100);
}

function onUnitOff(unit) {
if (unit.name == 'Unit1') controller.unitWithName('Unit2').setLevel(0);
if (unit.name == 'Unit2') controller.unitWithName('Unit1').setLevel(0);
}

This looks a lot more like the old PC Access code, and exhibited different behavior - turning them on worked fine, turning them off started the cycle of random levels again.
 
Is there any way for me to get what I want out of this? Whatever level one unit is set to, the other one should be set to as well. Etc. And is onUnitLevelChange really supposed to execute repeatedly during a single change? Thanks for your time!
 
Hey SnarkyNinja, could you not just wrap the code in PC Access with a button, then trigger the button from Haiku Helper?
 
WHEN UnitsOn ON
THEN Unit1 ON
THEN Unit2 ON

WHEN UnitsOff OFF
THEN Unit1 OFF
THEN Unit2 OFF

 
Then HH Code:
 







  1. function onButtonActivate(button) {
    if(button.name == ‘UnitsOn’) {
    WhatYouWantItToDo }
    if(button.name == ‘UnitsOff’) {
    WhatYouWantItToDo }
    }
     
    You Could also just use the "Buttons" menu with Haiku, if you were just simply triggering them together.  Admittedly I don't know what you are trying to accomplish, but I think this would be a way you could attack it. 
 
The reason you are getting this "random" behaviour is because you are creating a loop on your first example. One triggers the other. You need to add some logic to prevent the loop. Ie. you could check that the state is the same first and if its the same, dont update it.
 
Back
Top