Python Script into .JSON file for node red?

The problem seems to be in the function node "managing all the AQI'S".
I want to manage all the numbers i get from before ( 1 to 5) to output always the highest one.
I try'd it with the Math.max() function I found but I don't know if it just don't work or if i messed up entirely :smiley:


And i don't know why i get the var max outputed twice, but thats no problem I just wondered :smiley:

Edit: Or does it just don't work because i haven't connected the other parameters? I thought that they are just 0.

EDITEDIT: Nope, i just tried it without them :smiley:

The variables does not look like they will get any values at all

The code in the function node does not look correct. You can add a number of node.warn() statements to check that the values are correct. You should start with that

EDIT: I would expect that you set the variables values using the incoming data. You have to filter on the key in the msg object like:

let IAQ_pm10 = msg.IAQ_pm10;

This is fundamental javascript :wink:

ok I changed the car and the function, now i get NaN, what is that supposed to mean ? :smiley:

Where exactly do i have to put "node.warn()" in ? In the line with the var max or in the return part?

NaN = Not a Number

You can put a node.warn in multiple places so add it in several places to see that the data is at each point.

the max function is still wrong, it calculates on variables that are not defined

Should use IAQ_C, IAQ_CO2 etc etc

Now i get the report that IAQ_pm2 is not defined, but the other debug node say's "IAQ_pm2".

What line in your code - that is not commented out - do you define it?

Maybe you want msg.IAQ_pm2??

I just want one thing. as you can so i recieve in the debug "value control" to numbers. one is called "IAQ_pm2" and the other one is "IAQ_pm10". I just want that always the higher number gets returned. So in this case below, I want that the debug "extracontrol" shows the number "3", because its the higher one.

And I want this procedere with 5 parameters as you can see. But now I can only test it with these two because the other sensors are not connected right now.

So do you have an example how I should code the function node "managing all the AQI's" that I get always the highest number of all 5 inputs?

I already tried that, doens't work.

The function node is now like this.

Math.max takes a list of parameters, not an array.
Get rid of the square brackets.

var max = Math.max(var1, var2)

I tried this. What am I doing wrong?


You have removed the msg. as well as the square brackets.

When i try "msg.IAQ_pm10" i get "NaN".

I thought I should get rid of them ?

If you look at the debug node output you will see that each message only contains one of the values, the other one is not present so you can't take the maximum of the two of them, as only one is available at a time. If these values are coming in from different sources then you need to join the messages together to give you both in one message.

See this article in the cookbook for an example of how to join messages into one object.

Note that you will need the values in msg.payload and each source must have a unique topic. It is almost always more convenient to put values in msg.payload anyway, most nodes expect, by default, the data to be there. If you don't understand what I have just written then I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Also read the node red docs page Working with Messages. Unless you understand what is on that page you will find things very difficult.

1 Like

As with many things in life there are so many alternatives. One is to use context to capture the values from the incoming messages. An example like this I think will work (you have to extend it to cover all your values). Try the code in your function node

if(msg.IAQ_pm2){
    context.set("IAQ_pm2", msg.IAQ_pm2);
}
if(msg.IAQ_pm10){
    context.set("IAQ_pm10", msg.IAQ_pm10);
}

if(context.get("IAQ_pm2") && context.get("IAQ_pm10")){
    let max = Math.max(context.get("IAQ_pm2"), context.get("IAQ_pm10"));    
    msg.payload = max;
    return msg;
}

1 Like

You are the greatest man :smiley: It works perfect.
After I get a good grade for this project I'll send you a present :smiley: Don't know what I would have done without all your help here

Thank you very much for your kind words, it was a pleasure to help! I'm glad it works as you want. Node-RED has so many possibilities to be applied in various projects for various tasks, it's really powerful!!

I hope you succeed with your examination at your university!

Best regards, Walter

hello @krambriw ! :slight_smile:
I'm working now for 2 weeks to connect another measuring device like you showed me. And i feel a little embarrassed to ask you again for your help after all of your work, but I'm not getting any further with it. :frowning:
I changed the VID and PID and all the names in your script for the new device, but after many hours it still doesn't work. (I didn't connect anything with mqtt or node red, I first want to make the script work)
Now I have the thought that maybe the measuring device isn't compatible for this method.
Do you mind to look over the operation manual? I read it 10 times but I don't get any smarter :smiley:

(from page 25 it's in english)

mavolux.txt (3.1 KB)

So what happens when you start the script? Does it find the device, does it connect correctly?
The document you linked to does only describe the product, there is nothing in about how the data is expected to look like

EDIT: I see one major problem, it could very well be why it does'nt work. You have this line:

lux_thread = Thread(target=luxAnalyzer, args=(lux_thread_Event,client,))

As target you have "luxAnalyzer" but here is no such function, it is instead "sdsAnalyzer". So replace sdsAnalyzer with luxAnalyzer (it is in two places in the script) and try then again

Tanks for your answer !
Sorry but I couldn't find anymore about the device :confused:
Ok I will try that ! The reports I got were about this line you mentioned.

I will get in touch.

Thanks :slight_smile:

hello @krambriw
I tried to get the UART communication Protocol but I didn't get an answer yet.
But I found this.

SB_MAVO-USB_GB.pdf (265.9 KB)

on page 3 you can see at the bottom the bit rate and how many data bits are there.
But I still doesn't know which bit has the data I need, I'm working on this to get the information from the manufacturer.

I think I changed all the lines you mentioned in the script. When I start it I get the following.

Screenshot 2021-06-30 124551

Has this something to do with that I don't know which bit contains the right information or is there another problem I didn't see? :confused:

lux.py.txt (3.1 KB)

EDIT: I found out that the device disconnects really fast, maybe this is the problem that the script doesn't detect anything because it's already disconnect?