I’m trying to list all my battery powered devices excluding tablets and phones and list all the percentages left.
[{"id":"bb78f07125f8413e","type":"change","z":"ebaa69a9.649708","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"battery_size","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":560,"wires":[["afd464a52b15da53"]]},{"id":"eb6da3b4107fbd1a","type":"inject","z":"ebaa69a9.649708","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":600,"wires":[["2da739aba61004d6","bb78f07125f8413e"]]},{"id":"afd464a52b15da53","type":"ha-get-entities","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","version":0,"rules":[{"property":"attributes.state_class","logic":"is","value":"measurement","valueType":"str"},{"property":"attributes.device_class","logic":"is","value":"battery","valueType":"str"},{"property":"attributes.battery_size","logic":"is","value":".*","valueType":"re"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":690,"y":560,"wires":[["4604e5a18e2e6b34"]]},{"id":"2da739aba61004d6","type":"change","z":"ebaa69a9.649708","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"battery_voltage","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":620,"wires":[["811736146a2f051a"]]},{"id":"4604e5a18e2e6b34","type":"join","z":"ebaa69a9.649708","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":850,"y":600,"wires":[["a8cddac6ea3fe7cb"]]},{"id":"811736146a2f051a","type":"ha-get-entities","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","version":0,"rules":[{"property":"attributes.state_class","logic":"is","value":"measurement","valueType":"str"},{"property":"attributes.device_class","logic":"is","value":"battery","valueType":"str"},{"property":"attributes.battery_voltage","logic":"is","value":".*","valueType":"re"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":690,"y":620,"wires":[["4604e5a18e2e6b34"]]},{"id":"a8cddac6ea3fe7cb","type":"function","z":"ebaa69a9.649708","name":"","func":"var array = {};\nfor (var index in msg.payload.battery_voltage) {\n //node.warn(msg.payload.battery_voltage[index].attributes.friendly_name);\n array[msg.payload.battery_voltage[index].attributes.friendly_name] = msg.payload.battery_voltage[index].state;\n}\n\nfor (var index in msg.payload.battery_size) {\n //node.warn(msg.payload.battery_voltage[index].attributes.friendly_name);\n array[msg.payload.battery_size[index].attributes.friendly_name] = msg.payload.battery_size[index].state;\n}\n\n//array.sort((A, B) => B.value - A.value);\nmsg.arr = array;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1000,"y":540,"wires":[["0e0aee1f9fb74388","a25898f22e6e5f71"]]},{"id":"0e0aee1f9fb74388","type":"sort","z":"ebaa69a9.649708","name":"","order":"descending","as_num":false,"target":"arr","targetType":"msg","msgKey":"payload","msgKeyType":"elem","seqKey":"payload","seqKeyType":"msg","x":990,"y":680,"wires":[["a25898f22e6e5f71"]]},{"id":"a25898f22e6e5f71","type":"debug","z":"ebaa69a9.649708","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1190,"y":640,"wires":[]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]
Now I have msg.arr as my devices in a key value pair.
I have tried to use sort in the function node and I have tried sort node but I just can’t get it sorted.
What am I doing wrong here?
I want the lowest battery percentage to be on top