# ui\_LED not always working

**URL:** https://discourse.nodered.org/t/ui-led-not-always-working/10369
**Category:** Dashboard
**Created:** [20 April 2019 00:56 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369 "2019-04-20T00:56:10Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [20 April 2019 00:56 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/1 "2019-04-20T00:56:11Z")

</div>

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` 'true`and the LED is green. Or I just press`false`and the LED is red, until the next _cycle_ comes around with a`true` 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.

Clarification:  
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.

Code:

```auto
[{"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?)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [20 April 2019 01:37 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/2 "2019-04-20T01:37:29Z")

</div>

> [@Trying\_to\_learn](#):
>
> 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 `if`statement (that may or may not be called) then use the variable outside of the if scope. e.g...

```auto
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)

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [20 April 2019 06:08 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/3 "2019-04-20T06:08:42Z")

</div>

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.

 ![Screenshot%20from%202019-04-20%2015-57-14](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0a9f9bfc67ef6b2ffab310dd89756a35b6d2e471.png)

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [20 April 2019 06:22 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/4 "2019-04-20T06:22:51Z")

</div>

> [@Trying\_to\_learn](#):
>
> Yes the screen is a mess. That's life.

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

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [20 April 2019 06:26 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/5 "2019-04-20T06:26:36Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [20 April 2019 06:44 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/6 "2019-04-20T06:44:31Z")

</div>

> [@Trying\_to\_learn](#):
>
> 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

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [20 April 2019 06:46 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/7 "2019-04-20T06:46:49Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [20 April 2019 09:52 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/8 "2019-04-20T09:52:27Z")

</div>

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.

 ![Screenshot%20from%202019-04-20%2019-45-03](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/79ff20da7f8299487594616295f508ce0fe71458.jpeg)  
 ![Screenshot%20from%202019-04-20%2019-45-15](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c567f325a3a7ba444df89be218f93055320d4749.jpeg)  
 ![Screenshot%20from%202019-04-20%2019-45-18](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/879074b14a092a70838017122524a7f570f3e226.jpeg)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 April 2019 10:14 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/9 "2019-04-20T10:14:31Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [20 April 2019 10:22 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/10 "2019-04-20T10:22:46Z")

</div>

Yes, I have the latest version of ui\_LED.

![Screenshot%20from%202019-04-20%2020-15-55](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex026/uploads/nodered/original/2X/9/9cd0442de0e24aa9f5403b97f528ad6476dd4609.png)

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.

![](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex026/uploads/nodered/original/2X/c/c4f9355c522660bb549a1e6b787e4fe8a7b1c863.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 April 2019 16:02 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/11 "2019-04-26T16:02:02Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [26 April 2019 21:09 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/12 "2019-04-26T21:09:13Z")

</div>

Hhmm.....

Yes, I think it does.

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

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 April 2019 21:10 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/13 "2019-04-26T21:10:20Z")

</div>

Possibly related to this then. Or possibly not.

> <https://github.com/Adorkable/node-red-contrib-ui-led/issues/29>

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [26 April 2019 21:11 UTC](https://discourse.nodered.org/t/ui-led-not-always-working/10369/14 "2019-04-26T21:11:46Z")

</div>

Maybe.

Thanks.
