At commands thru change node

I have this R428A01 relay board wich respond to AT commands. I can switch the relay with commands "AT+O1" and AT+C1" with an inject node no problem, but if i use a change node for say replace 0 with AT+O1 it gives me AT+OAT+C1. Wy is that? I tried all datatype available in the node.
Here is the flow:

[
    {
        "id": "f9783b6bec9f4ca0",
        "type": "inject",
        "z": "f263a8a04f1e1636",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "\"AT+O1\"",
        "payloadType": "str",
        "x": 1200,
        "y": 1360,
        "wires": [
            [
                "66239b4a814b6531"
            ]
        ]
    },
    {
        "id": "8e78d6e23f8c7079",
        "type": "inject",
        "z": "f263a8a04f1e1636",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "\"AT+C1\"",
        "payloadType": "str",
        "x": 1200,
        "y": 1180,
        "wires": [
            [
                "66239b4a814b6531"
            ]
        ]
    },
    {
        "id": "24c297e519e13faa",
        "type": "debug",
        "z": "f263a8a04f1e1636",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1020,
        "y": 1320,
        "wires": []
    },
    {
        "id": "54b0efb64de42987",
        "type": "change",
        "z": "f263a8a04f1e1636",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "0",
                "fromt": "num",
                "to": "AT+O1",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "1",
                "fromt": "str",
                "to": "AT+C1",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1000,
        "y": 1260,
        "wires": [
            [
                "24c297e519e13faa",
                "66239b4a814b6531"
            ]
        ]
    },
    {
        "id": "5c2ec6133bc9287a",
        "type": "inject",
        "z": "f263a8a04f1e1636",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "str",
        "x": 830,
        "y": 1220,
        "wires": [
            [
                "54b0efb64de42987"
            ]
        ]
    },
    {
        "id": "0c14bfd09078cffe",
        "type": "inject",
        "z": "f263a8a04f1e1636",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "1",
        "payloadType": "str",
        "x": 830,
        "y": 1300,
        "wires": [
            [
                "54b0efb64de42987"
            ]
        ]
    },
    {
        "id": "66239b4a814b6531",
        "type": "serial out",
        "z": "f263a8a04f1e1636",
        "name": "",
        "serial": "7fec6f8d91aefaea",
        "x": 1270,
        "y": 1260,
        "wires": []
    },
    {
        "id": "7fec6f8d91aefaea",
        "type": "serial-port",
        "name": "",
        "serialport": "/dev/ttyAMA1",
        "serialbaud": "9600",
        "databits": "8",
        "parity": "none",
        "stopbits": "1",
        "waitfor": "",
        "dtr": "none",
        "rts": "none",
        "cts": "none",
        "dsr": "none",
        "newline": "\\n",
        "bin": "false",
        "out": "char",
        "addchar": "",
        "responsetimeout": "10000"
    }

OK - i see the error is still there with correct datatypes, so you need to change the order of the rules.

It seems that the string "AR+O1" is converted into number 1 automatically, which should not be correct, when searching for a number in the 2nd change rule.
I would raise an issue.

Not really an issue. It is executing all the rules you asked it to. First it replaces the 0 with the AT command with a 1 in. Then it replaces that 1 with the other command. So yes you need to be careful of the order in this case.

i disagree. if i specify to look for a number which should be replaced by a string, i would not expect that a string will match this rule at all.

Replace looks for anything that looks like what you ask for regex style and doesn’t enforce types. So a 1 in a string is seen as a number.

I dont think that this behaviour is good as it is not always possible to solve this issue by reordering the rules.

@jupiter8
If you use regex - then this could be a workaround, if reordering of the rules doesnt work.

I have not followed this in detail, but the switch can test for 'is of type' if that is of any help.

I you use a switch node before, you can directly test if number 0 or 1 and use a following change nodes to set the correct payload. - So there may be a lot of workarounds, but in my opinion it is incorrect if I test in a change node for a datatype - and a string is automatically converted into a number.
In summary there are some workarounds, but the behaviour, if i want to replace a number, I would expect that the datatype is tested within the change node. - In my opinion then the option to look for a number to be replaced within a change node can be removed - if the replacement works only on strings and digits are handeled as chars. (see my workaround to use a regex expression)

In my opinion - if all of you think, that this is the correct behaviour - then it makes no sense to discuss this further. I would expect that if i replace a number, then I expect that the rule does not accept a string.

You may be right that this is not the optimum behavior, but there is no way this is going to be changed now as it would break existing flows.

I ended up using a switch node and set the msg.payload to the desired fonction. Much simpler.[ { "id": "62c8825ca040c1e9", "type": "inject", "z": "f263a8a04f1e1636", "name": "", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "1", "payloadType": "str", "x": 530, "y": 1640, "wires": [ [ "9d0f8565e18e4bad" ] ] }, { "id": "08020a422591615d", "type": "inject", "z": "f263a8a04f1e1636", "name": "", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "0", "payloadType": "str", "x": 530, "y": 1560, "wires": [ [ "9d0f8565e18e4bad" ] ] }, { "id": "9d0f8565e18e4bad", "type": "switch", "z": "f263a8a04f1e1636", "name": "", "property": "payload", "propertyType": "msg", "rules": [ { "t": "eq", "v": "0", "vt": "str" }, { "t": "eq", "v": "1", "vt": "str" } ], "checkall": "true", "repair": false, "outputs": 2, "x": 670, "y": 1600, "wires": [ [ "ef8cffdec4f3beb2" ], [ "36aa6ea18c929404" ] ] }, { "id": "36aa6ea18c929404", "type": "change", "z": "f263a8a04f1e1636", "name": "1 = Stop Pump", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "AT+O1", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 840, "y": 1640, "wires": [ [ "d7ca1498cc2651c2" ] ] }, { "id": "ef8cffdec4f3beb2", "type": "change", "z": "f263a8a04f1e1636", "name": "0 = Start Pump", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "AT+C1", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 840, "y": 1560, "wires": [ [ "d7ca1498cc2651c2" ] ] }, { "id": "d7ca1498cc2651c2", "type": "debug", "z": "f263a8a04f1e1636", "name": "debug 27", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 1040, "y": 1600, "wires": [] } ]

Two things if you change the order it works as expected

[
    {
        "id": "54b0efb64de42987",
        "type": "change",
        "z": "23bf10ad0b45c4f9",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "1",
                "fromt": "str",
                "to": "AT+C1",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "0",
                "fromt": "num",
                "to": "AT+O1",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 340,
        "wires": [
            [
                "24c297e519e13faa"
            ]
        ]
    }
]

A function node also works

[
    {
        "id": "b0add25e0ec0249a",
        "type": "function",
        "z": "23bf10ad0b45c4f9",
        "name": "function 1",
        "func": "if (msg.payload==0) {msg.payload = \"AT+O1\"};\nif (msg.payload==1) {msg.payload = \"AT+C1\"};\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 500,
        "y": 480,
        "wires": [
            [
                "4ab7dfd7789a9773"
            ]
        ]
    }
]

Both of which are 'simpler'

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