Hello everyone,
I am trying to step by step build a small MQTT filter flow.
I have my MQTT input all set up and can receive messages and I can "filter" using a function node with e.g.
var msg = { payload: msg.payload.ENERGY['Total'] };
return msg;
First issue
However, if a payload does not have the key 'Total' I get the following error message
TypeError: Cannot read properties of undefined (reading 'Total')
So I tried ensuring that the key exists, but it is also throws an error.
Tried
if ('Total' in msg.payload.ENERGY) {
var msg = { payload: msg.payload.ENERGY['Total'] };
return msg;
}
and
if (msg.payload.hasOwnProperty('Total')) {
var msg = { payload: msg.payload.ENERGY['Total'] };
return msg;
}
Second issue
I would also like to be able to use wildcards, but my syntax seems wrong and I cannot find the solution.
Here I am trying to find all payloads that contain a key with "ATC" in its name
payload: msg.payload+'.[ATC*]'
Also tried #
and +
instead of * (because those can be used e.g. when subscribing to an MQTT topic).
Thank you for your support
Alex
EDIT1:
Also unsuccessfully tried
if (msg.payload.ENERGY['Power'] !== undefined) {
var msg = { payload: msg.payload.ENERGY['Power'] };
return msg;
}
because apparently this is faster than the "in" or "hasOwnProperty" approach ( arrays - Checking if a key exists in a JavaScript object? - Stack Overflow )
EDIT2:
I think I may have solved my first issue:
EDIT3:
And @Colin solved the second issue