# Msg.topic in ui-switch node

**URL:** https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048
**Category:** General
**Tags:** dashboard-2
**Created:** [28 June 2024 14:57 UTC](https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048 "2024-06-28T14:57:48Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Gaetano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gaetano/32/81395_2.png) [@Gaetano](https://discourse.nodered.org/u/Gaetano)
#### Post date: [28 June 2024 14:57 UTC](https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048/1 "2024-06-28T14:57:48Z")

</div>

I use native NodeRed (4.0) on a Raspberry PI 4.0b. I'm converting my flows from dashboard 1 to dashboard 2 (1.12.2).  
I've seen that on a node ui-switch, I cannot use the "topic" writing a string that must reach MQTT. The string is "cmnd/tasmota\_cucina/POWER".  
If I write a string on that field I get the "Error: invalid topic".  
Is it a my mistake?

---

<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: [28 June 2024 15:06 UTC](https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048/2 "2024-06-28T15:06:48Z")

</div>

Have you selected string as the type input for topic field. Show us how it is configured.  
[edit]  
I have just checked and this seems to be a bug, can you raise a issue on the nodes github page.

---

<div class="post-metadata">

### Author: ![Gaetano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gaetano/32/81395_2.png) [@Gaetano](https://discourse.nodered.org/u/Gaetano)
#### Post date: [28 June 2024 15:11 UTC](https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048/3 "2024-06-28T15:11:08Z")

</div>

Yes, of course

---

<div class="post-metadata">

### Author: ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)
#### Post date: [28 June 2024 15:11 UTC](https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048/4 "2024-06-28T15:11:25Z")

</div>

> [@Gaetano](#):
>
> cmnd/tasmota\_cucina/POWER

Just tried this myself, and it seems to be okay:

 ![Screenshot 2024-06-28 at 16.10.34](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/064cae71ffa2edc01d06f543edce36297aeafe62.png)  
 ![Screenshot 2024-06-28 at 16.10.38](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6ba3d05915a7d50014acb48f820d3e7f6e05498.png)

Where are you seeing the error flag?

p.s. welcome to the NR forums 🙂

---

<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: [28 June 2024 15:14 UTC](https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048/5 "2024-06-28T15:14:09Z")

</div>

I am seeing the issue too Joe.

 ![chrome_screenshot_28 Jun 2024 16_12_40 BST](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/30d327fe4ed00d60a7380b3e5f56f3a703bb487e.png)

```auto
[{"id":"d4daef38e3602a9b","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":4860,"wires":[["bfb4f9cb103bcd4c"]]},{"id":"bfb4f9cb103bcd4c","type":"ui-switch","z":"d1395164b4eec73e","name":"","label":"switch","group":"ff7cbf009b7b3034","order":1,"width":0,"height":0,"passthru":true,"topic":"cmnd/tasmota_cucina/POWER","topicType":"str","style":"","className":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":250,"y":4920,"wires":[["24a9b94fdad4092e","ab4c02fbdf85c8fd"]]},{"id":"24a9b94fdad4092e","type":"mqtt out","z":"d1395164b4eec73e","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"e8ba3ef5.22f4a8","x":430,"y":4920,"wires":[]},{"id":"ab4c02fbdf85c8fd","type":"debug","z":"d1395164b4eec73e","name":"debug 2557","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":370,"y":4860,"wires":[]},{"id":"ff7cbf009b7b3034","type":"ui-group","name":"Form","page":"3bceecc44f91d26b","width":"6","height":"1","order":1,"showTitle":true,"className":"","visible":"true","disabled":"false"},{"id":"e8ba3ef5.22f4a8","type":"mqtt-broker","name":"testb","broker":"192.168.1.25","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"3bceecc44f91d26b","type":"ui-page","name":"Custom Form Submission","ui":"1805777f90e92057","path":"/form-submission","icon":"home","layout":"grid","theme":"35ee7753b5b3599b","order":1,"className":"","visible":true,"disabled":"false"},{"id":"1805777f90e92057","type":"ui-base","name":"dashboard","path":"/dashboard","includeClientData":true,"acceptsClientConfig":["ui-notification","ui-control"],"showPathInSidebar":false},{"id":"35ee7753b5b3599b","type":"ui-theme","name":"Theme Name","colors":{"surface":"#16234b","primary":"#1d44b9","bgPage":"#ecf2f8","groupBg":"#ffffff","groupOutline":"#cccccc"}}]

```

On latest update

---

<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: [28 June 2024 15:20 UTC](https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048/6 "2024-06-28T15:20:42Z")

</div>

I reported that back in December last year. The topic does not get sent unless a message is first sent to the switch.

> <https://github.com/FlowFuse/node-red-dashboard/issues/456>
>
> If a Switch is configured with a fixed string as the topic then it does not send… the topic unless a message is sent to its input.
> To replicate, import the flow below. Do \*\*not\*\* click the inject nodes. Exercise the switch in the UI and it will be seen that the topic is not sent. Now click one of the inject nodes and thereafter the configured topic will be sent when the switch is exercised. Note that the input messages have a different topic. ~~Restarting node red did not seem to be sufficient to show the problem again, I had to delete the node, Deploy, and re-import it.~~
> 
> Edit: Using Dashboard version 0.11.1, NR 3.1.3, nodejs 20.5.1
> 
> Edit: On retesting with dashboard 1.1.0, NR 3.1.5, I find that the symptom is repeatable on node red restart.
> 
> \`\`\`
> \[{"id":"43d268a322a00bb8","type":"debug","z":"997da33a0beedade","name":"debug 2473","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":1020,"wires":\[\]},{"id":"26801ad4f7fb6aa3","type":"ui-switch","z":"997da33a0beedade","name":"","label":"string topic test switch","group":"7545fdb22e4a388c","order":0,"width":0,"height":0,"passthru":false,"topic":"some/topic","topicType":"str","style":"","className":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":570,"y":1020,"wires":\[\["43d268a322a00bb8"\]\]},{"id":"dc38f39b5ded4394","type":"inject","z":"997da33a0beedade","name":"","props":\[{"p":"payload"},{"p":"topic","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"another/topic","payload":"true","payloadType":"bool","x":280,"y":1000,"wires":\[\["26801ad4f7fb6aa3"\]\]},{"id":"de7f3f046aa9dbf5","type":"inject","z":"997da33a0beedade","name":"","props":\[{"p":"payload"},{"p":"topic","vt":"str"}\],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"another/topic","payload":"false","payloadType":"bool","x":290,"y":1060,"wires":\[\["26801ad4f7fb6aa3"\]\]},{"id":"7545fdb22e4a388c","type":"ui-group","name":"D2 tests","page":"d7fcdd33578e5d34","width":"6","height":"1","order":-1,"showTitle":true,"className":""},{"id":"d7fcdd33578e5d34","type":"ui-page","name":"d2","ui":"ID-BASE-1","path":"/d2","layout":"flex","theme":"4bff2b59c4c518e1","order":-1,"className":""},{"id":"ID-BASE-1","type":"ui-base","name":"Dashboard","path":"/dashboard"},{"id":"4bff2b59c4c518e1","type":"ui-theme","name":"Another Theme","colors":{"surface":"#ffffff","primary":"#0094ce","bgPage":"#eeeeee","groupBg":"#ffffff","groupOutline":"#cccccc"}}\]
> \`\`\`

---

<div class="post-metadata">

### Author: ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)
#### Post date: [28 June 2024 15:22 UTC](https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048/7 "2024-06-28T15:22:58Z")

</div>

Ah, okay, thanks Colin - I'll bump this up the priority list and address asap. Will need to be early next week now.

---

<div class="post-metadata">

### Author: ![Gaetano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gaetano/32/81395_2.png) [@Gaetano](https://discourse.nodered.org/u/Gaetano)
#### Post date: [28 June 2024 15:23 UTC](https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048/8 "2024-06-28T15:23:27Z")

</div>

Thanks Joe.  
I see the error on "Debug" of NR.  
This string is correct because I use it on DB1 nodes. It works correctly if you insert the string directly on topic of MQTT-OUT node.  
But I didn't understand if it works for you by putting the string in the ui-switch node and then sending the output to mqtt-out.  
In other words, is it my problem?

---

<div class="post-metadata">

### Author: ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)
#### Post date: [28 June 2024 15:24 UTC](https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048/9 "2024-06-28T15:24:40Z")

</div>

It would appear I tested on a `ui-switch` where I'd previously injected a `msg`. As Colin and @E1cid mentione above, it seems to happen _before_ any messages are injected into the switch - definitely an _us_ problem to fix 🙂 sorry for the inconvenience

---

<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: [28 June 2024 15:27 UTC](https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048/10 "2024-06-28T15:27:27Z")

</div>

It is not your fault, the switch does not send the topic unless it is first sent a message, so the MQTT node throws the error. As a matter of good practice it is best to always give nodes names, then if they throw an error like this you will see which node it is, rather than having to click in the debug pane to get it to highlight the node. As a workaround configure the inject node to send it a valid message on startup. You can remove that when the bug has been fixed.

---

<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: [12 July 2024 15:28 UTC](https://discourse.nodered.org/t/msg-topic-in-ui-switch-node/89048/11 "2024-07-12T15:28:05Z")

</div>

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