Gathering detailed insights and metrics for epdoc-node-red-utils
Gathering detailed insights and metrics for epdoc-node-red-utils
Gathering detailed insights and metrics for epdoc-node-red-utils
Gathering detailed insights and metrics for epdoc-node-red-utils
Utilities for Node-RED Function Nodes to help with Home Assistant scripts
npm install epdoc-node-red-utils
Typescript
Module System
Node Version
NPM Version
TypeScript (96.94%)
JavaScript (3.06%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
GPL-3.0 License
84 Commits
2 Watchers
3 Branches
1 Contributors
Updated on Jun 22, 2024
Latest Version
0.15.3
Package Id
epdoc-node-red-utils@0.15.3
Unpacked Size
218.12 kB
Size
84.81 kB
File Count
33
NPM Version
10.1.0
Node Version
20.9.0
Published on
Oct 31, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
1
5
General purpose utilities for use with Node-RED and Home Assistant.
Service
wrapper, to generate payloads for use with the Call Service node.HA
wrapper, to retrieve state from home assistantUtilities for my personal use of Node-RED with Home Assistant. Included are:
setFan
function that is specific to my RF control of fans using both a
Bond Bridge to control speed (0
to 6), and wall switches to decouple the fans from power when they are off. I
do this to prevent mains noise from blowing the susceptible controllers on my
Minka fans.LocationHistory
and LocationMoving
classes that I use to monitor movement
from our house, for gate automation purposes. These are at a pre-release
level of quality.This module was originally written in ES6 and transpiled using Babel to generate
a module that could be loaded using require
or import
. Soon thereafter it
was migrated to TypeScript (developer hint: this resulted in catching quite a
few bugs). It was also migrated to Bun for build and unit
testing. Bun generates a different type of module that can only be loaded in
Node-RED using a dynamic
import,
as you will see in the next section.
1git clone epdoc-node-red-utils 2cd epdoc-node-red-utils 3bun install 4bun test 5bun run build
Perhaps the most predictable way to install this package with Home Assistant is
to add this dependency to the Node-RED package.json
file and restart Node-RED.
Node-RED is restarted from Settings > Add-ons > Node-Red. The restart should
cause the module to be installed and available. For module updates you can edit
the version number in package.json
, delete
node_modules/epdoc-node-red-utils
, then restart Node-RED.
For convenience you can add the module to globals, so that you don't need
to specify the module in each Function Node
where it is used. Here are the
required changes to /config/Node-RED/settings.json
for this to work:
1// Don't set module.exports yet 2let settings = { 3 4 // No need to touch any of the settings 5 6}; 7 8// Must use dynamic import because of the nature of how bun generates this module 9async function loadModules() { 10 const utils = await import('epdoc-node-red-utils'); 11 settings.functionGlobalContext['epdoc-node-red-utils'] = utils; 12} 13 14loadModules(); 15 16module.exports = settings;
Then, to use the following code in a Function Node, it's simply a matter of accessing the global context to get the module. In this example, the Function Node has two outputs, with the 2nd output wired to a Call Service node.
1const u = global.get("epdoc-node-red-utils"); 2const payload = u.newLightService('master_bedroom').on().payload(); 3node.send([null,{payload:payload}]); 4node.send([msg,null]); 5node.done();
Unfortunately there is no code completion in Node-RED's Function Node editor.
You can find a more exhaustive discussion of various ways to use your own libraries in Node-RED here.
The
Service
object is used to build a payload that can be passed to the Call Service
node.
Provided too are a number of subclasses for specific types of entities,
including SwitchService
, LightService
, AlarmService
, CoverService
,
FanService
and, finally FanSpeed6Service
, which is a 6-speed fan that uses a
Bond Bridge to set the fan speed and
a smart switch to turn the fans on and off.
There is the possibility for many more subclasses to be written, or you can
build your service payload directly using the base Service
class, or one of
the other subclasses.
The following shows the code for a function node that uses three equivalent implementations to tell a Cover to stop.
1let payload = newService('cover.garage').service('stop_cover').payload(); 2 3payload = new CoverService('garage').stop().payload(); 4 5let payloadBuilder = newCoverService('garage'); 6payload = payloadBuilder.stop().payload(); 7msg.payload = payload; 8return msg;
The following function node code creates a payload that can be used to set a light's brightness to 50%.
1msg.payload = new LightService('bedroom').percentage(50).payload(); 2return msg;
The following function node code shows several ways to create a payload that turns a light on.
1// In this example we directly use the LightService, 2// which will set the domain to `light` for us. 3// The LightService is a subclass of SwitchService. 4msg.payload = new LightService('bedroom').on().payload(); 5 6// In this example we use the SwitchService, but change it's default 7// domain from `switch` to `light` by specifying the full `entity_id`. 8msg.payload = new SwitchService('light.bedroom').on().payload(); 9 10// Override the default domain using the `domain` method. 11msg.payload = new SwitchService('bedroom').domain('light').on().payload(); 12return msg;
The HA class is again meant for use in Function Nodes. It provides a wrapper for a Home Assistant instance, and has methods to access the state of Home Assitant entities.
Example retrieves the state of a light.
1const gHA = global.get('homeassistant'); 2 3const ha = new HA(gHA); 4const lightEntity = ha.entity('light.bedroom'); 5const isOn = lightEntity.isOn(); 6node.warn(`The ${lightEntity.id} is ${isOn?'on':'off'}`)
This method takes a dictionary containing an id
field and optional type
field and retrieves sensor data for the listed sensors. This is a shortcut that
you might use when you have multiple sensors that you efficiently want to get
data for, and you need to access that data more than once.
1const gHA = global.get('homeassistant'); 2const ha = new HA(gHA); 3 4const sensorDict = { 5 sensor1: { id: 'input_boolean.evening', type: 'boolean' }, 6 sensor2: { id: 'sensor.outdoor_temperature', type: 'number' } 7}; 8 9ha.retrieveSensorsData(sensorDict); 10if( sensorDict.sensor1.on ) { 11 console.log('It is the evening'); 12} 13if( sensorDict.sensor2.val > 30 ) { 14 console.log('It is hot today'); 15}
The above code is equivalent to the following:
1const gHA = global.get('homeassistant'); 2const ha = new HA(gHA); 3 4if( ha.entity('input_boolean.evening').isOn() ) { 5 console.log('It is the evening'); 6} 7if( ha.entity('sensor.outdoor_temperature').asNumber() > 30 ) { 8 console.log('It is hot today'); 9}
No vulnerabilities found.
No security vulnerabilities found.