Wiki source code of Node-RED on Docker

Last modified by Normann P. Nielsen on 2025/12/12 15:15

Hide last authors
Normann P. Nielsen 1.1 1 {{toc/}}
2
3 After seeing some of the stuff my colleague Martin has done with Node-RED, I wanted to switch to oit, despite my KISS strategy, and Node-RED just introduces another device to mange.
4
5 = Installing Node-RED as a Docker container =
6
7 But, starting it on Docker on my Home Laptop (also used for [[Home Assistant on Docker>>Main.information-technology.home-assistant.home-assistant-on-docker]] and [[Plex Media Server>>Main.information-technology.plex-media-server]]):
8
9
10 {{code}}
11 mkdir /opt/node-reddocker run -d --restart unless-stopped --network=host -v /opt/node-red:/data --name nodered nodered/node-red
12 {{/code}}
13
14 And it was running on http://sparrow:1880. I saw some wierd stuff in the start, until i pressed "Deploy" the first time, after that every thing has just bee working.
15
16
17
18 Node-RED has no (as far as I know currently, any users or admin/management in front of it by default, and I wanted to be able to use it from home and work/work-vpn... so I created a dual stack of Reverse Proxies:
19
20 {{drawio diagramName="Proxies.drawio" /}}
21
22 Setting the browser to http://nodered.mos-eisley.dk it hits my Confluence Server (in a Datacenter) and the Apache2 on it proxies it to [[http://cantina,mos-eisley.dk:1890]] (Fiber Router with port forward to Home laptop), and the Apache2 on the Laptop proxies it to sparrow:1880 (the Docker container on the Home Laptop).
23
24 On both Apache2 I have IP Restrictions, and on the first, username/password - otherwise all people at work could change my Node-RED setup:
25
26
27 {{code}}
28 <VirtualHost nodered.mos-eisley.dk:80>
29 ServerName nodered.mos-eisley.dk
30 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
31 ErrorLog ${APACHE_LOG_DIR}/nodered.mos-eisley.dk-error.log
32 CustomLog ${APACHE_LOG_DIR}/nodered.mos-eisley.dk-access.log combined
33 RewriteEngine on
34 RewriteCond %{HTTP:Upgrade} ^WebSocket$ [NC]
35 RewriteCond %{HTTP:Connection} Upgrade$ [NC]
36 RewriteRule .*/(.*) "ws://cantina.mos-eisley.dk:1890/$1" [P,L]
37 ProxyPreserveHost On
38 ProxyRequests Off
39 ProxyPass / http://cantina.mos-eisley.dk:1890/
40 ProxyPassReverse / http://cantina.mos-eisley.dk:1890/
41 <Proxy *>
42 Order deny,allow
43 Deny from all
44 Allow from xxx.xxx.xxx.xxx
45 Allow from yyy.yyy.yyy.yyy
46 Authtype Basic
47 Authname "Password Required"
48 AuthUserFile /etc/apache2/.htpasswd
49 Require valid-user
50 </Proxy>
51 </VirtualHost>
52 {{/code}}
53
54 Creating password:
55
56
57 {{code}}
58 sudo htpasswd -c /etc/apache2/.htpasswd <username>
59 {{/code}}
60
61
62 {{code language="cantina.mos-eisley.dk"}}
63 <VirtualHost *:1890>
64 ServerAdmin webmaster@localhost
65 RewriteEngine on
66 RewriteCond %{HTTP:Upgrade} ^WebSocket$ [NC]
67 RewriteCond %{HTTP:Connection} Upgrade$ [NC]
68 RewriteRule .*/(.*) "ws://localhost:1880/$1" [P,L]
69 ProxyPreserveHost On
70 ProxyRequests Off
71 ProxyPass / http://localhost:1880/
72 ProxyPassReverse / http://localhost:1880/
73 <Proxy *>
74 Order deny,allow
75 Deny from all
76 Allow from 77.243.52.144 # www.mos-eisley.dk
77 Allow from 10 # Local Lan
78 </Proxy>
79 </VirtualHost>
80 {{/code}}
81
82
83
84 With this setup I can use it from xxx.xxx.xxx.xxx and yyy.yyy.yyy.yyy that represents some well known adresses like work and fiber.
85
86 = Adding Node-RED To Home Assistant =
87
88 === Node-RED ===
89 In Node-RED, under Settings → Palette - Add "node-red-contrib-home-assistant-websocket":
90
91 [[image:Screen Shot 2022-12-05 at 21.34.13.jpg]]
92
93 === Home Assistant ===
94 In Home Assistant, Add "Node-RED Companion" as an Integration:
95
96 [[image:Screen Shot 2022-12-05 at 21.36.27.jpg]]
97
98 Check out [[https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/custom_integration/]] for some tips also.
99
100 And You are ready to go.
101
102 = Automations =
103
104 === Lights ===
105 Now, Home Assistent is my "heart" of the Installation, but starting to use it, I realize that it has Integrations for Philips Hue and MQTT directly, so Home Assistant could be bypassed for several automations.... I guess that more religion than tech...
106
107 My first Automatins in Home Assistant was to bridge my propritary [[ZenseHome>>https://zensehome.dk/]] system to Philips Hue:
108
109 [[image:Screen Shot 2022-12-05 at 21.41.36.jpg]]
110
111 Later on,  [[ZenseHome>>https://zensehome.dk/]] actually got native Hue support... but Im stil using Home Assistant.
112
113 So, I moved this to Node-Red:
114
115 [[image:Screen Shot 2022-12-05 at 21.43.14.jpg||width="1011"||height="96"]]
116
117 The "Listen" is listening om [[MQTT>>Main.information-technology.home-assistant.adding-mosquitto-mqtt-to-my-home-automation]] Directly and turns on the ZenseHome ceiling outlet (just to be sure) and turns on the Philips Hue lightbulbs through Home Assistant.
118
119 I have also used the Philips Hue Zone "Christophers Ceiling Light" instead of turning on all three bulbs indivudually.
120
121 So, I moved all these similar automations from Home Assistant to Node-Red in no time!
122
123 === Stream Deck Support ===
124 My youngest son got a [[Stream Deck>>https://www.elgato.com/en/stream-deck]] and we wanted to be able to trigger stuff from the buttons, so I looked into the Home Assistant API - and well, You need authetications and all sorts of "complicated" stuff.
125
126 So I turned to Node-Red, and found the "http in" node, which setup and url that Node-RED listens on:
127
128 [[image:Screen Shot 2022-12-05 at 21.49.03.jpg||width="477"||height="250"]]
129
130 Added the toogle for the Home Assistant entity (A [[Philips Hue Plug>>https://www.philips-hue.com/da-dk/p/hue-smart-plug/8719514342309]]):
131
132 [[image:Screen Shot 2022-12-05 at 21.50.49.jpg||width="584"||height="440"]]
133
134 And wired them together:
135
136 [[image:Screen Shot 2022-12-05 at 21.49.55.jpg||width="591"||height="151"]]
137
138 Now, going to [[http://sparrow:1880/switch-vr]] simply toggles the VR on/off - no authentication or similar needed, the URL can be put directly into the Stream Deck software and assigned to a Button.
139
140 === Messaging ===
141 I looked into messaging (Home Assistant already setup for twillo) and found that Facebook Messenger are possible, but the setup is complicated.
142
143 So I turned (as and SMS alternative) to Discord, from the Settings → Palette - Add "node-red-contrib-discord-advanced":
144
145 [[image:Screen Shot 2022-12-05 at 21.55.16.jpg||width="1028"||height="534"]]
146
147 The setup is a bit complicated - follow the links on [[https://github.com/Markoudstaal/node-red-contrib-discord-advanced]]
148
149 Before sending the Discord Message, a "function" node will set the message (msg) - that is the Yellow/orabge "Prepare Message":
150
151 [[image:Screen Shot 2022-12-05 at 21.59.12.jpg||width="649"||height="303"]]
152 {{success}}The "msg.channel" is and ID and can be found in the URL for the Channel when running Discord in a Browser, its NOT a string like "#home-assistant". Thank for Support from the coders.{{/success}}
153
154
155
156
157 And so, a few messages to Discord:
158
159 [[image:Screen Shot 2022-12-05 at 21.57.30.jpg||width="754"||height="250"]]
160
161
162
163 Result:
164
165 [[image:Screen Shot 2022-12-05 at 22.02.55.jpg||height="250"]]