Solved: Function and Regex help. Thanks!

I’m trying to pull out the area of an entity in a flow. For example I’ve an entity with the id of binary_sensor.bathroom_occupancy. I’d like to be able to just pull out the bathroom referenced here.

After I’ve got the bathroom set as the topic/variable I then plan to use this to turn on the light group associated with the room. Thanks.

This is vague, providing your flow and how you are trying to use the variable would get a better response.

Here’s a simple example of the flow. Rather than have this same flow for five different rooms (areas). I’m trying to strip out the area value from the entity name to then turn on the appropriate light in that area.

[
    {
        "id": "7e8c20b971e9be1e",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "e2d1d51e1d85446d",
        "type": "server-state-changed",
        "z": "7e8c20b971e9be1e",
        "name": "Bathroom Motion",
        "server": "2244d24e336a3d2c",
        "version": 4,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "binary_sensor.bathroom_occupancy",
        "entityidfiltertype": "exact",
        "outputinitially": false,
        "state_type": "str",
        "haltifstate": "on",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "outputs": 2,
        "output_only_on_state_change": true,
        "for": 0,
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "eventData"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            }
        ],
        "x": 460,
        "y": 280,
        "wires": [
            [
                "529b17eb6a6e860d"
            ],
            [
                "c2bf93062e4d1c77"
            ]
        ]
    },
    {
        "id": "c2bf93062e4d1c77",
        "type": "api-call-service",
        "z": "7e8c20b971e9be1e",
        "name": "Lights Off",
        "server": "2244d24e336a3d2c",
        "version": 5,
        "debugenabled": true,
        "domain": "light",
        "service": "turn_off",
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "light.bathroom"
        ],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "x": 940,
        "y": 280,
        "wires": [
            []
        ]
    },
    {
        "id": "529b17eb6a6e860d",
        "type": "api-call-service",
        "z": "7e8c20b971e9be1e",
        "name": "Lights On",
        "server": "2244d24e336a3d2c",
        "version": 5,
        "debugenabled": true,
        "domain": "light",
        "service": "turn_on",
        "areaId": [],
        "deviceId": [],
        "entityId": [
            "light.bathroom"
        ],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "x": 940,
        "y": 200,
        "wires": [
            []
        ]
    },
    {
        "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
    }
]

It still doesn’t show how you intend to use the area_id. It’s not a simple attribute in NR. Look at these 2 threads for how to get the area_id

^\w+\.(\w+)_.*$

image

[{"id":"0aa7cffd44e087bf","type":"change","z":"91a80f99.6180e","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"^[\\w_]+\\.(\\w+)_.*$","fromt":"re","to":"$1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":768,"wires":[["3420b73640116b19"]]},{"id":"5dcb9ebd255563da","type":"inject","z":"91a80f99.6180e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"binary_sensor.bathroom_occupancy","payloadType":"str","x":318,"y":768,"wires":[["0aa7cffd44e087bf"]]},{"id":"3420b73640116b19","type":"debug","z":"91a80f99.6180e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":768,"wires":[]}]
1 Like

Thanks @Kermit, that looks like what I’m trying to do. Now I just need to work out how to configure the call service node to turn on light.bathroom.

Flow:

Summary
[
    {
        "id": "830c05fefca1c62f",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "f0d54f9b5439283b",
        "type": "inject",
        "z": "830c05fefca1c62f",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 240,
        "y": 240,
        "wires": [
            [
                "c90b780f6aa00fc4"
            ]
        ]
    },
    {
        "id": "c90b780f6aa00fc4",
        "type": "api-current-state",
        "z": "830c05fefca1c62f",
        "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": "triggerId"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 510,
        "y": 240,
        "wires": [
            [
                "093b702a9210a7c2"
            ]
        ]
    },
    {
        "id": "093b702a9210a7c2",
        "type": "change",
        "z": "830c05fefca1c62f",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "^[\\w_]+\\.(\\w+)_.*$",
                "fromt": "re",
                "to": "light.$1",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 840,
        "y": 320,
        "wires": [
            [
                "e6ddcb2e56e2c09f"
            ]
        ]
    },
    {
        "id": "e6ddcb2e56e2c09f",
        "type": "api-call-service",
        "z": "830c05fefca1c62f",
        "name": "",
        "server": "",
        "version": 5,
        "debugenabled": false,
        "domain": "",
        "service": "",
        "areaId": [],
        "deviceId": [],
        "entityId": [],
        "data": "",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [],
        "queue": "none",
        "x": 1070,
        "y": 320,
        "wires": [
            []
        ]
    },
    {
        "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
    }
]

Found the answer. I can use {{payload}} as the entity. Awesome. Thanks!

@Kermit this is working perfectly, thanks again. Now I’m wondering how i can achieve the same result without using the change node. Instead applying the regex search as part of a JSONata expression within the current state node.

Summary
[
    {
        "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": 2020,
        "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": "",
                "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": 2020,
        "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": 980,
        "y": 2000,
        "wires": []
    },
    {
        "id": "e8455e8d022d6cf0",
        "type": "debug",
        "z": "92f311dc4ccbcf60",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "area",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 900,
        "y": 2060,
        "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": 810,
        "y": 2000,
        "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
    }
]

Found the solution with help in the node red forum.

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