If your flows use the device nodes to get device status from HE to NR, you may have gone through some of the evolution that I have in how to use them. At first I used them strictly to get a single attributes status. This meant that when I cared about multiple statuses like a bulb’s switch state and a bulb’s brightness state, I had 2 separate nodes.
For playing roles in various flows this is often worthwhile, but if you are trying to collect a lot of data (for MQTT or Tables or InfluxDB), this can get quite tiresome and if you get it at NR startup/deploy it can really hammer your HE with requests. Thus, I was excited to start using the all entry for the attributes field of the node. This way I can get all the attribute information in one place.
The drawback I found however is that an all attributes device node sends 2 different formats of information depending if it was triggered or if an event occurred with the device. When triggered it sends an object containing an object for each of the device’s attributes. This is an example for my 2 attribute Floor Lamp Bulb:
Whereas an event triggered output has a payload with an object with details of the attribute that changed. Here is an example of the switch in that bulb changing:
Thus, to get all of this information, I had to set one node to not send events and have it triggerable via injection and set one node to send events but not to have it triggered. As a result a sample flow looked a bit like this:
And since I wanted to put all of the information into my database, I had to build 2 separate processes to handle the 2 types of inputs. The one getting the multi-object input as shown in the first image had to parse out N-number of individual inputs
So I decided what I really wanted was to be able always get single object status/event, but still have access to the multi-object if I ever wanted it. To do this I created a function node that pairs nicely with a device node that can be triggered manually, but that also sends events. Here is the flow from above with that function node:
If anyone is interested here is that function node ready to be imported:
[{"id":"18e80edf.fadfd1","type":"link out","z":"27566e75.091d22","name":"Do stuff with Multi-Attribute Objects","links":[],"x":820,"y":200,"wires":[],"l":true},{"id":"22cbbbf2.21e404","type":"link out","z":"27566e75.091d22","name":"Do stuff with Single Attribute Objects","links":[],"x":830,"y":160,"wires":[],"l":true},{"id":"719c2698.0d8118","type":"function","z":"27566e75.091d22","name":"Split 2 types of device node outputs","func":"var singleAttributeData = false\nvar foundType\nfor (let [key, value] of Object.entries(msg.payload)) {\n foundtype= typeof(value)\n if ((foundtype==\"string\")||(foundtype==\"number\")||(foundtype==\"boolean\")){\n singleAttributeData =true\n }\n}\nif (singleAttributeData){\n node.send([msg,null])\n} else\n{\n node.send([null,msg])\n //interate through the objects in the input\n for (let [key, value] of Object.entries(msg.payload))\n { \n msg.payload = value\n node.send([msg,null])\n node.done()\n \n }\n}\nreturn","outputs":2,"noerr":0,"x":480,"y":160,"wires":[["22cbbbf2.21e404"],["18e80edf.fadfd1"]]},{"id":"65749fae.88073","type":"link in","z":"27566e75.091d22","name":"Get Input from Device Node","links":[],"x":180,"y":160,"wires":[["719c2698.0d8118"]],"l":true}]
Not that anyone is waiting for it with bated breath, but I am also working to redo my earlier post where I shared a few function nodes that I might be helpful if you are sending HE info to MQTT or InfluxDB. I messed up the formatting on the old one and so prevented anyone from using the code.