Solved. Help with JSONata match output

Hi,

Busy trying to learn Node-red. I'm trying to search a message using match and appropriate REGEX. I've got this working however the result is an Object with a group and associated array. How am I able to output the message as a string with the value of the group that matched.

In my example flow below I'm trying to have the output of msg.area = bathroom much as the msg.topic does.

Code
[
    {
        "id": "e9ac906fde06ca7e",
        "type": "inject",
        "z": "92f311dc4ccbcf60",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 220,
        "y": 2200,
        "wires": [
            [
                "05cfb0bb4a03329b"
            ]
        ]
    },
    {
        "id": "05cfb0bb4a03329b",
        "type": "api-current-state",
        "z": "92f311dc4ccbcf60",
        "name": "",
        "server": "2244d24e336a3d2c",
        "version": 3,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "binary_sensor.bathroom_occupancy",
        "state_type": "str",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            },
            {
                "property": "area",
                "propertyType": "msg",
                "value": "{\t   \"area\":$match(data.entity_id,/^[\\w_]+\\.(\\w+)_.*$/)\t}",
                "valueType": "jsonata"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 510,
        "y": 2200,
        "wires": [
            [
                "e8455e8d022d6cf0",
                "e2f6c8a3bfbb0232"
            ]
        ]
    },
    {
        "id": "058dfb0e82cefc9e",
        "type": "debug",
        "z": "92f311dc4ccbcf60",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "topic",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1020,
        "y": 2240,
        "wires": []
    },
    {
        "id": "e8455e8d022d6cf0",
        "type": "debug",
        "z": "92f311dc4ccbcf60",
        "name": "Area",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "area",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 850,
        "y": 2200,
        "wires": []
    },
    {
        "id": "e2f6c8a3bfbb0232",
        "type": "change",
        "z": "92f311dc4ccbcf60",
        "name": "Area",
        "rules": [
            {
                "t": "change",
                "p": "topic",
                "pt": "msg",
                "from": "^[\\w_]+\\.(\\w+)_.*$",
                "fromt": "re",
                "to": "$1",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 830,
        "y": 2240,
        "wires": [
            [
                "058dfb0e82cefc9e"
            ]
        ]
    },
    {
        "id": "2244d24e336a3d2c",
        "type": "server",
        "name": "Home Assistant",
        "version": 2,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true,
        "heartbeat": false,
        "heartbeatInterval": 30
    }
]

Debug output
image

Thanks!

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

Thanks for the reply @zenofmud, I don't think I'm explaining myself properly.

I'd like to change the JSONata expression on the current state node such that it outputs the value of the string that has been matched. Apologies I'm a bit of a novice with all this.

Current expression results in an object where I want it to be a string...?

{
   "area":$match(data.entity_id,/^[\w_]+\.(\w+)_.*$/)
}

Try running it thru the json node

Which converts it to a string, cool thanks. However the string is the full object where I just want the "groups".

Output
area: "{"area":{"match":"binary_sensor.bathroom_occupancy","index":0,"groups":["bathroom"]}}"

Desired Output:
area: "bathroom"

Well if you look at the animation in my first post, you will see how to get the path of anything in the payload. Using that you can build an object with the value using that path

try

{
   "area":$match(data.entity_id,/^[\w_]+\.(\w+)_.*$/).groups[0]
}

Awesome, thanks

@E1cid I just realized it's still outputting as an object. Got a clever way to turn it into a string?

run it thru the json node

I am guessing blind here, as you supply no data to see what is actually outputted.

$match(data.entity_id,/^[\w_]+\.(\w+)_.*$/).groups[0]

Apologies.

Output:
image

I'm trying to get the output of the msg.area to be the value "bathroom" as a string not an object. Thanks again.

Code:

Code
[
    {
        "id": "e9ac906fde06ca7e",
        "type": "inject",
        "z": "92f311dc4ccbcf60",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 160,
        "y": 2200,
        "wires": [
            [
                "05cfb0bb4a03329b"
            ]
        ]
    },
    {
        "id": "05cfb0bb4a03329b",
        "type": "api-current-state",
        "z": "92f311dc4ccbcf60",
        "name": "",
        "server": "2244d24e336a3d2c",
        "version": 3,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "binary_sensor.bathroom_occupancy",
        "state_type": "str",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            },
            {
                "property": "area",
                "propertyType": "msg",
                "value": "{\t   \"area\":$match(data.entity_id,/^[\\w_]+\\.(\\w+)_.*$/).groups[0]\t}",
                "valueType": "jsonata"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 470,
        "y": 2200,
        "wires": [
            [
                "e8455e8d022d6cf0",
                "e2f6c8a3bfbb0232"
            ]
        ]
    },
    {
        "id": "058dfb0e82cefc9e",
        "type": "debug",
        "z": "92f311dc4ccbcf60",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "topic",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1020,
        "y": 2240,
        "wires": []
    },
    {
        "id": "e8455e8d022d6cf0",
        "type": "debug",
        "z": "92f311dc4ccbcf60",
        "name": "Area",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "area",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 930,
        "y": 2200,
        "wires": []
    },
    {
        "id": "e2f6c8a3bfbb0232",
        "type": "change",
        "z": "92f311dc4ccbcf60",
        "name": "Area",
        "rules": [
            {
                "t": "change",
                "p": "topic",
                "pt": "msg",
                "from": "^[\\w_]+\\.(\\w+)_.*$",
                "fromt": "re",
                "to": "$1",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 830,
        "y": 2240,
        "wires": [
            [
                "058dfb0e82cefc9e"
            ]
        ]
    },
    {
        "id": "2244d24e336a3d2c",
        "type": "server",
        "name": "Home Assistant",
        "version": 2,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true,
        "heartbeat": false,
        "heartbeatInterval": 30
    }
]

So my previous should work,
set msg area
to
J: $match(data.entity_id,/^[\w_]+\.(\w+)_.*$/).groups[0]

So good, thanks!


  1. \w_ ↩︎

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