Skip to content

Instantly share code, notes, and snippets.

Avatar

Julian Knight TotallyInformation

View GitHub Profile
@TotallyInformation
TotallyInformation / README.md
Created Sep 29, 2017
UIbuilder with MoonJS - simple, responsive UI
View README.md

Here is a quick example using UIbuilder which is an alternative UI for Node-RED.

It demonstrates the use of a lightweight front-end library, MoonJS, which can be a lot faster and easier to work with than Node-RED's built-in Dashboard UI once you reach the point of needing to customise the UI beyond the standard capabilities of Dashboard.

This is a really quick demo of what you can do with uibuilder. MoonJS is a lightweight alternative to the likes of Angular (used by Dashboard), REACT, VueJS, etc. It is fast and really easy to use.

Here are the steps to set up (details given below):

  1. Install MoonJS & uibuilder using npm
@TotallyInformation
TotallyInformation / README.md
Created Aug 20, 2016
Node Dashboard: How to access the msg object in a Template script
View README.md

Introduction

node-red-dashboard provides an easy to access web UI for Node-Red. It uses Angular, Angular Material and JQuery all of which are available to work with. Most of the available nodes are fairly simple to use but the Template node is available to use for those times when the simple nodes are not enough. Using the Template node can be easy, see the documentation. However, it can get difficult very quickly.

I put this flow together to illustrate how to access the msg object that is passed to the Template node from a JavaScript block within the template. This gives you access to the msg object in the browser.

How things work

Node that interactions with the Dashboards you create happen in two parts. The first is when you initially load the Dashboard UI. This is a single page app (SPA) so all of the Dashboard nodes you've used will all contribute to the UI. You will

@TotallyInformation
TotallyInformation / README.md
Last active Dec 17, 2020
Simple uibuilder flow (Quote of the Day)
View README.md

This simple flow will deliver a random quote of the day to a web URL using uibuilder.

All of the front-end html, javascript and css is included in the flow so simply add the flow and edit the front-end code using uibuilder's built-in editor.

See if you can work out how to use the control outputs (output port #2 on the uibuilder node) to trigger a new quote when you reload the page. (Hint: See the GitHub page for this example as it has another flow attached).

The real purpose of this flow is to help you learn a little bit about VueJS which is the default front-end framework provided with uibuilder v2 and v3.

The front-end code is extremely simple so do take a look and see how it works.

View iot-schemas.js
/** Standardised data schema's for IoT Home Automation
*/
const schemas = {
/** network table
* A table of network devices (logical and physical)
* This schema uses a structure suitable for use with VueJS/bootstrap-vue `b-table` options.
* The table is an object of objects with the ID as the primary key
* It is periodically updated by an NMAP scan.
@TotallyInformation
TotallyInformation / README.md
Last active Sep 10, 2019
Debug messages using uibuilder
View README.md

Well maybe "debug" is slightly over selling it!

This example will dump any msg you send to the uibuilder node out to the front-end web page and will nicely format it.

You will get an entry on the page for every topic that you send.

@TotallyInformation
TotallyInformation / README.md
Last active Sep 7, 2019
JSONata Example: Changing topic and payload
View README.md

OK, so I've been a bit slow in getting my head around JSONata. I knew it was going to be valuable but I've just had my first actual use case.

I needed to rebuild an output payload from several parts of the input so I could monitor inputs from an RFXtrx433E. I wired up all the input nodes from Max's RFX node, added a timestamp using my moment node then added a change node with 2 changes.

The first change alters the msg.topic - I wanted to add RFX/ to the beginning to push the MQTT output messages to a new, dedicated path. Here is the JSONata for that:

"RFX/" & topic
@TotallyInformation
TotallyInformation / README.md
Created May 13, 2017
Reliably Get Network IP Address
View README.md

Your network IP address is the default address that other systems on your network will see as the "server" running Node-RED.

In many environments though, this address is remarkably hard to get hold of reliably. Particularly when you have multiple NIC's. Worse, many of the methods are totally different on different operating systems.

There is one fairly reliable method however, though it does require a connection to the Internet.

To make use of this, you first need to add a global function in settings.js

    functionGlobalContext: {
@TotallyInformation
TotallyInformation / README.md
Created Dec 21, 2016
Simple Windows Text to Speech
View README.md

Windows from certainly at least version 7+ and the equivalent server versions have an excellent built-in Speech engine that does both text-to-speech and speech recognition.

The speech engine is written as a system library and so is easily called from PowerShell.

This flow makes use of that feature and uses the exec node to shell out to PowerShell, calling the speech engine using a one-line PowerShell script.

You send the text into the exec node and it passes it through.

See the comment node for some details. This is exactly the way that the popular say.js library works.

@TotallyInformation
TotallyInformation / README.md
Created Sep 3, 2016
Node-RED Process Details (Debugging, Linux)
View README.md

This example flow will let you monitor the Operating System process details for your Node-RED instance.

To make this work, you need to know the PID (Process Identifier) of your Node-RED instance. The easiest way to do that is to grab it on startup and add it to the global variables from settings.js. Add the following to your settings.js file & restart NR:

...
functionGlobalContext: {
    'pid': process.pid
},
...
@TotallyInformation
TotallyInformation / README.md
Created Jun 12, 2016
Example reading file paths using native node.js modules
View README.md

This example flow uses the native Node.JS fs and path modules to read a list of file names having a specified file extension from a given folder.

You can pass the folder and extension strings in via the msg.payload. Output is a single message for each file found. The payload will be the full file path. Everything other than the payload will be passed through to each output msg.

You would actually probably be better off using a 3rd party module as indicated in the main code but this may be a useful example of using modules in a Node-Red function node.

Note that function nodes run in a "sandbox" and have restricted syntax. As such, you cannot do:

var fs = require('fs');