Part 2: Creating custom devices in Octoblu
Part 3: Setting the state of an Octoblu device from a flow
Part 4: Listening to and acting on device state change in Octoblu
Part 5: Breaking values into new keys with a function node
Part 6: Reformatting nested JSON with JavaScript
In my last post I left you with some JavaScript to reformat a JSON message and come out with a nice new format.
I left you hanging with my key format though.
Why did I format my key names the way I did?
It is actually pretty simple in concept (but it took me a long time to get all the code right).
Previously I mentioned that after I $set the data on my Octoblu state device, I want to catch that data change in another workflow.
And I also mentioned that logically grouping that data would make it easier to visualize and work with farther down the chain.
So, back to the key name pattern in my output:
{
"msg": {
"rooms.Redmond.lunch.motion": {
"name": "Redmond_lunch_motion",
"mapTitle": "Redmond",
"room": "lunch",
"device": "motion",
},
"rooms.Redmond.lunch.refrigerator": {
"name": "Redmond_lunch_refrigerator",
"mapTitle": "Redmond",
"room": "lunch",
"device": "refrigerator",
},
"rooms.Redmond.lunch.door": {
"name": "Redmond_lunch_door",
"mapTitle": "Redmond",
"room": "lunch",
"device": "door",
}
},
"node": "e271a6c0-9f9b-8d7882b7836a"
}
This is all about the $set.
I want each set of devices grouped under their room which is under the map they correspond to.
Thus: rooms.map.roomName.deviceName
Which is passed to a JSON Template Node in Octoblu which very simply looks like this:
{
"$set": {{msg}}
}
Here is where there are different patterns for referencing the message values in Octoblu.
If you are referencing a blob don't put quotes around the mustache notation like I did above.
If you are referencing a value, then put double quotes around the value like this:
"rooms.{{msg.name}}"
The hard thing to get right is the quotes. Since you will get a false message that your JSON is improperly formatted from the editor, when the message that comes out is actually totally right.
Now, back to why I had the dot notation key name.
When I listen to my state device for a change I will get this nice hierarchy as the output. And I persist my data nice and logically.
{
"msg": {
"rooms": {
"Redmond": {
"lunch": {
"motion": {
"name": "Redmond_lunch_motion",
"mapTitle": "Redmond",
"room": "lunch",
"device": "motion",
},
"refrigerator": {
"name": "Redmond_lunch_refrigerator",
"mapTitle": "Redmond",
"room": "lunch",
"device": "refrigerator",
},
"door": {
"name": "Redmond_lunch_door",
"mapTitle": "Redmond",
"room": "lunch",
"device": "door",
}
}
}
},
"fromUuid": "d5b77d9b-aaf3-f089a7096ee0"
},
"node": "b5149300-9cbd-1f1b56e5d7bb"
}
Next post: I am going to parse all that nesting back apart and make yet another message
No comments:
Post a Comment