how to use variable values in template node

980 views
Skip to first unread message

Paolo Jo

unread,
May 21, 2018, 9:06:05 AM5/21/18
to Node-RED

Hi all,

In my application I would use a “template” node in order to show a text line together with a led (located below the test line).

The template node shall get the following global variables:

  • global_txt;
  • global.led
  • global_flag
and the node shall work as follows:
  • global.txt contains the string to show on the dashboard on the text line;
  • global.led determines the colour of the led (circle). If global.led = 1, the led is green, if global.led = 0, the led is red;
  • if global.flag = 0 the global.txt and led is showed in gray, if global.flag = 1 the global.txt is showed in black and the led color depend on the global.led;

Here the code

 

<script>

var txt = global.get(‘global.txt’)||0;

var led = global.get(‘global.led’)||0;

var flag = global.get(‘global.flag’)||0;

 

if (flag==0)

{

            text_color = led_color = gray;

}

else

{

            text_color = black;

            if (led == 0)

            {

                        led_color=”red”;

            }

            else

            {

                        led_color=”green”;

            }

 

</script>

 

<svg>

<circle cx="10" cy="10" r="10" style="stroke: none; fill: {{led_color}};"/>
 <text x="0" y="15" fill="{{text_color}}"> {{txt}}</text>

</svg>

 

The code doesn’t works, could you help me in order to understand where is/are the error(s)?

My main problem is how to “use” the variable txt, led and flag into SVG code lines.

 

Another question: is it possible to disable the entire template node in case of global_flag=0?

 

Thanks in advance

Paolo 

AIOT MAKER

unread,
May 24, 2018, 9:57:53 PM5/24/18
to Node-RED
Hello Paolo,

Looks like there are some syntax issues on your code (missing quotes and unbalanced curly braces). 

See if below code achieves what you want. 

I moved part of the code to a function node (personal preference). The other nodes were added to test the flow with different values of flag, text and led.



´´´
[{"id":"4117ffc1.b817","type":"ui_template","z":"44cc2866.39c908","group":"89b6c636.3f1818","name":"","order":0,"width":0,"height":0,"format":"<svg>\n\n<circle cx=\"10\" cy=\"10\" r=\"10\" style=\"stroke: none; fill: {{msg.led_color}};\"/>\n<text    x=\"30\"   y=\"15\" fill=\"{{msg.text_color}}\"> {{msg.txt}}</text>\n\n</svg>","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":654.1000556945801,"y":202.00000190734863,"wires":[[]]},{"id":"d0140866.baf4f8","type":"function","z":"44cc2866.39c908","name":"Test","func":"msg.txt = global.get(\"txt\") || 0;\nmsg.led = global.get(\"led\") || 0;\nmsg.flag = global.get(\"flag\") || 0;\n\n\nmsg.led_color  = (msg.led  === 0) ? \"red\" : \"green\";\n\nif (msg.flag  === 0) {\n    msg.led_color = \"gray\";\n    msg.text_color = \"gray\";\n} else {\n    msg.text_color = \"black\";\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":509.0000057220459,"y":202.4000015258789,"wires":[["4117ffc1.b817"]]},{"id":"29eeed59.9b5102","type":"inject","z":"44cc2866.39c908","name":"LED 0","topic":"","payload":"Testing","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":148.00000381469727,"y":230.00000524520874,"wires":[["b9caf343.452d5"]]},{"id":"b9caf343.452d5","type":"change","z":"44cc2866.39c908","name":"","rules":[{"t":"set","p":"led","pt":"global","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":293.1000099182129,"y":230.60000371932983,"wires":[["d0140866.baf4f8"]]},{"id":"3706258d.17255a","type":"inject","z":"44cc2866.39c908","name":"LED 1","topic":"","payload":"Testing","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":146.00000381469727,"y":276.00000524520874,"wires":[["6fa6de1d.1de44"]]},{"id":"6fa6de1d.1de44","type":"change","z":"44cc2866.39c908","name":"","rules":[{"t":"set","p":"led","pt":"global","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":291.1000099182129,"y":276.60000371932983,"wires":[["d0140866.baf4f8"]]},{"id":"937a6d4f.21ec1","type":"inject","z":"44cc2866.39c908","name":"FLAG 0","topic":"","payload":"Testing","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":148.00000524520874,"y":129.00000381469727,"wires":[["fe3dcfba.20445"]]},{"id":"fe3dcfba.20445","type":"change","z":"44cc2866.39c908","name":"","rules":[{"t":"set","p":"flag","pt":"global","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":303.10001134872437,"y":129.60000228881836,"wires":[["d0140866.baf4f8"]]},{"id":"c3970f4a.b4918","type":"inject","z":"44cc2866.39c908","name":"FLAG 1","topic":"","payload":"Testing","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":150.00000524520874,"y":176.00000381469727,"wires":[["e2ea84b8.83abb8"]]},{"id":"e2ea84b8.83abb8","type":"change","z":"44cc2866.39c908","name":"","rules":[{"t":"set","p":"flag","pt":"global","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":305.10001134872437,"y":176.60000228881836,"wires":[["d0140866.baf4f8"]]},{"id":"89b6c636.3f1818","type":"ui_group","z":"","name":"SVG LED","tab":"99b36f6.7a6d29","disp":true,"width":"6","collapse":false},{"id":"99b36f6.7a6d29","type":"ui_tab","z":"","name":"SVG LED","icon":"dashboard"}]

´´´


Reply all
Reply to author
Forward
0 new messages