node-red-contrib-boolean-logic-ultimate
Version:
A set of Node-RED enhanced boolean logic and utility nodes, flow interruption, blinker, invert, filter, toggle etc.., with persistent values after reboot. Compatible also with Homeassistant values.
50 lines (37 loc) • 1.26 kB
JavaScript
module.exports = function (RED) {
function StatusUltimate(config) {
RED.nodes.createNode(this, config);
this.config = config;
var node = this;
function setNodeStatus({ fill, shape, text }) {
let dDate = new Date();
node.status({ fill: fill, shape: shape, text: text + " (" + dDate.getDate() + ", " + dDate.toLocaleTimeString() + ")" })
}
setNodeStatus({ fill: "grey", shape: "dot", text: "" });
function fetchFromObject(obj, prop) {
if (obj === undefined) {
return undefined;
}
var _index = prop.indexOf('.')
if (_index > -1) {
return fetchFromObject(obj[prop.substring(0, _index)], prop.substr(_index + 1));
}
return obj[prop];
}
this.on('input', function (msg) {
try {
node.send(msg);
let props = [] = this.config.property.split(".");
let ret = fetchFromObject(msg, this.config.property);
if (ret !== undefined) {
setNodeStatus({ fill: "green", shape: "ring", text: ret.toString() });
} else {
setNodeStatus({ fill: "red", shape: "ring", text: this.config.property + " is undefined." });
}
} catch (error) {
setNodeStatus({ fill: "red", shape: "ring", text: error.message });
}
});
}
RED.nodes.registerType("StatusUltimate", StatusUltimate);
}