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-contrib-state

v1.6.1

Published

Shared state with persistence, notification, and history

Downloads

346

Readme

Shared state with persistence, notification, and history

This contributes Node-RED nodes for defining logical state, sharing that state across nodes, tracking history, and triggering flows based on change.

Shared state with history is persisted, remaining stable across reboots of Node-RED.

Logical State

State associated with a physical device is considered physical state. Logical state are computations such as "Person Is In The Room", usually a combination of physical state and some computation.

Logical state helps with system understanding. "Person Is In The Room" may be the combination of many physical and logical states, and can be used for example, in determining room HVAC profile.

These nodes provide a place to represent that logical state, share it with other nodes, persist it, keep history, and provide state change triggers for flows.

Data Typing

State nodes can specify data types offering inbound type conversions, min/max limiting, and unit of measue awareness and conversion.

Setting State

State is set by sending the value to the setState node in the msg.payload. After saving to disk, the new state is made available to all mechanisms described in the Getting State section below.

If data type is specified, setting state will assure the correct data type is represented.

State nodes with compatible units of measure can be chaned for unit of measure conversion.

Getting State

Once state is set and persisted using the Setting State section above, the new state is made available with the getState node and in the global state context.

The getState Node

The getState node can be dropped onto any flow to trigger a message on initialization, and on state change. The msg.topic contains the state name, the msg.payload contains the state value, and the msg.state object contains an object with the following structure {value:value, prev:prev_value, timestamp:num, history:history, config:config}, where the history object is an array of {val:value, ts=num} objects. Timestamps are milliseconds from the Unix epoch because they serialize nicely, they work well for Date() construction, and they simplify computing durations between timestamps. The config element is the state configuration, containing any metadata defined on the state node such as data type, unit of measure, etc.

Shared State with Global Context

The global context object contains a state element - an object containg the current state and history for all state elements in all flows. This is available for all function and custom nodes needing to use logical state to perform their task.

The keys in the state object are the state names, and the values are the same structure as the msg.state object defined in the getState node above.

An example using the function node:

let isRoomOccupied = global.get("state").isRoomOccupied.value;

Another useful way to obtain shared state is to add it to a message using the Change node:

Shared State Storage

Shared state is saved onto the filesystem, along with history, on each state change. This assures stability across server restarts.

Each state is written to a file in a ./shared-state directory within the current Node-RED application directory. If that isn't a good place to save state on your system, the global context sharedStateDir value can be used to override this default.

This can be placed in the settings.js file under the functionGlobalContext property.

Example settings.js:

  functionGlobalContext: {
    sharedStateDir: '/opt/data/node-red-shared-state'
  },

See the Global Context discussion for further information.

See Also

Installation

  1. Open the Node-RED dashboard
  2. Select the Manage Pallete menu item
  3. Select the Install tab
  4. Enter node-red-contrib-state into the search
  5. Press the install button for this module

License

MIT License. See LICENSE.txt for more details.