Payload.illuminance_lux < 2 and payload.presence: true then send ON to tasmota switch (function node not working)

var c = context.get('c') || 0;  // initialize variables
var m = context.get('m') || 0;

if (msg.topic == "illuminance_lux")  // update context based on topic of input
{
    c = {payload: msg.payload};
    context.set("c", c);  // save last value in local context
}

if (msg.topic == 'presence') // same here
{
    m = {payload: msg.payload};
    context.set('m', m); // save last value in local context
}

// now do the test to see if both inputs are triggered...
if (m.payload == true) // check last value of meshstatus first
{
    if (c.payload >2)  // now check last value of checkconf
        return {topic:'value', payload: "ON"};
}
else
    return {topic:'value', payload: "OFF"};

please help I'm a beginner!

Thank you!

You may want to do the conditionals in one if statement. You are also adding extra properties that are not really required

let c = context.get('c') || 0;  // initialize variables
let m = context.get('m') || 0;

if (msg.topic == "illuminance_lux")  // update context based on topic of input
{
    c = msg.payload;
    context.set("c", c);  // save last value in local context
}

if (msg.topic == 'presence') // same here
{
    m = msg.payload;
    context.set('m', m); // save last value in local context
}

// now do the test to see if both inputs are triggered...
if (m == true && c > 2){ // check last value of meshstatus first
    msg.payload = "ON";
}else{
    msg.payload = "OFF";
}
msg.topic = "value";
return msg

EXAMPLE FLOW

[{"id":"aa8c611096dbf55f","type":"inject","z":"febe25a17f3bb5df","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"illuminance_lux","payload":"1","payloadType":"num","x":320,"y":660,"wires":[["5e2d4ed1a1e0cf44"]]},{"id":"5e2d4ed1a1e0cf44","type":"function","z":"febe25a17f3bb5df","name":"function 29","func":"let c = context.get('c') || 0;  // initialize variables\nlet m = context.get('m') || 0;\n\nif (msg.topic == \"illuminance_lux\")  // update context based on topic of input\n{\n    c = msg.payload;\n    context.set(\"c\", c);  // save last value in local context\n}\n\nif (msg.topic == 'presence') // same here\n{\n    m = msg.payload;\n    context.set('m', m); // save last value in local context\n}\n\n// now do the test to see if both inputs are triggered...\nif (m == true && c > 2){ // check last value of meshstatus first\n    msg.payload = \"ON\";\n}else{\n    msg.payload = \"OFF\";\n}\nmsg.topic = \"value\";\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":700,"wires":[["9307b112d42893a0"]]},{"id":"736237b9a92a476c","type":"inject","z":"febe25a17f3bb5df","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"illuminance_lux","payload":"3","payloadType":"num","x":320,"y":700,"wires":[["5e2d4ed1a1e0cf44"]]},{"id":"d2f01b20978d8f4f","type":"inject","z":"febe25a17f3bb5df","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"presence","payload":"1","payloadType":"num","x":300,"y":760,"wires":[["5e2d4ed1a1e0cf44"]]},{"id":"bf1b4599e4e3c70d","type":"inject","z":"febe25a17f3bb5df","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"presence","payload":"0","payloadType":"num","x":300,"y":800,"wires":[["5e2d4ed1a1e0cf44"]]},{"id":"9307b112d42893a0","type":"rbe","z":"febe25a17f3bb5df","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":630,"y":700,"wires":[["5417a38153aa5a09"]]},{"id":"5417a38153aa5a09","type":"debug","z":"febe25a17f3bb5df","name":"debug 291","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":700,"wires":[]}]

(Sorry. I can't read)

Thank you for your help!

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