Nicedevil's Starting Questions

Hey guys,
I started with my homeautomation just today. My Node-Red is up and running and I did a few basic stuffs.
Now I have a little problem where I don't know what I have to google for. Tryed out something like this:

node-red function return string

I can't find any usefull result.

My function looks like this and I want to send "on" or "off" as a string to my mqtt node to turn a switch on and off.

if (msg.payload.activityStatus == "0") {
    return [msg,null];
}
if (msg.payload.activityStatus != "3") {
    return [null,msg];
}

2nd Question, where should I start to not create so much MQTT nodes for one device? Both of these Nodes got different topics :confused:

grafik

Can someone help me?

Your image doesn’t show a function node.

But you should return a string, a function node should return a message object, and generally that object should contain the important info as msg.payload

If you look at the info panel for the MQTT out node it should say that the MQTT node will transmit msg.payload

With your function code, I’m guessing you need something such as the following if you are checking for a value

if (msg.payload.activityStatus == "0") {
    msg.payload = 0
}
else {msg.payload = 1;}
    
return msg;

But for this you could also use the change node

1 Like

The image is for my 2nd problem :slight_smile:

Mhhh to make things a bit clearer:

My Mqtt Device needs on or off on that topic
shellies/shellyplug-s-XXXXX/relay/0/command

I achieve this with a switch from the dashboard that looks like this:

If I create a function node with

if (msg.payload.activityStatus == "0") {
    msg.payload = off
}
else {msg.payload = on;}
    
return msg;

Than there will be an error that tells me that the Function trys to send a string.

Should that be;
msg.payload = "off";
(Same with on)

1 Like

hey Paul,
that did the trick, thank you :slight_smile:
Now my only problem is with the function itself it seems.

This is what my harmony outputs if I hit a button on my remote:
grafik

With this function I always get "on" in the debug log.

if (msg.payload.activityStatus === "0") {
    msg.payload = "off";
}
if (msg.payload.activityStatus !== "0") {
    msg.payload = "on";
}
return msg;

I test this with the inject function like this (i got 2 of them for simulation on / off):
grafik

Looks like this (only 2 images in one post):
grafik

Instead of this:

if (msg.payload.activityStatus === "0") {
   msg.payload = "off";
}
if (msg.payload.activityStatus !== "0") {
   msg.payload = "on"; 
}

Why not use an if...else...?

remember that using === or !== means the value AND type have to match.

tryed out both but doesn't make a difference :confused:

here is the output after hitting inject1 and then inject2, I also added 2 more msg.payload debug nodes to those injectnodes.

grafik

function looks now like this:

if (msg.payload.activityStatus === "0") {
    msg.payload = "off";
    return [msg,null];
}
else {
    msg.payload = "on";
    return [null,msg];
}

Attach a debug to your inject node and see what properties the payload has and then look at your code again

Thats what I told you in my post before?
Here is the output of my injectnode2

grafik

and this from injectnode1

grafik

And what msg.payload property are you testing?

also that should be in the post above your last post :slight_smile:
but here again, I want to check if condition is true or not

if (msg.payload.activityStatus === "0") {
    msg.payload = "off";
    return [msg,null];
}
else {
    msg.payload = "on";
    return [null,msg];
}

Once again, look at the msg.payload property name in the debug node and compare it to what you are using in the if statement

sry I don't understand I guess :confused:
It is an object? Is it that what you mean?
Sry I'm realy new to Node-Red :slight_smile:

ah I think I found what you mean:

if (msg.payload.status == "0") {
    msg.payload = "off";
    return [msg,null];
}
else {
    msg.payload = "on";
    return [null,msg];
}

This is working, "activity" was too much and one "=" was too much as well.

1 Like

If you look at the debug node it shows you have a property in the payload called status. To access it you would use msg.payload.status but what are you using in the if statement? answer: msg.payload.activityStatus which does not exist in the payload.

1 Like

was faster now :smiley:

do you have a good guide for beginners for the function node?
Just basic stuff like how to access those payload things and so on would be nice.

When I have a question I generally do a google search like 'javascript object' or 'javascript comparrison' and then look at the w3school results.

1 Like

yeah that was/is the problem that I sometimes don't know what to startpage for when I start with new things :wink:

Could you help me out with my 2nd question of the initial post?
How to split up topics from one MQTT node that is just linked to the maintopic?

my shelly devices use a topic for every device like shellies/shellyplug-s-7X123X

I thought about to just make one MQTT node to the topic shellies/# and then grab the individual device by there follwing topic with device IDs?

It is a bit huge right now in my flow with just 4 devices :smiley:

actual screenshot:

Use a switch node have one mqtt node subscribing to `shellies/# and in the switch node test msg.topic
for
shellies/thing1 -> outout1
shellies/thing2 -> outout2
shellies/...
shellies/thingn -> outoutn

1 Like