Sorting arrays
-
When you train a rule or expression on a large number of devices, you're going to get activity when those devices change, and that can be a lot. There's more activity than you know, normally. Not everything gets logged at the default level. 21286 does contain a bit more logging.
@toggledbits Master, following the discussion of this thread, the variable FaultMSG is composed with the list of devices that will be displayed, example below.
As you can see, the final string of devices, not sorted by name, follows the array generated by the device ID, composed one step earlier.
Is it possible to sort by device name? Before generating the string? I think when generating the array?
Thanks.
-
This actually is a separate topic, so I forked it here...
I'll need to add a
sort()
function to the expression language. This is something I've had on radar for a while, but doing it "right" is not as simple as one might think. -
OK. In build 21297 just released, there is now a
sort()
function in the expression language/library.FaultOn = sort( each id in FaultSensor: do e=getEntity( id ), e?.attributes?.x_vera_device?.failed == true ? e.name : null done )
-
OK. In build 21297 just released, there is now a
sort()
function in the expression language/library.FaultOn = sort( each id in FaultSensor: do e=getEntity( id ), e?.attributes?.x_vera_device?.failed == true ? e.name : null done )
@toggledbits there is an error in the expression.
-----------------------^
Expecting 'EOF', 'COMMA', 'EXPRSEP', ')', '-', 'POW', 'RANGE', '', '/', 'MOD', '+', '<<', '>>', '>>>', 'BAND', 'BOR', 'BXOR', 'LAND', 'LOR', '==', '!=', '===', '!==', 'IN', '<', '<=', '>', '>=', 'COALESCE', 'COALESCENAN', '?', 'ELSE', 'ENDIF', 'DONE', got 'COLON'
Error: Parse error on line 1:
...ice?.failed == true : e.name : null done
-----------------------^
Expecting 'EOF', 'COMMA', 'EXPRSEP', ')', '-', 'POW', 'RANGE', '', '/', 'MOD', '+', '<<', '>>', '>>>', 'BAND', 'BOR', 'BXOR', 'LAND', 'LOR', '==', '!=', '===', '!==', 'IN', '<', '<=', '>', '>=', 'COALESCE', 'COALESCENAN', '?', 'ELSE', 'ENDIF', 'DONE', got 'COLON'
at Parser.parseError (C:\MSR\reactor\common\lexp.js:281:21)
at Parser.parse (C:\MSR\reactor\common\lexp.js:348:22)
at Object.evaluate (C:\MSR\reactor\common\lexp.js:1574:32)
at Engine._eval_expr (C:\MSR\reactor\server\lib\Engine.js:934:413)
at Engine.evaluateGlobalExpression (C:\MSR\reactor\server\lib\Engine.js:1135:115)
at Immediate._onImmediate (C:\MSR\reactor\server\lib\Engine.js:1149:35)
at processImmediate (node:internal/timers:464:21)
[latest-21297]2021-10-24T22:00:57.220Z Engine:ERR Engine#1: error evaluating expression FaultOn: Error: Parse error on line 1:
...ice?.failed == true : e.name : null done
-----------------------^
Expecting 'EOF', 'COMMA', 'EXPRSEP', ')', '-', 'POW', 'RANGE', '', '/', 'MOD', '+', '<<', '>>', '>>>', 'BAND', 'BOR', 'BXOR', 'LAND', 'LOR', '==', '!=', '===', '!==', 'IN', '<', '<=', '>', '>=', 'COALESCE', 'COALESCENAN', '?', 'ELSE', 'ENDIF', 'DONE', got 'COLON'
[latest-21297]2021-10-24T22:00:57.220Z Engine:ERR Engine#1: expression: sort( each id in FaultSensor: do e=getEntity( id ), e?.attributes?.x_vera_device?.failed == true : e.name : null done )
[latest-21297]2021-10-24T22:00:57.221Z Engine:ERR Engine#1: path FaultMSG->FaultOn
[latest-21297]2021-10-24T22:00:57.223Z Engine:CRIT Error: Parse error on line 1:
...ice?.failed == true : e.name : null done
-----------------------^
Expecting 'EOF', 'COMMA', 'EXPRSEP', ')', '-', 'POW', 'RANGE', '', '/', 'MOD', '+', '<<', '>>', '>>>', 'BAND', 'BOR', 'BXOR', 'LAND', 'LOR', '==', '!=', '===', '!==', 'IN', '<', '<=', '>', '>=', 'COALESCE', 'COALESCENAN', '?', 'ELSE', 'ENDIF', 'DONE', got 'COLON'
Error: Parse error on line 1:
...ice?.failed == true : e.name : null done
-----------------------^
Expecting 'EOF', 'COMMA', 'EXPRSEP', ')', '-', 'POW', 'RANGE', '*', '/', 'MOD', '+', '<<', '>>', '>>>', 'BAND', 'BOR', 'BXOR', 'LAND', 'LOR', '==', '!=', '===', '!==', 'IN', '<', '<=', '>', '>=', 'COALESCE', 'COALESCENAN', '?', 'ELSE', 'ENDIF', 'DONE', got 'COLON'
at Parser.parseError (C:\MSR\reactor\common\lexp.js:281:21)
at Parser.parse (C:\MSR\reactor\common\lexp.js:348:22)
at Object.evaluate (C:\MSR\reactor\common\lexp.js:1574:32)
at Engine._eval_expr (C:\MSR\reactor\server\lib\Engine.js:934:413)
at Engine._expr_resolve (C:\MSR\reactor\server\lib\Engine.js:1185:38)
at _resolve (C:\MSR\reactor\common\lexp.js:1172:35)
at _run (C:\MSR\reactor\common\lexp.js:1205:31)
at _run (C:\MSR\reactor\common\lexp.js:1430:38)
at C:\MSR\reactor\common\lexp.js:1424:33
at Array.forEach (<anonymous>) -
Sorry, see edit in my prior post.
-
Sorry, see edit in my prior post.
@toggledbits I updated the expression, and the syntax error disappeared. I see that it now returns the name directly in the array and no the ID...
.. but continues following the ID sequence, no sort.
-
OK. Pull the latest release package (again) and try it. I respun the release. Sorting is locale-specific, not that that would affect you since you're likely using the default locale, but it changes the way things work internally and makes it more complicated, and I missed a parameter on a call.
-
OK. Pull the latest release package (again) and try it. I respun the release. Sorting is locale-specific, not that that would affect you since you're likely using the default locale, but it changes the way things work internally and makes it more complicated, and I missed a parameter on a call.
@toggledbits PERFECT!!!
Well, now it's time to review everything I had before and put it in order
And best of all, remember that I did 3 steps:
- List of all devices;
- Applying the rule, like battery level;
- Generate a string to send by message putting the device name.
Now, with the sort get the name, I can eliminate the third variable, and using format() generate the message directly, SHOW!!
The game is getting fun.
It's going to be a long night
-
T toggledbits locked this topic on