Some useful wee bits of code from my Node-red datalogge

Folks may find this interesting and useful

Uses Hubitat Elevation MakerAPI

Grabs all device details periodically of when poked from Hubitat Elevation

image

As an aside I would really like a Rule Machine trate limiting option. I manage this currently as shown above with a cancelable timer. Does anyone have a better idea?
Also how about allowing a response from an HTTP request to be assigned to a variable in RM.

var msg1 = {};
var devs = [];

var a = {};

var obj = {};
var names = [];
var values = [];

for(j = 0;j < msg.payload.length;j++)
{
    
    var label = msg.payload[j].label;
    var name = msg.payload[j].name;
    var id = msg.payload[j].id;
    a  = msg.payload[j].attributes;
    
    var dev = {};
    dev.id = id;
    dev.label = label;
    dev.name = name;
    dev.attributes = a;
    devs.push( dev );
    

}

msg1.payload = devs;


return msg1;

Then a function for each device for selecting data

var msg1 = {};

var devicelabel = "Outside Heat Index";

msg1.topic = devicelabel;

var obj = {};
var names = [];
var values = [];

for(j = 0;j < msg.payload.length;j++)
{
    //node.warn( msg.payload[j]);
    
    if( msg.payload[j].label == devicelabel)
    {
        obj = msg.payload[j];
                
        var a = obj.attributes;
        
        for(i = 0;i < a.length;i++)
        {
            var val = a[i];
        
            obj[val.name] = (val.currentValue == 999 ? 0 :  val.currentValue);            
            
        }
        
        msg1.payload = obj.attributes;
        msg1.payload.devicelabel = devicelabel;
       
        break;
    }

}


return msg1;

I have a Hubitat Elevation Hub with a mix of Shelly plus various other Arduino, Zigbee Xiaomi and Sonoff devices. No Zwave so far, quite a few polled LAN based Arduino and Shelly devs.

1 Like

dunno if it's just for me, but none of your images are showing...

I see them fine,. Try clearing your browser cache.

Yup, works now, I guess editing the links does wonders :smiley:...

I'm not seeing the cancelable timer. The delay node has a rate limiting option.

Not sure what you mean. Also not sure what RM refers to.

Hi @nickatredbox, welcome to the forum. Thanks for sharing.

In the interest of sharing, here is how I think those 2 functions could be cleaned up and combined into 1...

image

Note: the below is untested & I had to guess the label names - they will need adjusting. Hopefully though it gives you some tips

const reformatted = msg.payload.map(e => {
    return {
        label: e.label,
        name: e.name,
        id: e.id,
        ...e.attributes
    }
})

let msg1, msg2, msg3
const OutsideHeatIndex = reformatted.find(e => e.label === "Outside Heat Index")
const HouseHumidity = reformatted.find(e => e.label === "House Humidity")
const DeviceHeatingPower = reformatted.find(e => e.label === "Device Heating Power")

if (OutsideHeatIndex) {
    msg1 = { payload: OutsideHeatIndex}
}
if (HouseHumidity) {
    msg2 = { payload: HouseHumidity}
}
if (DeviceHeatingPower) {
    msg3 = { payload: DeviceHeatingPower}
}

return [msg1, msg2, msg3];

Some more tips...

  1. Use the monaco editor (default editor in node-red v3.0.2) - it will show you things like undeclared variables
  2. Try to use const and let instead of var
  3. Read up on array.map and array.find functions