# At commands thru change node

**URL:** https://discourse.nodered.org/t/at-commands-thru-change-node/89618
**Category:** General
**Created:** [19 July 2024 03:37 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618 "2024-07-19T03:37:25Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![jupiter8](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jupiter8](https://discourse.nodered.org/u/jupiter8)
#### Post date: [19 July 2024 03:37 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618/1 "2024-07-19T03:37:25Z")

</div>

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:

```auto
[
    {
        "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"
    }

```

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [19 July 2024 05:11 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618/2 "2024-07-19T05:11:52Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea00a06ae45e5274274d90e26764feca8209602b.png)

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.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [19 July 2024 09:45 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618/3 "2024-07-19T09:45:32Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [19 July 2024 11:21 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618/4 "2024-07-19T11:21:07Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [19 July 2024 18:16 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618/5 "2024-07-19T18:16:01Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [19 July 2024 18:35 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618/6 "2024-07-19T18:35:03Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/6/c69d4ebe3bfe432f2dc6d990988fbb47ee9261e2.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [19 July 2024 21:01 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618/7 "2024-07-19T21:01:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [19 July 2024 22:30 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618/8 "2024-07-19T22:30:45Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 July 2024 06:06 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618/9 "2024-07-20T06:06:47Z")

</div>

> [@mickym2](#):
>
> 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.

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.

---

<div class="post-metadata">

### Author: ![jupiter8](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jupiter8](https://discourse.nodered.org/u/jupiter8)
#### Post date: [20 July 2024 13:30 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618/10 "2024-07-20T13:30:39Z")

</div>

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": [] } ]`

---

<div class="post-metadata">

### Author: ![pmknowles](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pmknowles/32/33880_2.png) [@pmknowles](https://discourse.nodered.org/u/pmknowles)
#### Post date: [23 July 2024 13:21 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618/11 "2024-07-23T13:21:13Z")

</div>

> [@jupiter8](#):
>
> [{ "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

```auto
[
    {
        "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

```auto
[
    {
        "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'

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [6 August 2024 13:21 UTC](https://discourse.nodered.org/t/at-commands-thru-change-node/89618/12 "2024-08-06T13:21:22Z")

</div>

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