Node Red/Hubitat - Feedback requested (Long Read)

Hello - I've been playing around with Node Red and wanted to get feedback from more experienced people! I'm new to Hubitat and Node Red so please be gentle :grinning:

Background: I was having a hard time trying to follow the Hubitat native logs as they were getting spammed by entries from Zooz ZEN25 Double Plug and the Aeotec Multisensor6. The logs would not go back more than a day and thus were not very useful. I leveraged examples that I saw on found here and other places.

Basic Logic: Listen for events published by the MakerAPI, filter out events that I don't want to log (e.g. illuminance, temperature, power etc.), build the SQL statement and create an entry in a MySQL database. I created a bunch of virtual switches that get turned on/off by the automations and I use those the naming convention of "Virtual..." to log an automation event (I could not find a way to capture when an app executed, hence this workaround).


Script for filtering events:
//Create an array of events to be filtered out

   var filterEvents =    ['power','current','voltage','energy','illuminance','temperature','lastActivity','humidity','energyDuration']; // build the array of events to filter

if(filterEvents.indexOf(msg.payload.name)!== -1){ // check if published event needs to be filtered
    msg = null //null should stop the flow
}
return msg;

Script for creating the SQL statement;
//Assign value to temp to build query
//
var q_source ="'"+ "events"+"',"; //default source is events

if(msg.payload.displayName.indexOf('Virtual')!== -1){ // check if this is an automation (requires virtual device with name "Virual...")
q_source = "'"+ "automation"+"',"
}

var q_displayName = msg.payload['displayName'] === undefined? "null,":"'"+msg.payload['displayName']+"',";
var q_name = "'"+msg.payload['name']+ "',";
var q_value = "'"+msg.payload['value']+ "',";
var q_unit = "'"+msg.payload['unit'] + "',";
var q_deviceId =msg.payload['deviceId']===undefined||msg.payload['deviceId']===null?"null,": "'"+msg.payload['deviceId']+"',";
var q_hubId =msg.payload['hubId']===undefined||msg.payload['hubId']===null?"null,": "'"+msg.payload['hubId']+"',";
var q_locationId =msg.payload['locationId']===undefined?"null,": "'"+msg.payload['locationId']+"',";
var q_installedAppId =msg.payload['installedAppId']===undefined?"null,":"'"+msg.payload['installedAppId']+"',";
var q_descriptionText =msg.payload['descriptionText']===undefined?"null,":"'"+msg.payload['descriptionText']+"'";

//Build the query
//
var query = "INSERT INTO events (source, name, displayName, value, unit, deviceId, hubId, locationId, installedAppId, descriptionText)";
query+= " VALUES ("+q_source+q_name+q_displayName+q_value+q_unit+q_deviceId+q_hubId+q_locationId+q_installedAppId+q_descriptionText+")";
msg.topic = query;
return msg;

Actual Database Entries:

Questions/feedback requests:

  1. Areas for improvement? I haven't built in any error handling as yet, so any suggestions on how to handle that would be great

  2. Is returning "null" if I want to stop the flow (filter script) an acceptable way? I tried to use a switch with no connector on one output, but I wasn't sure if that was the right way either.

  3. Is there any downside to creating virtual switches? I don't expect to have a ton of devices (as yet!) - just need to move two door locks (Kwikset Z-Wave) and a garage door (GoControl) over, so will end up with about 25 or so actual devices and about 15-20 virtual devices.

Thanks for your help.

I'd go put this in the big node red thread.

I don't see anything wrong with what you did. I would likely have just included return (followed by nothing) and put return message in an else, but I don't think there is anything wrong with either approach. If you aren't sure you can always attach a debug node to the output and see what is coming out.

I will defer to your design on the validity of the SQL, but I assume that it is working in your testing.

Regarding error handling, this is something I should do more of, but for something this simple it may not matter unless you view the info as so vital that you want to jump through lots of hoops if something goes wrong.

Finally, I use a fair number of function nodes myself, but there are many that would point out that everything that you have done here can be done with standard nodes. I wouldn't bother figuring out exactly how, but I think it would be split, switch, join, change nodes to do what you did with those 2 function nodes.

Regarding virtual devices, I think that is really an issue of personal preference until the number of devices gets very large.

Is there any way to connect this to that thread or should I just double post? Haven't tried the "hyperlink" option, maybe that would work?

I just copy the address from the address bar and paste it in the other thread.