Custom Logging/Payload Message

So I'm pretty sure I need a function node for what I'm looking for... however I'm clueless when it comes to writing javascript. I've attempted to read several different tutorials, however nothing is really "clicking." I'm also not convinced I'm reading the correct tutorials...

What I'm after is changing the event message that gets sent, taking that changed message and logging it. The last part I can figure out. It's the capture and changing of the message that is stumping me.

When my zigbee lock gets unlocked this message gets sent through node-red:

{"action":"lock","action_source":1,"action_source_name":"rf","action_user":65535,"battery":100,"last_seen":"2022-02-11T15:14:34-07:00","linkquality":123,"lock_state":"locked","state":"LOCK"}

or

{"action":"manual_unlock","action_source":2,"action_source_name":"manual","action_user":65535,"battery":100,"last_seen":"2022-02-11T15:21:45-07:00","linkquality":116,"lock_state":"unlocked","state":"UNLOCK"}

And I want to change that to a simple "Jim (or Dana, Karen, etc based on action_user) locked the front door." to keep an easily readable log along with a timestamp. I imagine a function node would be easier than a grip of switch and change nodes...

I appreciate any guidance anyone can provide!

ETA: Also, if anyone has a better idea than simple file logging, I'd like to hear it!

I assume this is a home system so you will not be getting a huge number of locks and unlocks each day ?

If so i would start with simple file logging.

If you do not know any JS then i would suggest using the built in nodes to start with

Switch node to switch on the User

Then change node on each of the outputs of the switch node to set a value on the message along the lines of msg.username, you might want to pull out some of the other stuff like Lock_state etc - again you can do these with a change node or with more switch nodes

Then you can use one of the advanced logging nodes -

node-red-contrib-flogger is the one i use - this gives you lots of flexibility.

once you have all this down and running you can decide if you want to simplify/streamline it be writing some JS functions or moving some of it into subflows etc.

its nearly always easiest to start off with the inbuilt nodes and then ask for help than getting someone to write JS for you that you do not understand

Craig

Here is my solution for you - without a function node - and using a translation object as flow variable.
You can expand this object to your own needs.

With an inject node you can initialize a flow variable like this:

image

So I used both of your zigbee messages to generate the following output:

You can use other verbs for your actions as well if you have understood how the translation worked.

Zusammenfassung
[
    {
        "id": "4fdc2bdca68946e6",
        "type": "inject",
        "z": "ebe8134f6b5af26c",
        "name": "Init Translation Table",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"user\":[{\"id\":65535,\"name\":\"Jim\"},{\"id\":65534,\"name\":\"Dana\"}],\"door\":[{\"id\":1,\"name\":\"front door\"},{\"id\":2,\"name\":\"rear door\"}]}",
        "payloadType": "json",
        "x": 620,
        "y": 760,
        "wires": [
            [
                "cac703906013f669"
            ]
        ]
    },
    {
        "id": "cac703906013f669",
        "type": "change",
        "z": "ebe8134f6b5af26c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "translate",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 870,
        "y": 760,
        "wires": [
            []
        ]
    },
    {
        "id": "14d9297a0fc09095",
        "type": "inject",
        "z": "ebe8134f6b5af26c",
        "name": "lock msg",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"action\":\"lock\",\"action_source\":1,\"action_source_name\":\"rf\",\"action_user\":65535,\"battery\":100,\"last_seen\":\"2022-02-11T15:14:34-07:00\",\"linkquality\":123,\"lock_state\":\"locked\",\"state\":\"LOCK\"}",
        "payloadType": "json",
        "x": 340,
        "y": 840,
        "wires": [
            [
                "cb9f2f6678de487d"
            ]
        ]
    },
    {
        "id": "b1d29236ac323952",
        "type": "inject",
        "z": "ebe8134f6b5af26c",
        "name": "unlock msg",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"action\":\"manual_unlock\",\"action_source\":2,\"action_source_name\":\"manual\",\"action_user\":65535,\"battery\":100,\"last_seen\":\"2022-02-11T15:21:45-07:00\",\"linkquality\":116,\"lock_state\":\"unlocked\",\"state\":\"UNLOCK\"}",
        "payloadType": "json",
        "x": 330,
        "y": 880,
        "wires": [
            [
                "cb9f2f6678de487d"
            ]
        ]
    },
    {
        "id": "cb9f2f6678de487d",
        "type": "change",
        "z": "ebe8134f6b5af26c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "user",
                "pt": "msg",
                "to": "$flowContext(\"translate\").user[id=$$.payload.action_user].name\t",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "door",
                "pt": "msg",
                "to": "$flowContext(\"translate\").door[id=$$.payload.action_source].name\t",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "action",
                "pt": "msg",
                "to": "payload.action",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "ts",
                "pt": "msg",
                "to": "$now()\t",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 580,
        "y": 860,
        "wires": [
            [
                "9cce8da04e108079"
            ]
        ]
    },
    {
        "id": "0d73cc92bc38ee94",
        "type": "debug",
        "z": "ebe8134f6b5af26c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 950,
        "y": 860,
        "wires": []
    },
    {
        "id": "9cce8da04e108079",
        "type": "template",
        "z": "ebe8134f6b5af26c",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "{{ts}}: {{user}} {{action}}ed {{door}}!",
        "output": "str",
        "x": 780,
        "y": 860,
        "wires": [
            [
                "0d73cc92bc38ee94"
            ]
        ]
    }
]

The output can be included in the change node as well. With the template node it is only a little bit easier to change the output to your needs.

One of the fascinating possibilities of JSONATA is to query a property of an object and return another one. If this becomes clear - you can use this mechanism for many use cases.

Is that coming through mqtt? If so then make sure that you have set the output to parsed JSON. Then it will output a javascript object rather than a JSON string.

It is, yes. It's what's coming out of the zigbee2mqtt node. Not sure where to make that setting...

It's ugly... but it works. I went with switch nodes and templates... probably cheated using this method. Then passed on to flogger.

[
    {
        "id": "d435d2ee9237403d",
        "type": "comment",
        "z": "ee52bb7bfcba9876",
        "name": "Logging",
        "info": "",
        "x": 70,
        "y": 1040,
        "wires": []
    },
    {
        "id": "036f0ed434226417",
        "type": "link in",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "links": [
            "8ad931818f7bd773"
        ],
        "x": 55,
        "y": 1080,
        "wires": [
            [
                "d28eafbb1be9397b"
            ]
        ]
    },
    {
        "id": "d28eafbb1be9397b",
        "type": "switch",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "property": "payload.action",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "unlock_failure_invalid_pin_or_id",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "manual_unlock",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "manual_lock",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "pin_code_added",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "pin_code_deleted",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "one_touch_lock",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "lock",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "unlock",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 8,
        "x": 110,
        "y": 1160,
        "wires": [
            [
                "c7d09878c8b2655a"
            ],
            [
                "b83de959ce2e3096"
            ],
            [
                "832e0d1e186fdb9b"
            ],
            [
                "bd8394c7391fb0f2"
            ],
            [
                "c7c5a31eb4d8a33d"
            ],
            [
                "f8271497af1152ea"
            ],
            [
                "3c7401b7c27ed087"
            ],
            [
                "fe5818340782e416"
            ]
        ]
    },
    {
        "id": "4a1983d9.a7083c",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "Zigbee locked the front door.",
        "output": "str",
        "x": 300,
        "y": 1340,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "c7d09878c8b2655a",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "ALERT: INVALID PIN ENTERED",
        "output": "str",
        "x": 300,
        "y": 1100,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "b83de959ce2e3096",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "Front door manually unlocked.",
        "output": "str",
        "x": 300,
        "y": 1140,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "832e0d1e186fdb9b",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "Front door manually locked.",
        "output": "str",
        "x": 300,
        "y": 1180,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "bd8394c7391fb0f2",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "PIN Code Added",
        "output": "str",
        "x": 300,
        "y": 1220,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "c7c5a31eb4d8a33d",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "PIN Code Deleted",
        "output": "str",
        "x": 300,
        "y": 1260,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "f8271497af1152ea",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "Front Door locked by one-touch.",
        "output": "str",
        "x": 300,
        "y": 1300,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "3c7401b7c27ed087",
        "type": "switch",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "property": "payload.action_user",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "65535",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 110,
        "y": 1280,
        "wires": [
            [
                "4a1983d9.a7083c"
            ]
        ]
    },
    {
        "id": "fe5818340782e416",
        "type": "switch",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "property": "payload.action_user",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "65535",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "2",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "3",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 4,
        "x": 110,
        "y": 1380,
        "wires": [
            [
                "269b48695bb1069c"
            ],
            [
                "3cb0d43170fbe863"
            ],
            [
                "0b3102def0ccf320"
            ],
            [
                "1931e733124ec042"
            ]
        ]
    },
    {
        "id": "269b48695bb1069c",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "Zigbee unlocked the front door.",
        "output": "str",
        "x": 300,
        "y": 1380,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "3cb0d43170fbe863",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "Mike unlocked the front door.",
        "output": "str",
        "x": 300,
        "y": 1420,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "0b3102def0ccf320",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "Bob unlocked the front door.",
        "output": "str",
        "x": 300,
        "y": 1460,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "1931e733124ec042",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "Jim unlocked the front door.",
        "output": "str",
        "x": 300,
        "y": 1500,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "9a9e4b590ca7a015",
        "type": "switch",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "property": "payload.On",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "true",
                "vt": "jsonata"
            },
            {
                "t": "eq",
                "v": "false",
                "vt": "jsonata"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 110,
        "y": 1480,
        "wires": [
            [
                "285a8a7903bc7a31"
            ],
            [
                "411bc43cbacc1109"
            ]
        ]
    },
    {
        "id": "f03bc264d84a4c87",
        "type": "link in",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "links": [
            "5ef842d8c5e5c82a"
        ],
        "x": 55,
        "y": 1440,
        "wires": [
            [
                "9a9e4b590ca7a015"
            ]
        ]
    },
    {
        "id": "285a8a7903bc7a31",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "Front door open.",
        "output": "str",
        "x": 300,
        "y": 1540,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "411bc43cbacc1109",
        "type": "template",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "Front door closed.",
        "output": "str",
        "x": 300,
        "y": 1580,
        "wires": [
            [
                "cbb8bfca7317824b"
            ]
        ]
    },
    {
        "id": "cbb8bfca7317824b",
        "type": "rbe",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "func": "rbe",
        "gap": "",
        "start": "",
        "inout": "out",
        "septopics": true,
        "property": "payload",
        "topi": "topic",
        "x": 550,
        "y": 1360,
        "wires": [
            [
                "759859cadbd7c747"
            ]
        ]
    },
    {
        "id": "759859cadbd7c747",
        "type": "flogger",
        "z": "ee52bb7bfcba9876",
        "name": "",
        "logfile": "front-door",
        "inputchoice": "object",
        "inputobject": "payload",
        "inputobjectType": "msg",
        "inputmoustache": "Recieved payload {{payload}} and topic {{topic}}",
        "loglevel": "INFO",
        "logconfig": "0ae80f941e5e6f3e",
        "sendpane": true,
        "x": 550,
        "y": 1420,
        "wires": [
            []
        ]
    },
    {
        "id": "0ae80f941e5e6f3e",
        "type": "config-log",
        "logname": "front-door",
        "logdir": "logs",
        "stamp": "local",
        "logstyle": "plain",
        "logrotate": true,
        "logcompress": true,
        "logrotatecount": "5",
        "logsize": "1000"
    }
]

I don't use that node, I keep it simple by using MQTT nodes instead. It may be the default in that node

Finally figured out a working function... somehow!

var action = msg.payload.action;
var action_user = msg.payload.action_user;
var user;

if(action_user == "65535") {
    user = "Zigbee";
} else if(action_user == "1") {
    user = "Jim";
} else if(action_user == "2") {
    user = "Bob";
} else if(action_user == "3") {
    user = "Jimbob";
}

if(action == "unlock_failure_invalid_pin_or_id") {
    message = "ALERT: INVALID PIN ENTERED";
} else if(action == "manual_unlock") {
    message = "Front door manually unlocked.";
} else if(action == "manual_lock") {
    message = "Front door manually locked.";
} else if(action == "pin_code_added") {
    message = "PIN Code Added";
} else if(action == "pin_code_deleted") {
    message = "PIN Code Deleted";
} else if(action == "one_touch_lock") {
    message = "Front door locked by one-touch.";
} else if(action == "lock") {
    message = user + " " + action + "ed the front door.";
} else if(action == "unlock") {
    message = user + " " + action + "ed the front door.";
}

msg.payload = message;

return msg;

Works wonderfully! and much cleaner on the flow lol

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.