# Node-red and logical functions

**URL:** <https://discourse.nodered.org/t/node-red-and-logical-functions/58314>\
**Category:** General\
**Created:** [14 February 2022 13:07 UTC](https://discourse.nodered.org/t/node-red-and-logical-functions/58314 "2022-02-14T13:07:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pilou8830](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@pilou8830](https://discourse.nodered.org/u/pilou8830)\
**Post date:** [14 February 2022 13:07 UTC](https://discourse.nodered.org/t/node-red-and-logical-functions/58314/1 "2022-02-14T13:07:10Z")

</div>

hello, a problem with a function: 🤨  
the goal: when the virtual switch "A" in lovelace is "true" and the motion detector is "true" I have the plug on "on" in other cases on "off" \*  
But nothing comes out of the function? 😭  
can you help me?  
I attach my flow file

 ![Capture400](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f8310196c719de1fc7408df68a803a3633dbf8ad.jpeg)  
 ![Capture401](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/5/a597bf1874c91f8bd3742111d6394f2e6143eafc.jpeg)  
[flows (5).json](https://discourse.nodered.org/uploads/short-url/1Dr18WB8tJgdiAX2Sxthj8evlCw.json) (6.9 KB)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 February 2022 13:22 UTC](https://discourse.nodered.org/t/node-red-and-logical-functions/58314/2 "2022-02-14T13:22:39Z")

</div>

You are comparing apples with oranges. "on" is not equal to **true**

---

<div class="post-metadata">

**Author:** ![pilou8830](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@pilou8830](https://discourse.nodered.org/u/pilou8830)\
**Post date:** [14 February 2022 16:13 UTC](https://discourse.nodered.org/t/node-red-and-logical-functions/58314/3 "2022-02-14T16:13:32Z")

</div>

thank you jbudd  
I changed the "true" to "on" and it works.  
but it does not give a command to the socket?  
I think I should use a "change" but I don't see how.

 ![Capture403](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/e/2e6c479e198d78548de6ddbf83495ccf21f4de18.jpeg)  
🙄

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [14 February 2022 16:16 UTC](https://discourse.nodered.org/t/node-red-and-logical-functions/58314/4 "2022-02-14T16:16:43Z")

</div>

Why not just set msg.payload to true or false instead on 'on' or 'off'

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 February 2022 16:42 UTC](https://discourse.nodered.org/t/node-red-and-logical-functions/58314/5 "2022-02-14T16:42:40Z")

</div>

What mqtt topic does the socket listen for?  
What payload causes it to switch on or off?

My smart socket is flashed with Tasmota and the topic is defined as (for example) "bedroom/light"  
To turn them on I set topic to "cmnd/bedroom/light" and payload to "on"  
That's Tasmota, if yours have different firmware then the topic will probably be different.

---

<div class="post-metadata">

**Author:** ![pilou8830](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@pilou8830](https://discourse.nodered.org/u/pilou8830)\
**Post date:** [14 February 2022 18:54 UTC](https://discourse.nodered.org/t/node-red-and-logical-functions/58314/6 "2022-02-14T18:54:20Z")

</div>

like this?

 ![Capture500](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/5913d11de1097d57c1defb4a4b53518e325e14bf.jpeg)  
it does not change anything the socket does not change state

---

<div class="post-metadata">

**Author:** ![pilou8830](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@pilou8830](https://discourse.nodered.org/u/pilou8830)\
**Post date:** [14 February 2022 19:05 UTC](https://discourse.nodered.org/t/node-red-and-logical-functions/58314/7 "2022-02-14T19:05:09Z")

</div>

it's an IKEA socket that works with my zigbee2mqtt usb key

## Exposes

### [#](https://www.zigbee2mqtt.io/devices/E1603_E1702_E1708.html#switch)Switch

The current state of this switch is in the published state under the `state` property (value is `ON` or `OFF` ). To control this switch publish a message to topic `zigbee2mqtt/FRIENDLY_NAME/set` with payload `{"state": "ON"}` , `{"state": "OFF"}` or `{"state": "TOGGLE"}` . To read the current state of this switch publish a message to topic `zigbee2mqtt/FRIENDLY_NAME/get` with payload `{"state": ""}` .

 ![Capture501](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2ab535486e9432d2d5388762b471e9aae211cc2d.jpeg)

However, I can order it on lovelace.

 ![Capture502](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/8/c8729ed218d19f0a22f715101b8359d5499904a6.jpeg)

---

<div class="post-metadata">

**Author:** ![pilou8830](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@pilou8830](https://discourse.nodered.org/u/pilou8830)\
**Post date:** [14 February 2022 22:05 UTC](https://discourse.nodered.org/t/node-red-and-logical-functions/58314/8 "2022-02-14T22:05:48Z")

</div>

I tried that but it doesn't work either.  
an idea ?

 ![Capture600](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bc67d0393ba4a5de1c81892d7aa21b80e3819973.jpeg)

---

<div class="post-metadata">

**Author:** ![pilou8830](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@pilou8830](https://discourse.nodered.org/u/pilou8830)\
**Post date:** [19 February 2022 10:04 UTC](https://discourse.nodered.org/t/node-red-and-logical-functions/58314/9 "2022-02-19T10:04:05Z")

</div>

it's not very pretty but it works well.

 ![capture_alarme_ok](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/95cd7b3b3b655bf32e7893f5a79a6d264a79b64a.jpeg)

```auto
[{"id":"bf54e42bbb243fab","type":"tab","label":"Flow alarme test","disabled":false,"info":"","env":[]},{"id":"f02add9212c79299","type":"inject","z":"bf54e42bbb243fab","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"switch_on","payloadType":"str","x":100,"y":180,"wires":[["5e6cc9945dfb26d8"]]},{"id":"e575a5d3ac10195a","type":"inject","z":"bf54e42bbb243fab","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"switch_off","payloadType":"str","x":100,"y":220,"wires":[["5e6cc9945dfb26d8"]]},{"id":"b8013777e540be29","type":"inject","z":"bf54e42bbb243fab","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"pir_on","payloadType":"str","x":90,"y":320,"wires":[["4c464465cae112a5"]]},{"id":"a669b618d825ac6d","type":"inject","z":"bf54e42bbb243fab","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"pir_off","payloadType":"str","x":90,"y":360,"wires":[["4c464465cae112a5"]]},{"id":"276a7239eeac557a","type":"function","z":"bf54e42bbb243fab","name":"","func":"let sensors = msg.payload; //don't need to write msg.payload to acces sensors\n\n//Door open and window not false, or garage 0\nif (sensors.alarme === \"switch_on\" && sensors.pir_hall === \"pir_on\")\n { return [msg, null]; }\n \n else {\n return [null, msg]; //Send message to second output\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":280,"wires":[["3bf66e090680aed5"],["1e0a2a1a3bf29a86"]]},{"id":"5e6cc9945dfb26d8","type":"change","z":"bf54e42bbb243fab","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"alarme","tot":"str"},{"t":"set","p":"alarme","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":200,"wires":[["23b715a59369d6e5"]]},{"id":"4c464465cae112a5","type":"change","z":"bf54e42bbb243fab","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"pir_hall","tot":"str"},{"t":"set","p":"pir_hall","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":340,"wires":[["23b715a59369d6e5"]]},{"id":"23b715a59369d6e5","type":"join","z":"bf54e42bbb243fab","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":470,"y":280,"wires":[["276a7239eeac557a"]]},{"id":"c42d5047228c82a7","type":"debug","z":"bf54e42bbb243fab","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":80,"wires":[]},{"id":"98508b4f7c1d2c68","type":"debug","z":"bf54e42bbb243fab","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":440,"wires":[]},{"id":"c11ccd637234e10a","type":"inject","z":"bf54e42bbb243fab","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":640,"wires":[["701ce8e588a465bd"]]},{"id":"701ce8e588a465bd","type":"function","z":"bf54e42bbb243fab","name":"","func":"let sensors = {\n door: global.get(\"door\"),\n garage: global.get(\"garage\"),\n window: global.get(\"window\")\n}\n\n//Door open and window not false, or garage 0\nif ( (sensors.door === \"close\" && !sensors.window) || sensors.garage === 0){\n return [msg, null]; //Send message to first output\n}else if (sensors.garage === 1){\n return [null, msg]; //Send message to second output\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":640,"wires":[["723ffc4aa03623cd"],["efd81456e2377e5f"]]},{"id":"723ffc4aa03623cd","type":"debug","z":"bf54e42bbb243fab","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":600,"wires":[]},{"id":"efd81456e2377e5f","type":"debug","z":"bf54e42bbb243fab","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":680,"wires":[]},{"id":"21f33a4dd9d78bc4","type":"comment","z":"bf54e42bbb243fab","name":"Sensor check on external event","info":"","x":630,"y":640,"wires":[]},{"id":"3bf66e090680aed5","type":"change","z":"bf54e42bbb243fab","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":180,"wires":[["c42d5047228c82a7","543e61eb5a75e7d5"]]},{"id":"1e0a2a1a3bf29a86","type":"change","z":"bf54e42bbb243fab","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":360,"wires":[["98508b4f7c1d2c68","0f498f62b38e18fe"]]},{"id":"543e61eb5a75e7d5","type":"api-call-service","z":"bf54e42bbb243fab","name":"Prise ON","server":"d260f8f066987915","version":5,"debugenabled":false,"domain":"switch","service":"turn_on","areaId":[],"deviceId":[],"entityId":["switch.prise_1"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":760,"y":40,"wires":[[]]},{"id":"0f498f62b38e18fe","type":"api-call-service","z":"bf54e42bbb243fab","name":"Prise off","server":"d260f8f066987915","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":["hall"],"deviceId":[],"entityId":["switch.prise_1"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":800,"y":300,"wires":[[]]},{"id":"65bdaf8a00c82f91","type":"server-state-changed","z":"bf54e42bbb243fab","name":"presence Hall","server":"d260f8f066987915","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.pir_hall_occupancy","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":90,"y":100,"wires":[["5934e1a664547d86"]]},{"id":"b882e4912395ce85","type":"server-state-changed","z":"bf54e42bbb243fab","name":"alarme on/off","server":"d260f8f066987915","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.alarme","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":90,"y":480,"wires":[["d2f93990fa908a59"]]},{"id":"d2f93990fa908a59","type":"change","z":"bf54e42bbb243fab","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"pir_on","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"pir_off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":420,"wires":[["4c464465cae112a5"]]},{"id":"5934e1a664547d86","type":"change","z":"bf54e42bbb243fab","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"switch_on","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"switch_off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":120,"wires":[["5e6cc9945dfb26d8"]]},{"id":"d260f8f066987915","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

```

---

<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:** [5 March 2022 10:04 UTC](https://discourse.nodered.org/t/node-red-and-logical-functions/58314/10 "2022-03-05T10:04:20Z")

</div>

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