Send error status message that arrives as malfromed JSON String

I have my 3d printer connected to Home assistant which I use Node Red to do some automations.

I am trying to send a message with the status information from moonraker (Klipper firmware) when there is an error.

I can get the error message in Node red but am having trouble using it. At first glance it looks like JSON but has errors when I parse it.

The Status message is ....

{"code":"key244": "msg":"Extrude only move too long (-3912.506mm vs 2000.000mm)
See the 'max_extrude_only_distance' config option for details", "values":[-3912.506, 2000.000]}

If I send this to a Home Assistant Call Service (Notification) as the Message. I get an error

Call-service error. extra keys not allowed @ data['0']

Ideally I would like to strip the value of message out of this (Extrude only move too long (-3912.506mm vs 2000.000mm) See the 'max_extrude_only_distance' config option for details)

But if I pass the message to a JSON node I get ..

Unexpected token : in JSON at position 16

Any ideas how I can work with this status message? to send it out as a string?

There are two problems with that msg (1) the : between key244 and msg should be a comma.
(2) there is a unescaped new line character in the long string

Thank you for your response. I do appreciate your help.

The problem is I did not create the message. I only receive it from my 3d printer software. So I cant correct it.

I would just like to send it as a text string to a mobile phone notification. So I know why the print has failed.

You should report it to whom ever created the software, maybe they can fix it and publish a new release

Do you receive other messages or just this one?

In the meantime you could use a change node to fix the string. Here is an example using that data (it's in the inject node)

[{"id":"4fd59efb2f3759cb","type":"inject","z":"58d202c52925b8cb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"code\":\"key244\": \"msg\":\"Extrude only move too long (-3912.506mm vs 2000.000mm) See the 'max_extrude_only_distance' config option for details\", \"values\":[-3912.506, 2000.000]}","payloadType":"str","x":130,"y":100,"wires":[["f983334c92632273"]]},{"id":"f983334c92632273","type":"change","z":"58d202c52925b8cb","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"\": \"msg","fromt":"str","to":"\", \"msg","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"\\n","fromt":"str","to":" ","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":160,"wires":[["f49328d84bad9d6d"]]},{"id":"273372bf19b7ccda","type":"debug","z":"58d202c52925b8cb","name":"debug 43","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":140,"y":280,"wires":[]},{"id":"f49328d84bad9d6d","type":"json","z":"58d202c52925b8cb","name":"","property":"payload","action":"","pretty":false,"x":150,"y":220,"wires":[["273372bf19b7ccda"]]}]

Thank you again for your response. I think found in the HA integration it was going wrong. I have found an alternative version that seams to produce valid JSON now. I will use that



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