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

**URL:** <https://discourse.nodered.org/t/payload-illuminance-lux-2-and-payload-presence-true-then-send-on-to-tasmota-switch-function-node-not-working/78060>\
**Category:** General\
**Created:** [1 May 2023 08:26 UTC](https://discourse.nodered.org/t/payload-illuminance-lux-2-and-payload-presence-true-then-send-on-to-tasmota-switch-function-node-not-working/78060 "2023-05-01T08:26:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![murraa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/murraa/32/59354_2.png) [@murraa](https://discourse.nodered.org/u/murraa)\
**Post date:** [1 May 2023 08:26 UTC](https://discourse.nodered.org/t/payload-illuminance-lux-2-and-payload-presence-true-then-send-on-to-tasmota-switch-function-node-not-working/78060/1 "2023-05-01T08:26:36Z")

</div>

![sc1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/2/9226b6b66031f56771908c16f5465b27bbe6829c.png)

```auto
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!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 May 2023 08:56 UTC](https://discourse.nodered.org/t/payload-illuminance-lux-2-and-payload-presence-true-then-send-on-to-tasmota-switch-function-node-not-working/78060/2 "2023-05-01T08:56:14Z")

</div>

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

```auto
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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 May 2023 09:51 UTC](https://discourse.nodered.org/t/payload-illuminance-lux-2-and-payload-presence-true-then-send-on-to-tasmota-switch-function-node-not-working/78060/4 "2023-05-01T09:51:42Z")

</div>

(Sorry. I can't read)

---

<div class="post-metadata">

**Author:** ![murraa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/murraa/32/59354_2.png) [@murraa](https://discourse.nodered.org/u/murraa)\
**Post date:** [1 May 2023 10:06 UTC](https://discourse.nodered.org/t/payload-illuminance-lux-2-and-payload-presence-true-then-send-on-to-tasmota-switch-function-node-not-working/78060/5 "2023-05-01T10:06:48Z")

</div>

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [15 May 2023 10:06 UTC](https://discourse.nodered.org/t/payload-illuminance-lux-2-and-payload-presence-true-then-send-on-to-tasmota-switch-function-node-not-working/78060/6 "2023-05-15T10:06:49Z")

</div>

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