# Switch node not working for string comparison with 'on'/'off' values

**URL:** <https://discourse.nodered.org/t/switch-node-not-working-for-string-comparison-with-on-off-values/78222>\
**Category:** General\
**Tags:** function-node, switch-node\
**Created:** [6 May 2023 11:05 UTC](https://discourse.nodered.org/t/switch-node-not-working-for-string-comparison-with-on-off-values/78222 "2023-05-06T11:05:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronyn77](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronyn77/32/73254_2.png) [@ronyn77](https://discourse.nodered.org/u/ronyn77)\
**Post date:** [6 May 2023 11:05 UTC](https://discourse.nodered.org/t/switch-node-not-working-for-string-comparison-with-on-off-values/78222/1 "2023-05-06T11:05:49Z")

</div>

I am trying to use the switch node after a current state node to check whether the device is turned on or off, and based on that value, perform a subsequent operation. Inside the switch node, I am doing a direct comparison to see if the value matches a string with "on" or "off". However, it's not working, and I can't understand why. I'm forced to create a custom function where I use an "if" statement and the "===" operator for comparison. This way, it works. Is it possible to do the same comparison in the switch node? How can I avoid using a custom function?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 May 2023 11:24 UTC](https://discourse.nodered.org/t/switch-node-not-working-for-string-comparison-with-on-off-values/78222/2 "2023-05-06T11:24:34Z")

</div>

Show us what is going into the Switch node (using a debug node) and how you have configured the node. We are not telepathic so it is impossible to know what is wrong if you do not show us what you have tried.

---

<div class="post-metadata">

**Author:** ![ronyn77](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronyn77/32/73254_2.png) [@ronyn77](https://discourse.nodered.org/u/ronyn77)\
**Post date:** [6 May 2023 17:42 UTC](https://discourse.nodered.org/t/switch-node-not-working-for-string-comparison-with-on-off-values/78222/3 "2023-05-06T17:42:21Z")

</div>

Here it is the code for this flow : [Test - Pastebin.com](https://pastebin.com/8wLw0Bc2)

Could you take a look? You can change the device with any other you have.  
this is what is going inside the switch node.

```auto
{"_msgid":"8f65cfa7583d532e","payload":"on","topic":"","data":{"entity_id":"media_player.webos_tv","state":"on","attributes":{"source_list":["Accessibilità","AirPlay","All 4","Apple TV","Attività","BBC iPlayer","Browser Web","Canali TV","Configurare la TV per Amazon Alexa","Configurare la TV per Assistente Google","Connettore dispositivo","Developer Mode","Foto e video","GALLERIA","Google","Guida TV","HDMI ARC","IPTV Smarters Player","ITVX","LG Channels","LG Content Store","MKIPTV","Manuale d'uso","Multi-view","Musica","My5","NOW","Netflix","Notifiche","PC","Prime Video","Rakuten TV","Registrazioni","Riproduzione audio Bluetooth","Screen Share","Servizio remoto LG","Smart IPTV","Smart STB","TV Cast","VOYO","YouTube"],"volume_level":0.02,"is_volume_muted":false,"source":"PC","sound_output":"external_arc","assumed_state":true,"device_class":"tv","entity_picture":"/api/media_player_proxy/media_player.webos_tv?token=6318c843b84b8041d8b61c1c34479287694ff47332534bf1994baaff304da638&cache=9ab22af5828b3226","friendly_name":"webOS TV","supported_features":24377},"context":{"id":"01GZRZC0EBR0MPZ4DHKN80QWE8","parent_id":null,"user_id":null},"last_changed":"2023-05-06T10:42:39.091Z","last_updated":"2023-05-06T16:47:24.107Z","timeSinceChangedMs":25045338}}

```

I check if payload is on or off.

---

<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:** [6 May 2023 18:07 UTC](https://discourse.nodered.org/t/switch-node-not-working-for-string-comparison-with-on-off-values/78222/4 "2023-05-06T18:07:08Z")

</div>

Typo in switch node property field `paylod`

---

<div class="post-metadata">

**Author:** ![ronyn77](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronyn77/32/73254_2.png) [@ronyn77](https://discourse.nodered.org/u/ronyn77)\
**Post date:** [6 May 2023 20:18 UTC](https://discourse.nodered.org/t/switch-node-not-working-for-string-comparison-with-on-off-values/78222/5 "2023-05-06T20:18:44Z")

</div>

Really? Oh my gosh…I am blind…thank you!

---

<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 July 2023 20:18 UTC](https://discourse.nodered.org/t/switch-node-not-working-for-string-comparison-with-on-off-values/78222/6 "2023-07-05T20:18:56Z")

</div>

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