Convert slider value in MQTT message

Hi

I have a slider in my dashboard which outputs a number between 1 - 100. I would like this number to be included into mqtt message:

"{"state":"OFF","level":"100","temperature":"50"}"

The number after level I would like this to variable from the slider while keeping the others intact.

Anybody or a way to do this?

Hi,
what does "included" mean in your case ? Is that a completely new message - or and existing message and you want to add (or change) the level value ?

I want to change the value of level, if it's the entire message that needs to be changes makes no difference to me at this point

for a complete message then either a simple template or function node would do the trick, for example:

[{"id":"8e223ffb.ea79e","type":"template","z":"439238bf.9f6608","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\n{\"state\":\"OFF\",\"level\":{{payload}},\"temperature\":50}","output":"json","x":300,"y":1040,"wires":[["ebdfa204.a682f"]]}]

Have you considered the option of using separate topics, so your/topic/state, your/topic/level and yourtopic/temperature? Then you won't need to merge them when publishing or split them up when subscribing.

Thank you all for this help, worked perfect :slight_smile:

@Colin - The topics are different. They depend on the remote, the bulbs, etc...

Not sure why that invalidates my suggestion, it is up to you of course.

I understand your idea now, and that would also work. Depends on what I want to do offcause :slight_smile:

@agoralive I'm trying to figure this out too but a bit new to node-red, still. Could you share how you place the slider upstream from the template and then how the template injects into the MQTT node? Sample code would be great.

Hi @jkmaxwell, welcome to the forum. I deduce from that question that you have only just started with node-red. If so then I strongly recommend that you watch the Node Red Essentials video playlist linked from the node red docs. A couple of hours spent doing that will allow you to answer basic questions yourself and will save a lot of time in the long run.
When you have done that then if you are still stuck then ask again. However this thread is 18 months old so I suggest you start a new thread with the specific question you have.