Function tried to send a message of type boolean

Hi,

I'm trying to build up a logic to control my ventilator.
Only one of the four switches can be active at one time. So I'm trying to send the message to activate/deactivate the relays with a function.

var L1_on = msg.payload.On === true;
var L1_off = {payload:"stufe1"};
var L2_on = {payload:"stufe2"};
var L2_off = {payload:"stufe2"};
var L3_on = {payload:"stufe3"};
var L3_off = {payload:"stufe3"};
var L4_on = {payload:"stufe4"};
var L4_off = {payload:"stufe4"};

var topic=msg.topic;
if (topic=="stufe1"){
    if(msg.payload.On == true){
    return L1_on;
    } 
} else if (topic=="stufe2"){
    if(msg.payload.On == true){
    return L2_on;
    }
} else if (topic=="stufe3"){
    if(msg.payload.On == true){
    return L3_on;
    }
} else if (topic=="stufe4"){
    if(msg.payload.On == true){
    return L4_on;
    }
}

It would help a lot, if the vars contain the corresponding messages so I can send them as bulk.
I.e.
return [L1_on, L2_off, L3_off, L4_off]
if button 1 is pressed or
return [L1_off, L2_on, L3_off, L4_off]
if button 2 is pressed and so on.

How can I assign the variables a bool message as objects?
I attached a picture of the message I want to store in var L1_on.

Ohne Titel

I tried var L1_on = msg.payload.On === true; but this ends up in an error (Function tried to send a message of type boolean).

Thank you very much!

This will make a Boolean value.

Maybe you meant

var L1_on = {payload: msg.payload.On === true}

This only returns
msg.payload : boolean
true

Is there a way to send the message like defined in the picture without using the change node?

Of course

var L1_on = {
  payload: {
    On:  msg.payload.On === true
  }
}

Though I must point out it is good practice to keep the original msg object (just update its payload) as there may be properties in it that are required down stream.

1 Like

\o/ Thank you! \o/

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