ui_LED not always working


I have one of those ui_LEDs on my dashboard.

(Big picture)
I compare two inputs.
If they are less than 'n' different it is good.
If they are greater than 'n' different, it is bad.

So the output is basically:
true if good and false if bad.

It supposedly works, but every time I load that page, the LED is blank/grey.

I have 3 inject nodes connected to it.
ture, false and "blah" - to wipe it.

I have a debug node from the function node and I see the true being sent.
The only quibble is that it is BLUE, not ORANGE as true/false signals are.
But the inject nodes produce the same result in the output (debug on the right of the screen).

Pressing the true button it says it is injected, but the LED stays off.
But if I press the false button (and either look at the dashboard then or wait) the LED is then working.
So I can press false 'trueand the LED is green. Or I just pressfalseand the LED is red, until the next _cycle_ comes around with atrue` and it turns green.

The is the important part of the flow.
It shows the function node and you can look at its outputs. You see the debug node, the LED node, and the 3 inject nodes.

The LED is GREY - no input.
There is a pulsed output from the function node (say every 18 seconds) of true.
The LED stays GREY.

Pressing the true button nothing happens.
It is only when I press the false button that it turns RED and then starts to obey the function node output.

And: typicall as I am posting this, it is behaving itself. But only now is it doing that.
When I started it wasn't.

I know: Hard to believe. So I am not sure what to say.
It sometimes works. But a lot of the time it doesn't.


[{"id":"eed5dd50.fd5f48","type":"ui_led","z":"e2bd5a4e.5597e8","group":"44e9ffdf.d2d948","order":1,"label":"Time sync","colorForValue":[{"color":"red","value":"false","valueType":"bool"},{"color":"lime","value":"true","valueType":"bool"}],"name":"","x":900,"y":2330,"wires":[]},{"id":"45f33aee.ea5af4","type":"inject","z":"e2bd5a4e.5597e8","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":700,"y":2470,"wires":[["eed5dd50.fd5f48"]]},{"id":"98458eac.83762","type":"inject","z":"e2bd5a4e.5597e8","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":700,"y":2510,"wires":[["eed5dd50.fd5f48"]]},{"id":"839e9da.fa9976","type":"inject","z":"e2bd5a4e.5597e8","name":"","topic":"","payload":"wipe","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":700,"y":2550,"wires":[["eed5dd50.fd5f48"]]},{"id":"16df7bcf.f3953c","type":"debug","z":"e2bd5a4e.5597e8","name":"Time Sync LED","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1060,"y":2310,"wires":[]},{"id":"80e3a376.889948","type":"function","z":"e2bd5a4e.5597e8","name":"","func":"var threshold = context.get('threshold')|| 60;\ncontext.set('threshold', threshold);\nmsg3 = {payload: threshold};\n\nif (msg.topic == 'Debug')\n{\n    if (msg.payload === true)\n    {\n        context.set('db', 1);\n    } else\n    if (msg.payload === false)\n    {\n        context.set('db', 0);\n    }\n}\nvar db = context.get('db');\n\n//---------------------\nif (msg.topic == 'slip')\n{\n    if (db == 1)\n    {\n        node.warn(\"**************************\");\n        node.warn(\"Threshold set to \" + msg.payload);\n    }\n    context.set('threshold', msg.payload);\n}\n//---------------------\nif (msg.topic == \"RTC\")\n{\n    context.set('RTC',msg.payload);\n    //node.warn(\"RTC   \" + context.get('RTC'));\n}\nif (msg.topic == \"local\")\n{\n    context.set('local', msg.payload);\n    //node.warn(\"Local \" + context.get('local'));\n}\nif (db == 1)\n{\n    node.warn(\"Stored RTC value \" + context.get('RTC'));\n    node.warn(\"Stored RPI value \" + context.get('local'));\n    node.warn(\"Stored threshold value \" + context.get('threshold'));\n}\n\nif (context.get('RTC') !== 0)\n{\n    if (context.get('local') !== 0)\n    {\n        var a = context.get('RTC');\n        var b = context.get('local');\n        var r = a - b;\n    }\n    if (db == 1)\n    {\n        node.warn(\"Error is \" + r);\n    }\n    if (r > threshold)\n    {\n        //  ** We have a problem! **\n        if (db == 1)\n        {\n            node.warn(\"Part 1\");\n        }\n        node.status({fill:\"red\",shape:\"dot\",text:\"Time error\"});\n        msg1 = {payload: false};\n        msg2 = {payload: r};\n        return [msg1, msg2, msg3];\n    } \n    if (r < 0)\n    {\n        r = b - a;\n        if (r > threshold)\n        {\n            //  ** We have a problem! **\n            if (db == 1)\n            {\n                node.warn(\"Part 2\");\n            }\n            node.status({fill:\"red\",shape:\"dot\",text:\"Time error\"});\n            msg1 = {payload: false};\n            msg2 = {payload: r};\n            return [msg1, msg2, msg3];\n        }\n    }\n    else\n    {\n        //   All Good!\n        if (db == 1)\n        {\n            node.warn(\"Part 3\");\n        }\n        node.status({fill:\"green\",shape:\"dot\",text:\"Time good\"});\n        msg1 = {payload: true};\n        msg2 = {payload: r};\n        return [msg1, msg2, msg3];\n    }\n}\n","outputs":3,"noerr":0,"x":740,"y":2330,"wires":[["eed5dd50.fd5f48","627aca75.54ebfc","e4853e2f.376418","b9b46c55.6fae6","16df7bcf.f3953c"],["b8dac5dc.347b2","bd2b18ee.4803c8"],["c9fb0e46.8a1908","cc9de920.79fa58"]],"outputLabels":["state","difference","threshold"]},{"id":"44e9ffdf.d2d948","type":"ui_group","z":"","name":"Local Time","tab":"173dcde5.96e88a","order":2,"disp":true,"width":"11","collapse":false},{"id":"173dcde5.96e88a","type":"ui_tab","z":"","name":"Local Readings","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

Are there any elephants in there?
(or maybe: Gremlins?)


Many i'm afraid - your code has many holes!

So the flow you posted has a function node with 3 outputs but you only use one output.

Additionally, that function has no input so will never be triggered.

Even if it was triggered, msg.topic must be equal to "RTC" so that context.set('RTC',msg.payload) will be set. Thats just one of several conditions that must be met before your code could ever reach a return.

Some other observations...

  • you are declaring variables inside an ifstatement (that may or may not be called) then use the variable outside of the if scope. e.g...
if (context.get('local') !== 0)
        var a = context.get('RTC');
        var b = context.get('local');
        var r = a - b;   // <<<< bad practice to declare var inside if{} scope then use it outside of that scope. It will work but only due to the nature of JS var scope
    if (db == 1)
        node.warn("Error is " + r);
    if (r > threshold) //here you use r but it may not have been initialised as its declared inside an IF statement! bad practice! 

In summary,

  1. you never initialise the ui_LED (e.g. you could use an inject with "Inject once" enabled to initialise it)
  2. Your code inside function has so many conditions to meet before ever hope of reaching a return [msg1,msg2,msg3] - it is very likely one or more of these conditions are not being met so the execution never reaches the return
  3. Why do you keep calling context.get('RTC')? why dont you create a local variable at the beginning of your code & use that? You can still store it in context but just use a local var for your function code.
  4. Add node.warn messages on all paths of code (inside each if and each else). In particular, add a node.warn("returning xxxxx") just before your return statements to understand if/when the return code is reached. I suspect you will find it is not reached when you expect it (due to the many if() conditions you have)

Hi Steve.

Sorry, but the node does work. It puts out true from the first output at 18 second intervals.
The LED doesn't always turn GREEN though.

Thanks for the tips, but sorry, I have to reply:

Yes the function node has 3 outputs.
Yes, I use them all. The other two are nothing to do with the LED not working.

Of course there is no input. Again: that is outside the scope of the problem.

Thanks for the heads up on the variable declaration. I try not to do that but this whole node is a bit of a hack job to try and alert me if the two clocks don't match.
(Little good it will do as shown, but again: Outside the scope of the post.)

Here is a shot of the node at work. Yes the screen is a mess. That's life.
You can see the true messages coming out and displayed by the debug node.
The problem - and of course: again like so many of my problems, now I just checked it is glowing - is that is that I would go to the screen where the LED is and it would be blank.

I added the 3 inject nodes to test the LED working.
As I mentioned: It is "Grey". I inject the true button. Look. Still grey.
I inject the false button and look. It is red. At the end of that 18 second cycle, it turns Green.
Or, I inject the false then the true button and vlila! It is Green, and starts playing the game.

During all this, the debug node is (every 18 seconds) showing me true being injected.

Doesn't make sense.


And yet you want us to reverse engineer why you get errors.


I am asking for help on a part of a flow.

Exactly how much of that flow do you want me to post?

There is about 8 tabs of flow. Some sparse, some a lot worse than that for nodes.

I cut out the piece which is of interest together with the output.

I explained what I was seeing and included that using the 3 (extra) inject nodes it was/is/(sometimes/usually) doing weird things.

The output is doing what it needs to do.

What else am I supposed to do?


You have been given help and suggestions by Steve. You have yet to act on those, other than to say that the function that is driving the led is correct, although it doesn't do what you want. So I guess its a stalemate for now.

You also haven't shown us how the inject true button is configured


I've re-written the code to get the variables more global.
The first three lines define (set up) a, b, and r.

I'll let it go for a few days and see what happens.


I've got lucky.

Here are some screen shots showing how the LED isn't working.

Pic 1: Establish the LED is "blank".
Pic 2: The dashboard is responding to inputs.
Pic 3: The 18 second time frame has gone and a true would be sent. Yet the LED is still "blank".

Typically after doing the screen shots and editing the pictures, the LED turned green.


You need the output from the debug node showing what is going into the led at the same time.

[Edit] Also check you are using the latest version of the LED node.


Yes, I have the latest version of ui_LED.


The output of the node is as I showed in previous picture:

Granted it is now now but it will be the same. The true messages will be there.

AND (as I said)....... Typically after I posted it, the LED decided to start playing the game and turned green.

This is possibly to validate that I am not crying wolf. Though I have said: I do not cry wolf.
But I feel I need to validate what I am saying happening because it is so difficult to make happen.


If you do a full refresh of the page (Ctrl-F5 or similar, possibly browser dependent) when it is grey but should not be, does it then show the led colour?



Yes, I think it does.

It was grey, I did the FORCE REFRESH and it turned green.


Possibly related to this then. Or possibly not.