Save two different outputs into one message.payload . Status from PID to RC:1

#1

Hello.

Overview of the nodes in my flow: inject node -> run python script with exec node-> save into local file

The output of my python script is current, voltage, power and energy values (every 5 seconds, which means the file will be overwritten every 5 seconds).

My problem is that for some reason, the output of the python script gets separated into two message.payloads one right after the other. Which means my file is overwritten by the second part of the message and this way, only half of the values are being saved. I need all the values in the output of the python script to be saved at once in the local file. How can I do this?

*****EDIT

I have modified the python script so that it only saves the data once (when I press inject), and now the whole message is being passed and downloaded. I don't know why, but under the exec node to run the python script, there is a small yellow square and it says rc:1 , when before it was blue and it said pid with a number next to it. Apart from that, when I inject now, sometimes it works, sometimes it doesn't

0 Likes

#2

Have a look at the join node.

0 Likes

#3

How are you running your python script?
If you look at your python code what is causing the break where it occurs?

0 Likes

#4

I am running it with exec in spawn mode. The Python script outputs everything fine. The problem is when I run it in Node-RED the output, which is a string, gets separated into two message.payloads. I don't know why. Maybe because the string is too long(?)

0 Likes

#5

It probably gets separated into two messages because there's a line return somewhere in the stdout from your script. In spawn mode, the exec node sends a message for each line of output from the command. If you only want one message with the complete output you probably want to use the default exec mode.

From the Exec node info tab:

By default uses the exec system call which calls the command, waits for it to complete, and then returns the output. For example a successful command should have a return code of { code: 0 } .

Optionally can use spawn instead, which returns the output from stdout and stderr as the command runs, usually one line at a time. On completion it then returns an object on the 3rd port. For example, a successful command should return { code: 0 } .

0 Likes

#6

Just a suggestion, what is it your python code does that node-red cannot?

For example, if it something relatively simple I would recommend trying to achieve it without spawning python processes.

For 1, you will better learn the amazing flexibility and capabilities of node-red

Good luck either way.

0 Likes