Hyphen character (-) problem in function mqtt tasmota

Hi,
I have a problem with the dash (-)in this function (mqtt - tasmota temperature sensor):

var temp = msg.payload.AM2301-13.Temperature;



var msg = {
    payload:temp
};

return msg;

How can I use - as simple text and not as an operator,
I've try with:

var temp = msg.payload.["AM2301-13"].Temperature;



var msg = {
    payload:temp
};

return msg;

but I get an error

thanks

Can you edit your post and use the </> button in the formatting toolbar to properly format your code sections? At the moment, the forum software is interpreting some of your code as markdown formatting and it means we can't read your code.

You can't use a dash in a JavaScript property name when used with dot notation.

You would have to do:

var temp = msg.payload['AM2301-13'].Temperature;

Unfortunately, always an error, I can not understand what syntax should be used to include the hyphen
test

The name is automatically generated by tasmota, when you have more than one sensor the dash (-) with the number of the gpio is added to the name, in fact with a single sensor everything works perfectly

I think the problem is you're trying to declare 'msg' again.

Try the following...

var temp = msg.payload['AM2301-13'].Temperature;

msg = {
    payload:temp
};

return msg;

it cannot be, otherwise it would not work even if you omitted the hyphen

Please add a debug node (set to display the complete msg object) to the outpit of the mqtt-in node. When the data comes in, Open all the levels in the debut and copy/paste that to a reply.
This will give us a chance to see the actual data names and values. With that I think someone can tell you what to do.

13/1/2020, 15:19:49[node: 8005f275.19abb8](http://172.22.22.16:1880/#)

tele/Condizionatore/SENSOR : msg : Object
object
topic: "tele/Condizionatore/SENSOR"
payload: object
qos: 1
retain: false
_msgid: "21ab5da2.8eac52"

13/1/2020, 15:19:49[node: Get temperature](http://172.22.22.16:1880/#)function : (error)

"TypeError: Cannot read property 'Humidity' of undefined"

This is mqtt from tasmota console:
MQT: tele/Condizionatore/SENSOR = {"Time":"2020-01-13T15:25:22","AM2301-13":{"Temperature":23.0,"Humidity":30.2},"AM2301-14":{"Temperature":23.6,"Humidity":28.5},"TempUnit":"C"}

If i remove a sensor the mqtt is
15:28:56 MQT: tele/Condizionatore/SENSOR = {"Time":"2020-01-13T15:28:56","AM2301":{"Temperature":23.0,"Humidity":30.2},"TempUnit":"C"}
and and it works perfectly :roll_eyes: with function:

var temp = msg.payload.AM2301.Temperature;



var msg = {
    payload:temp
};

return msg;

You did not expand the msg.payload as I asked so I can't see the full path to the data.

2 SENSOR (PROBLEM)
immagine|690x452

1 SENSOR (OK)

Export your flow and share.

The name is automatically generated by tasmota, when you have more than one sensor the dash (-) with the number of the gpio is added to the name, in fact with a single sensor everything works perfectly

I don't see a dash in the debug at all. Where did the -13 come from?

EDIT: Ahhh I see you have edited your post and now show it with the dashes.

That is what tasmota does. If there is one sensor, the property name is AM2301. If there is more such sensors, property names will be like AM2301-1,... . Usually they start from 1.

But again, share your flow. Issue might be hidden somewhere else.

Yes correct! the problem is the names of the two sensors, this is the flow with a sensor that works properly:

[{"id":"5d0c2e1c.1695a","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"cd67773.ec6e608","type":"mqtt in","z":"5d0c2e1c.1695a","name":"","topic":"tele/Condizionatore/SENSOR","qos":"1","datatype":"auto","broker":"49b62a53.4ad6e4","x":280,"y":380,"wires":[["c1c69759.caac2"]]},{"id":"c1c69759.caac2","type":"json","z":"5d0c2e1c.1695a","name":"","pretty":false,"x":482,"y":533,"wires":[["76d5194c.03ef38","9346b6b1.f960c8","f5d51ba9.108348"]]},{"id":"76d5194c.03ef38","type":"function","z":"5d0c2e1c.1695a","name":"Get temperature","func":"var temp = msg.payload.AM2301.Temperature;\n\nmsg = {\n    payload:temp\n};\n\nreturn msg;","outputs":1,"noerr":0,"x":636,"y":533,"wires":[["8123415.0a4414","b81a3d7d.f86e4"]]},{"id":"8123415.0a4414","type":"ui_gauge","z":"5d0c2e1c.1695a","name":"Room temp","group":"ceee212e.e6fb28","order":5,"width":"6","height":"4","gtype":"gage","title":"Temp","label":"","format":"{{value}}°C","min":0,"max":"50","colors":["#00ff00","#fdd242","#ff0202"],"seg1":"","seg2":"","x":807,"y":503,"wires":[]},{"id":"b81a3d7d.f86e4","type":"debug","z":"5d0c2e1c.1695a","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","targetType":"full","x":960,"y":660,"wires":[]},{"id":"9346b6b1.f960c8","type":"debug","z":"5d0c2e1c.1695a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":630,"y":700,"wires":[]},{"id":"9c4ba77a.ee4328","type":"ui_gauge","z":"5d0c2e1c.1695a","name":"Room Umidity","group":"ceee212e.e6fb28","order":5,"width":"6","height":"5","gtype":"gage","title":"Umidità","label":"","format":"{{value}}%","min":0,"max":"80","colors":["#0000ff","#00fb00","#ff0202"],"seg1":"","seg2":"","x":801,"y":410,"wires":[]},{"id":"6e48b0e7.3be9d","type":"debug","z":"5d0c2e1c.1695a","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1040,"y":520,"wires":[]},{"id":"f5d51ba9.108348","type":"function","z":"5d0c2e1c.1695a","name":"Get temperature","func":"var humy = msg.payload.AM2301.Humidity;\n\n\n\n\nvar msg = {\n    payload:humy\n};\n\nreturn msg;","outputs":1,"noerr":0,"x":560,"y":420,"wires":[["9c4ba77a.ee4328","6e48b0e7.3be9d"]]},{"id":"49b62a53.4ad6e4","type":"mqtt-broker","z":"","broker":"192.168.1.80","port":"1883","clientid":"node-red","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"ceee212e.e6fb28","type":"ui_group","z":"","name":"Blanket","tab":"30daa7b8.3ce76","disp":true,"width":"6"},{"id":"30daa7b8.3ce76","type":"ui_tab","z":"","name":"Bedroom1","icon":"dashboard"}]

If I add another sensor I've try with:
AM2301-13
[AM2301-13]
["AM2301-13"]
[' AM2301-13']
always the same problem, - is not recognized as a name, I'm starting to think it's easier to change the separation character in tasmota :rofl:

Let's do following:

  1. remove every wire from json node
  2. place new function node after json node and paste this in it.
for (const property in msg.payload) {
  node.warn(property+":"+JSON.stringify(msg.payload[property]));
}
return msg;
  1. make sure you have multiple sensors connected
  2. run and lets see what is printed to debug tab.

so okay?

Good.
Now we need to know what you need to do with those values. As they are actually from different sensors but provided from one source - do you need to split them by sensor (lets call it location based) or somehow differently?
There is no reason to create multiple functions to split such data. All can be done in single function. Function can have multiple outputs (one for every measured value)

Just tell us how do you need the values be splitted.

I just need to have the temperature and humidity values ​​for the two sensors, the values ​​will then be passed to a node to display a linear gauge on the dashboard.

OK. Look at the function I prepared for you. You can modify it by your needs. I added many comments. Hope it helps you to understand what I did.
Basically I did function with 2 outputs, one for each sensor.
Connect debug nodes to see what are the outputs to create following flow properly.

[{"id":"d6c69556.fdf428","type":"function","z":"6d08d5ab.a8aa3c","name":"parse multiple AM2310","func":"var out_1 = null // this will be message for first output\nvar out_2 = null// this will be message for second output\n\n// notice that if message stays null, it will not be sended out from here\n\nfor (const property in msg.payload) {\n    // you can always debug incoming payload for what properties it has.\n    //just remove \"//\" from beginning of the next line\n  //node.warn(property+\":\"+JSON.stringify(msg.payload[property]));\n  if(property == \"AM2301-13\"){\n      // incoming payload contains AM2301-13. lets create outgoing message out_1\n      out_1 = {topic:property}// you can change the topic to indicate sensor location\n      // let's create payload containing humidity and temperature of that sensor\n      out_1.payload = {temperature:msg.payload[property].Temperature,humidity:msg.payload[property].Humidity}\n      \n  }\n  //let's do same things if AM2301-14 found in payload\n if(property == \"AM2301-14\"){\n      out_2 = {topic:property}\n      out_2.payload = {temperature:msg.payload[property].Temperature,humidity:msg.payload[property].Humidity}\n      \n  }\n}\n\n// now there will be 2 outputs. \nreturn [out_1,out_2];","outputs":2,"noerr":0,"x":330,"y":300,"wires":[["e674927.ece217"],["95078d.5b80587"]]}]

now I don't get any more errors but if I link to a linear gouge directly, I get the values ​​of each sensor together, I assume I have to add another function to split the two values (temperature and humidity), right?