I am trying to write to a global context using a variable as first parameter instead of a hard-coded string but am strugling about what the syntax for that is.
If I am running the following code within a function node, it works flawlessly:
global.set("NewEnvironment.Abstellkammer.Climate.temperature", msg.payload.temperature, "file"); global.set("NewEnvironment.Abstellkammer.Climate.humidity", msg.payload.humidity, "file"); return msg;
(Abstellkammer = lumber room)
However, if I want to dynamically set the global context (don't know the exact name for this, I'm talking about the first parameter of global.set(...)) I am struggling to find the right syntax.
var myEnvironment = "NewEnvironment."; myEnvironment = myEnvironment + msg.payload.RoomName + "."; myEnvironment = myEnvironment + msg.payload.SensorType + "."; global.set(myEnvironment, msg.payload.temperature, "file"); global.set(myEnvironment, msg.payload.humidity, "file"); return msg;
throws an error message:
"Error: Invalid property expression: unterminated expression"
I tried to find out about properties and tried different syntax to use my variable instead of a hard-coded first parameter, but have not been successful.
What I would like to achive, is that I am using the variable myEnvironment within the global.set function.
At the moment I'm helping myself out with using a gigantic switch node which takes care about the room name at first and duplicate versions of my function where the RoomName and SensorTypes are hard-coded.
Unfortunately I'm struggling on this tiny syntax thing here - as I think it should be possible to pass a variable there.
Any help is much appreciated.