Write object to context variable

I have a MQTT message that comes in looking like this:

topic: "zigbee2mqtt/lys/baderom/tak"
payload: object
brightness_relay: 254
linkquality: 58
state: "OFF"
state_relay: "OFF"

I am deducting the structure using the following:

const room = msg.topic.split("/")[2];
const light = msg.topic.split("/")[3];
const status = msg.payload.state.toLowerCase();
const brightness = msg.payload.brightness_relay;

Using context.set, I would like to store to store these (status) messages in the following format:

{
  "room": {
     "light": {
       "status" = status
       "brightness" = brigthness
     }
     "light": {
       "status" = status
       "brightness" = brigthness
     }
  "room": {
     "light": {
       "status" = status
       "brightness" = brigthness
     }
     "light": {
       "status" = status
       "brightness" = brigthness
     }
}    

How can I do this using context.set?

Thanks,
Tom

Impossible, an object can only have one property called room. In your base object you have 2. You room objects have to properties called light also impossible.
If you mean room is changing and the output would be

{
    "baderom": {
        "tak": {
            "status": "off",
            "brightness": 254
        }
    },
    "baderom1": {
        "tak1": {
            "status": "off",
            "brightness": 254
        }
    }
}

then

const room = msg.topic.split("/")[2];
const light = msg.topic.split("/")[3];
const status = msg.payload.state.toLowerCase();
const brightness = msg.payload.brightness_relay;
context.set(`store.${room}.${light}`, {status,brightness})
return msg;

Perhaps you mean that the outermost layer should be an array?

Or perhaps you meant, where you say "room", it should be, for example, "tak", and similarly for the light.

Yes, that is correct!

I'm sorry, bad explanation from my side, this is how it should look like:

{
  "baderom": {
     "tak": {
       "status" = "on",
       "brightness" = 235
     }
     "vegg": {
       "status" = "off"
       "brightness" = 192
     }
  "gjestebad": {
     "tak": {
       "status" = "on"
       "brightness" = 211
     }
     "vegg": {
       "status" = "off"
       "brightness" = 121
     }
}   

So whenever I receive a message with e.g. topic: "zigbee2mqtt/lys/stue/vegg", I need the "stue" and "vegg" "status" and "brightness" to be updated...

Well that's not valid JSON.
Can you confirm that this is what you want your context variable to contain?

{
 "baderom":{
   "tak":{"status":"on","brightness":235},
   "vegg":{"status":"off","brightness":192}
 },
 "gjestebad":{
   "tak":{"status":"on","brightness":211},
   "vegg":{"status":"off","brightness":121}
 }
}

Which you can access by (eg) <variable name>.gjestebad.tak.brightness

Yes, that is correct :grinning:

Something like, untested:

let data = context.get("data") || {}
data[room] = data[room] || {}
data[room][light] = {status: status, brightness: brightness}
context.set("data", data)

Perfect, thanks!!

@widert the solution I posted 3 hours ago didn't work then?

I'm sorry, but I managed to miss your proposed solution...

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