npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-red-programming-tools

v1.0.5

Published

Tool to simplify the integration between Home Assistant and Node-Red

Downloads

3

Readme

Node-RED Programming Tools (NRPT)

Tools to simplify the integration between Home Assistant and Node-Red

Todo

  • Improve and move installing IntelliSense (d.ts files) to separate package. Load d.ts files from Node-Red config.
  • Add functionalities:
    • Retrieval of entities
    • Enum for each service
  • Add better error/exception handling:
    • Catch errors
    • Logging of exceptions
    • Send exceptions to for example twitter
  • Improve documentation
  • Call service from entity example

Install package

Caution:

This package is only tested with the hassio addon (https://github.com/hassio-addons/addon-node-red)

Installation

To install this package in Node-Red you can import the following nodes.

It calls a function node each time Node-Red restarts or deploys. The function retrieves the NRPT package and saves the package in the Tools constant. After this node installs IntelliSense data in Node-Red.

[{"id":"aab650160f7d9808","type":"inject","z":"2f4d204a142d188b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":480,"wires":[["ddbfeababe97e8e1"]]},{"id":"ddbfeababe97e8e1","type":"function","z":"2f4d204a142d188b","name":"Install IntelliSense","func":"Tools.InstallIntelliSense.install();","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"Tools","module":"node-red-programming-tools"}],"x":270,"y":480,"wires":[[]]}]

Update package

  • In Home Assistant, navigate to the file editor.
  • In the editor navigate to /config/node-red/package.json and update the version to the desired version number of the NRPT package.
  • Save the file.
  • Restart the Node-Red container to update the package and the IntelliSense.

How to use the package in Node-Red

Initialization of the object

To retrieve the tool object, you can either use the create function or instantiate it yourself.

const tool = Tools.create(this); // Function
const tool = new Tools.Tool(this); // Instantiation

How to handle entity data

Retrieve data of an entity:

const entity = tool.entities.getEntity('sun.sun');

Retrieve multiple entities:

Call with array

tool.entities.getEntities([
    'sun.sun',
    'sensor.hacs'
])

// Outputs: 
Collection([
    {
        key: 'sun.sun',
        entityId: 'sun.sun',
        // Other data
    },
    {
        key: 'sensor.hacs',
        entityId: 'sensor.hacs',
        // Other data
    }
])

// Retrieve an entity
entities.getByKey('sun.sun');

Call with object

tool.entities.getEntities({
    sun: 'sun.sun',
    hacs: 'sensor.hacs'
})

// Outputs: 
const entities = Collection([
    {
        key: 'sun',
        entityId: 'sun.sun',
        // Other data
    },
    {
        key: 'hacs',
        entityId: 'sensor.hacs',
        // Other data
    }
]);

// Retrieve an entity
entities.getByKey('sun');

The collection object is from the package: https://www.npmjs.com/package/collect.js

How to work with outputs

Single call service output:

Connect the function node to a call service node to call a service.

return tool.output
    .callService((cs) => {
        cs
            .setEntityId('switch.switch1')
            .setService(Tools.Enums.Service.Switch.TURN_ON)
    })
    .output();

Get all entities of a certain domain

// Get all entities in een domain "switch"
tool.entities.getEntitiesByDomain('switch');

Get all entities of a certain domain

// Get all entities in domain switch except "switch.switch1".
tool.entities.getEntitiesByDomainExcept('switch', [
    'switch.switch1'
]);

Multiple call service output:

Connect the function node to a call service node to call a service.

return tool.output
    .callMultipleServices((cs) => {
        cs.createPayload((cb) => {
            cb
                .setEntityId('switch.switch1')
                .setService(Tools.Enums.Service.Switch.TURN_ON)
        });
        cs.createPayload((cb) => {
            cb
                .setEntityId('switch.switch2')
                .setService(Tools.Enums.Service.Switch.TOGGLE)
        });
    })
    .output();

How to log data

Log data to the debug console:

tool.logger.log(any);

Log data of an entity:

tool.entities.logEntityData('sun.sun');

How to create a collection

This project heavily relies on the collect.js collection. It is inspired by the Laravel collection and tries to imitate the same behavior in javascript. The following code is an example to create a collection in a Node-Red function node.

Tools.collect([
  {value: 'one'}, 
  {value: 'two'}, 
  {value: 'three'},
]).pluck('value').toArray();

// Output:
['one', 'two', 'three']