# Msg.topic as name for flow variable

**URL:** <https://discourse.nodered.org/t/msg-topic-as-name-for-flow-variable/23823>\
**Category:** General\
**Created:** [29 March 2020 11:16 UTC](https://discourse.nodered.org/t/msg-topic-as-name-for-flow-variable/23823 "2020-03-29T11:16:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![KoenV](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/koenv/32/18994_2.png) [@KoenV](https://discourse.nodered.org/u/KoenV)\
**Post date:** [29 March 2020 11:16 UTC](https://discourse.nodered.org/t/msg-topic-as-name-for-flow-variable/23823/1 "2020-03-29T11:16:33Z")

</div>

Hi,

I am reasonable new to node-red. in my project i have a number of settings that are changeable on a dashboard. When that change comes in i want to use the msg.topic as the name for the flow variable. i tried many syntaxes and thought to have it working but then not really:

flow.set("{{msg.topic}}", msg.payload};

I tried with double and single quotes, $ sign,... but could not get it to work. i probably miss a simple syntax thing.

Regards,  
Koen

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 March 2020 11:41 UTC](https://discourse.nodered.org/t/msg-topic-as-name-for-flow-variable/23823/2 "2020-03-29T11:41:17Z")

</div>

use a function node

```auto
var t = msg.topic
flow.set(t,msg.payload)
return msg;

```

---

<div class="post-metadata">

**Author:** ![KoenV](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/koenv/32/18994_2.png) [@KoenV](https://discourse.nodered.org/u/KoenV)\
**Post date:** [29 March 2020 11:59 UTC](https://discourse.nodered.org/t/msg-topic-as-name-for-flow-variable/23823/3 "2020-03-29T11:59:56Z")

</div>

Thx zenofmud,

This works!!  
I was using a function node already. It seems the input of the set command can only deal with a simple variable.

regards,  
Koen

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 March 2020 12:48 UTC](https://discourse.nodered.org/t/msg-topic-as-name-for-flow-variable/23823/4 "2020-03-29T12:48:31Z")

</div>

the set command expects a valid javascript property name - so as long as what you use produces that then it can be whatever you like.

---

<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:** [29 March 2020 12:55 UTC](https://discourse.nodered.org/t/msg-topic-as-name-for-flow-variable/23823/5 "2020-03-29T12:55:36Z")

</div>

> [@zenofmud](#):
>
> flow.set(t,msg.payload)

This should work just as well  
`flow.set(msg.topic,msg.payload)`

---

<div class="post-metadata">

**Author:** ![KoenV](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/koenv/32/18994_2.png) [@KoenV](https://discourse.nodered.org/u/KoenV)\
**Post date:** [29 March 2020 13:11 UTC](https://discourse.nodered.org/t/msg-topic-as-name-for-flow-variable/23823/6 "2020-03-29T13:11:19Z")

</div>

Hi Colin,

You are right. I have tried this syntax before but it didn't work. Now tried it again and it does work. I must have made another error and sent myself looking for a difficult solution... Thanks for getting me on the road again 🙂

---

<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 April 2020 13:11 UTC](https://discourse.nodered.org/t/msg-topic-as-name-for-flow-variable/23823/7 "2020-04-12T13:11:21Z")

</div>

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