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
E1cid
5 March 2023 15:46
2
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;
Colin
5 March 2023 16:05
3
Perhaps you mean that the outermost layer should be an array?
Colin
5 March 2023 16:07
4
Or perhaps you meant, where you say "room"
, it should be, for example, "tak"
, and similarly for the light.
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...
jbudd
5 March 2023 17:03
8
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
Colin
5 March 2023 18:13
10
Something like, untested:
let data = context.get("data") || {}
data[room] = data[room] || {}
data[room][light] = {status: status, brightness: brightness}
context.set("data", data)
E1cid
5 March 2023 18:33
12
E1cid:
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;
@widert the solution I posted 3 hours ago didn't work then?
I'm sorry, but I managed to miss your proposed solution...
system
Closed
19 March 2023 21:14
14
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.