logo elektroda
logo elektroda
X
logo elektroda

[Solved] Node-RED: How do I assign the value from msg.payload.svalue1 to a variable?

Lokomotion 5889 8
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 16924988
    Lokomotion
    Level 11  
    Good day I have installed Node-RED and mosquito. I am sending data from Domoticz via MQTT and created a simple flowchart at the end I set msg.payload and I get data like this from different sensors:
    {
    "Battery" : 255,
    "RSSI" : 12,
    "description" : "",
    "dtype" : "Temp",
    "id" : "82002",
    "idx" : 5,
    "meterType" : "Energy",
    "name" : "Test",
    "nvalue" : 0,
    "stype" : "LaCrosse TX3",
    "svalue1" : "55.5",
    "unit" : 1
    }
    Is it possible to assign the value itself from e.g. svalue1 to a variable so that I can use this variable in another function ? I mean just the value 55.5
  • ADVERTISEMENT
  • #2 16925175
    michalko12
    MCUs specialist
    I hope I have understood you correctly:
    Code: Javascript
    Log in, to see the code
    .
  • #3 16925189
    Lokomotion
    Level 11  
    I need to extract only the number that is in svalue1 i.e. 55.5 and write it to a variable to use it in another function.
    Or with idx so that it only writes the value of idx, i.e. 5, to the variable
  • ADVERTISEMENT
  • #5 16925207
    Lokomotion
    Level 11  
    i.e. as he understands

    var x = 0.0;
    x = parseFloat(msg.svalue1);

    will write me the svalue itself to the x variable which is 55.5.
    And how do I then display this msg.payload to see if it works ?
    Sorry for such questions but I'm just starting with node red
  • ADVERTISEMENT
  • #6 16925220
    michalko12
    MCUs specialist
    There you have a node of type debug and a Debug tab on the right side of the screen to which this node writes the entire msg : Object



    .
  • #7 16925291
    Lokomotion
    Level 11  
    something is not assigning the value from the svalue1 field to the x variable
  • ADVERTISEMENT
  • #8 16926060
    Anonymous
    Anonymous  
  • #9 17092526
    Lokomotion
    Level 11  
    Closing topic

Topic summary

The discussion addresses how to extract and assign the numeric value from the field `svalue1` within a JSON payload received via MQTT in Node-RED. The payload originates from Domoticz sensors and includes multiple fields such as Battery, RSSI, dtype, idx, and svalue1. The user seeks to isolate the value "55.5" from `msg.payload.svalue1` and store it in a variable for use in other functions. The recommended approach involves using JavaScript within a function node, specifically parsing the string value to a float with `parseFloat(msg.payload.svalue1)` and assigning it to a variable. To verify the assignment, the use of a debug node is suggested to output the message object. Additionally, the concept of global variables in Node-RED is referenced for sharing data across functions. The conversation highlights beginner-level questions about JavaScript usage in Node-RED and the practical steps to manipulate and debug message payloads.
Summary generated by the language model.
ADVERTISEMENT