Need help with find/change value in non json string

Hello, I have a battery on my balcony PV panels connected. The battery can be manipulated via mqtt. Problem is that I have to sent a string to the battery which isn't json formatted. I tried with change and template node without success.
Below is the string I want to change the number behind v1= against a number I got from a different sensor. The number is responsible for the power which sending from battery to grid.
Any help appreciate!

cd=20,md=0,a1=1,b1=21:10,e1=23:59,v1=300,a2=0,b2=00:00,e2=05:30,v2=123,a3=0,b3=12:30,e3=20:30,v3=123

my flow

[
    {
        "id": "356f9f2a26aea580",
        "type": "function",
        "z": "490a6de6a9a41926",
        "name": "P_Summe",
        "func": "msg.topic = 'P_Summe';\nmsg.payload = msg.payload.Body.Data[\"0\"].PowerReal_P_Sum;\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 530,
        "y": 400,
        "wires": [
            [
                "5eb2acfe03978634"
            ]
        ]
    },
    {
        "id": "c73a59b3c328b111",
        "type": "mqtt in",
        "z": "490a6de6a9a41926",
        "name": "Batterie",
        "topic": "hame_energy/HMJ-2/device/{{uid or mac}}/ctrl/#",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "48a3a12b5a66dd33",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 130,
        "y": 460,
        "wires": [
            [
                "8edfe9e63c69f998"
            ]
        ]
    },
    {
        "id": "8edfe9e63c69f998",
        "type": "string",
        "z": "490a6de6a9a41926",
        "name": "Entladung",
        "methods": [
            {
                "name": "getRightMost",
                "params": [
                    {
                        "type": "str",
                        "value": "lv"
                    }
                ]
            },
            {
                "name": "between",
                "params": [
                    {
                        "type": "str",
                        "value": "="
                    },
                    {
                        "type": "str",
                        "value": ","
                    }
                ]
            }
        ],
        "prop": "payload",
        "propout": "payload",
        "object": "msg",
        "objectout": "msg",
        "x": 360,
        "y": 460,
        "wires": [
            [
                "c939d9184cb2de7e"
            ]
        ]
    },
    {
        "id": "c939d9184cb2de7e",
        "type": "change",
        "z": "490a6de6a9a41926",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "Entladung",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 540,
        "y": 460,
        "wires": [
            [
                "c9bce0cdea0b09e4"
            ]
        ]
    },
    {
        "id": "c9bce0cdea0b09e4",
        "type": "json",
        "z": "490a6de6a9a41926",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 690,
        "y": 460,
        "wires": [
            [
                "a90e96380a9bc69b",
                "0a75aed6e5477ce8"
            ]
        ]
    },
    {
        "id": "a90e96380a9bc69b",
        "type": "join",
        "z": "490a6de6a9a41926",
        "name": "Join",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "useparts": true,
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 890,
        "y": 400,
        "wires": [
            [
                "7eff079fb35aec4e"
            ]
        ]
    },
    {
        "id": "7eff079fb35aec4e",
        "type": "function",
        "z": "490a6de6a9a41926",
        "name": "v1 berechnen",
        "func": "msg.topic = 'change_power';\nmsg.payload = msg.payload.Entladung + msg.payload.P_Summe;\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1060,
        "y": 400,
        "wires": [
            [
                "7dac084efc2ee6a7"
            ]
        ]
    },
    {
        "id": "5eb2acfe03978634",
        "type": "switch",
        "z": "490a6de6a9a41926",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "gte",
                "v": "15",
                "vt": "num"
            },
            {
                "t": "lte",
                "v": "-15",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 690,
        "y": 400,
        "wires": [
            [
                "a90e96380a9bc69b"
            ],
            [
                "0a75aed6e5477ce8"
            ]
        ],
        "info": "Überprüfung ob noch Solar produziert wird. Bei nur positiven Werten liefert die PV Anlage nicht genügend Strom."
    },
    {
        "id": "0a75aed6e5477ce8",
        "type": "join",
        "z": "490a6de6a9a41926",
        "name": "Join",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "useparts": true,
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 890,
        "y": 460,
        "wires": [
            [
                "80446b571a22f01f"
            ]
        ]
    },
    {
        "id": "80446b571a22f01f",
        "type": "function",
        "z": "490a6de6a9a41926",
        "name": "v1 berechnen",
        "func": "msg.topic = 'change_power';\nmsg.payload = msg.payload.Entladung + msg.payload.P_Summe;\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1060,
        "y": 460,
        "wires": [
            [
                "8df63f458728c090"
            ]
        ]
    },
    {
        "id": "3cd49d869df0d59c",
        "type": "function",
        "z": "490a6de6a9a41926",
        "name": "Payload v1",
        "func": "msg.payload = 'cd=20,md=0,a1=1,b1=21:10,e1=23:59,v1=300,a2=0,b2=00:00,e2=05:30,v2=123,a3=0,b3=12:30,e3=20:30,v3=123'\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1490,
        "y": 440,
        "wires": [
            []
        ]
    },
    {
        "id": "7dac084efc2ee6a7",
        "type": "switch",
        "z": "490a6de6a9a41926",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "btwn",
                "v": "80",
                "vt": "num",
                "v2": "800",
                "v2t": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 1210,
        "y": 400,
        "wires": [
            [
                "8435ab95fca04744"
            ]
        ]
    },
    {
        "id": "8df63f458728c090",
        "type": "switch",
        "z": "490a6de6a9a41926",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "btwn",
                "v": "80",
                "vt": "num",
                "v2": "800",
                "v2t": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 1210,
        "y": 460,
        "wires": [
            [
                "8435ab95fca04744"
            ]
        ],
        "info": "Überprüfung ob noch Solar produziert wird. Bei nur positiven Werten liefert die PV Anlage nicht genügend Strom."
    },
    {
        "id": "8435ab95fca04744",
        "type": "json",
        "z": "490a6de6a9a41926",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 1350,
        "y": 440,
        "wires": [
            [
                "3cd49d869df0d59c"
            ]
        ]
    },
    {
        "id": "c6d434a3a78a0a6e",
        "type": "comment",
        "z": "490a6de6a9a41926",
        "name": "",
        "info": "Entladung is node-red-contrib-string\nP_Summe my powermeter sending what is going or comming from grid",
        "x": 340,
        "y": 400,
        "wires": []
    },
    {
        "id": "48a3a12b5a66dd33",
        "type": "mqtt-broker",
        "name": "",
        "broker": "192.168.178.106",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "autoUnsubscribe": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "false",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closeRetain": "false",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]

Hi and welcome to the forum.

It's late here and I am not sure I am going to be much help, but I'll try.
(Oh, sorry I am not going to load the flow. Security and all that.)

What I suggest you do is - I know this will sound painful - try to break it down a bit more.

What you say makes sense to you as you already know a lot about what you are saying.
I'm not too sure.

So get the message and put it into a debug node - set to display entire message. (Default is just the payload)

You seem to have the copy/paste down pat, so I won't go into that.

I'm not understanding the cd=..... line.
What is that? The message received?

So you receive a message .... from .... not sure who.
There is another number you want to put in the message just received at position v1

If the message you received is just text and is like the posted:
cd=20,md=0,a1=1,b1=21:10,e1=23:59,v1=300,a2=0,b2=00:00,e2=05:30,v2=123,a3=0,b3=12:30,e3=20:30,v3=123
Then you would/could feed it into a function node and write some code to do what you want.

Or you could split the message into parts with the split node.
Set the parameters so you get a series of messages.
Each one a different part of the original one.
So you would get messages like:
cd=20
md=0
a1=1
and so on.
Use the switch node to find/see/detect that message and send it to a node which modifies the value and then returns it to the stream.

Then use a join node to remake the original message.
You would set it to MANUAL and specify the number of messages needed to make a new message.

Hope that helps in some way.

Andrew's split would work.

However, you could also use a regular expression (regex).

I can never remember the exact syntax for complex replacements so your best bet is to use an online regex tool.

According to https://regex101.com, this regex will find what you want. It will create a regex variable as well which is the number that you can use in a replace.

/v1=(\d*),/

Here is an example flow:

[{"id":"75e77a60026e1779","type":"inject","z":"b2f18a716bd20f99","name":"","props":[{"p":"payload"},{"p":"newNum","v":"999","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"cd=20,md=0,a1=1,b1=21:10,e1=23:59,v1=300,a2=0,b2=00:00,e2=05:30,v2=123,a3=0,b3=12:30,e3=20:30,v3=123","payloadType":"str","x":310,"y":560,"wires":[["5b4b8ff42e33d353"]]},{"id":"6a414b11ce66a202","type":"debug","z":"b2f18a716bd20f99","name":"debug 37","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":840,"y":560,"wires":[]},{"id":"5b4b8ff42e33d353","type":"change","z":"b2f18a716bd20f99","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$replace(payload, /v1=(\\d*),/, \"v1=\" & newNum & \",\")\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":560,"wires":[["6a414b11ce66a202"]]}]

And a test in the JSONata expression tester:

https://try.jsonata.org/5eI3lkOel

1 Like

@TotallyInformation ,@Trying_to_learn, I'm very sorry to answer so late! I didn't get an email or it was sending to spam :frowning: Thank you very much for your suggestion. I solved it with a string node.

I received the message from the battery as a respond of my sending payload asking the battery what the status is. The battery doesn't sent messages without asking for it. v1 is value for the power which is sending from battery to grid. What I want to do is change the value v1 according to power consumption at my home during the night. I make a calculation with that value and my consumption and next I change v1 when necessary.
I tried it also with split and switch node but couldn't find right adjustment of the nodes or might it be possible that it isn't a json formatted message?

@TotallyInformation I will check your links.
Thank you booth again for your help and really sorry for my late reply.
Cheers

3 Likes

Here is the complete message.