Performance get/set variables. different methods

If you need all the variables then not quite;

let {block_active, allow_rtc_onoff, allow_scheduler, allow_solar, block_mode, message_limit, counter, messages_yesterday, messages_today, block_mode_previous} = global.get( 'MQTT', "file").MQTT

The MQTT file is not the MQTT Object, just a container for the Object

[{"id":"a63d08342b71197c","type":"inject","z":"7fdabd9.f693544","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"MQTT\":{\"block_active\":true,\"allow_rtc_onoff\":\"on\",\"allow_scheduler\":true,\"allow_solar\":true,\"block_mode\":true,\"message_limit\":null,\"counter\":2,\"messages_yesterday\":20,\"messages_today\":2,\"block_mode_previous\":\"\"}}","payloadType":"json","x":2470,"y":420,"wires":[["dd6be678775335e5"]]},{"id":"dd6be678775335e5","type":"function","z":"7fdabd9.f693544","name":"function 12","func":"context.set(\"loadsOfThings\", msg.payload)\n\nlet {message_limit, allow_rtc_onoff} = context.get(\"loadsOfThings\").MQTT\nmsg.message_limit = message_limit\nmsg.allow_rtc_onoff = allow_rtc_onoff\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2670,"y":420,"wires":[["5ce9fa176e991281"]]},{"id":"5ce9fa176e991281","type":"debug","z":"7fdabd9.f693544","name":"debug 244","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2890,"y":420,"wires":[]}]

a quick demo to show what I mean. But other than that, yes, but only if you need them all and if so why not just

let MQTT = global.get('MQTT', 'file')

The advantage of global.get( 'MQTT', "file").MQTT is that it could be global.get( 'loadsOfStuff', "file").MQTT and 'loadsOfStuff' can be many other Objects - global.get( 'loadsOfStuff', "file").somethingImportant

2 Likes

This is good stuff. Thank you Buckskin !!!

I imported it and now I indeed see. This is very effective and easy to use. Less lines to write as well.
Very usefull !! Thanks.

1 Like

Just a check.
I had a good look at the stuff you gave me.
Is this the correct way you mean, or can the two (let) lists be combined even?


let msg1={};

let {message_limit, allow_rtc_onoff} = global.get('MQTT',"file");
let {LOW, NORMAL} = global.get('NightReductionRoomSetpoint',"file");

msg1.message_limit = message_limit;
msg1.allow_rtc_onoff = allow_rtc_onoff;
msg1.LOW = LOW;
msg1.NORMAL = NORMAL;

return msg1;

The result looks like this now, which seems fine to me. :slight_smile:

{
    "message_limit": 500,
    "allow_rtc_onoff": 1,
    "LOW": 18,
    "NORMAL": 21.6,
    "_msgid": "661c4966ea6a608a"
}

I cannot think of a way to reduce the (let) lists in one go (other than having all the globals in one Object) but the assignment to msg. can be shortened a bit

msg = {...msg, allow_rtc_onoff, message_limit, LOW, NORMAL}

or using msg1

let msg1 = {allow_rtc_onoff, message_limit, LOW, NORMAL}

You can also rename the incoming variables if required

let {LOW: lowSetPoint, NORMAL: normalSetPoint} = global.get('NightReductionRoomSetpoint',"file")

msg1 then becomes

let msg1 = {allow_rtc_onoff, message_limit, lowSetPoint, normalSetPoint}

Have a read of Destructuring assignment - JavaScript | MDN (mozilla.org), hours of fun. :wink:

1 Like

Thanks Buckskin

Just because I cannot resist

[{"id":"a63d08342b71197c","type":"inject","z":"e1e49eccc952ba10","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"anotherPayload","v":"{\"LOW\": true, \"NORMAL\": false}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"block_active\":true,\"allow_rtc_onoff\":\"on\",\"allow_scheduler\":true,\"allow_solar\":true,\"block_mode\":true,\"message_limit\":100,\"counter\":2,\"messages_yesterday\":20,\"messages_today\":2,\"block_mode_previous\":\"\"}","payloadType":"json","x":1490,"y":400,"wires":[["dd6be678775335e5"]]},{"id":"dd6be678775335e5","type":"function","z":"e1e49eccc952ba10","name":"function 12","func":"context.set(\"loadsOfThings\", msg.payload)\ncontext.set(\"moreThings\", msg.anotherPayload)\n\n// One way\nlet myContext = {}\nObject.assign(myContext, context.get(\"loadsOfThings\"), context.get(\"moreThings\") )\nmsg.myContext = myContext\n\n// Or another\nlet { message_limit, allow_rtc_onoff, LOW: lowSetPoint, NORMAL: normalSetPoint } = Object.assign(context.get(\"loadsOfThings\"), context.get(\"moreThings\"))\n\nmsg = { ...msg, allow_rtc_onoff, message_limit, lowSetPoint, normalSetPoint }\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1690,"y":400,"wires":[["5ce9fa176e991281"]]},{"id":"5ce9fa176e991281","type":"debug","z":"e1e49eccc952ba10","name":"debug 244","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1910,"y":400,"wires":[]}]

The 2 global.gets into 1 variable at the same time

let { message_limit, allow_rtc_onoff, LOW: lowSetPoint, NORMAL: normalSetPoint } = Object.assign(context.get("loadsOfThings"), context.get("moreThings"))

1 Like

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