Sort Array Based on Newly Added Items
-
A review of the documentation for the
sort()
function will also tell you how to write a custom comparator. -
maybe I'm missing something, but if the order is already OK, why are you sorting it? are you trying to get the last or fist ones added? look at https://reactor.toggledbits.com/docs/Expressions-%26-Variables/#arrayobject-handling-functions
pop
/shift
should be enough to get the first/last and remove it, or justmyArr[0]
andmyArr[myArr.length]
if you want to maintain the current array intact.@therealdb said in Sort Array Based on Newly Added Items:
maybe I'm missing something, but if the order is already OK, why are you sorting it? are you trying to get the last or fist ones added? look at https://reactor.toggledbits.com/docs/Expressions-%26-Variables/#arrayobject-handling-functions
pop
/shift
should be enough to get the first/last and remove it, or justmyArr[0]
andmyArr[myArr.length]
if you want to maintain the current array intact.I want to get the last ones added.
To my understanding
pop
/shift
will get the first/last item in the array but if its sorted alphabetically by default it won't get the last item added to the array.@toggledbits said in Sort Array Based on Newly Added Items:
A review of the documentation for the
sort()
function will also tell you how to write a custom comparator.Had a look at that
sort()
command and did some research but everything I come across only shows how to sort the array in a different way. I don't want to sort the array, I want it to act as a list sort of. As a new item is added it goes to the top of the array. -
The
pop
,push
,shift
andunshift
are what you need to be looking at, as @therealdb said. -
So I have tried
shift
since it returns the first element in the array, but the same issue still is happening. Since the array is sorted alphabetically the first element isn't always the latest item that was added to the array. Here's a better explanation:Here is a the array of the devices that are
not_home
.
My goal is to notify the user that was considered
not_home
last. In this case the order of devices that werenot_home
was "Mom_iPhone", "Armans_iPhone" and then "Jas_iPhone". Since "Jas_iPhone" was the last device that was considerednot_home
I would like to target that device. But when I use theshift
command "Armans_iPhone" is the output bc it is first in the array when sorted alphabetically. If I use thepop
command the output is "Moms_iPhone", which is still not what I want. -
So I have tried
shift
since it returns the first element in the array, but the same issue still is happening. Since the array is sorted alphabetically the first element isn't always the latest item that was added to the array. Here's a better explanation:Here is a the array of the devices that are
not_home
.
My goal is to notify the user that was considered
not_home
last. In this case the order of devices that werenot_home
was "Mom_iPhone", "Armans_iPhone" and then "Jas_iPhone". Since "Jas_iPhone" was the last device that was considerednot_home
I would like to target that device. But when I use theshift
command "Armans_iPhone" is the output bc it is first in the array when sorted alphabetically. If I use thepop
command the output is "Moms_iPhone", which is still not what I want.@pabla said in Sort Array Based on Newly Added Items:
Since the array is sorted alphabetically the first element isn't always the latest item that was added to the array.
OK. So which one IS? How do you know? If it's just the last one (really), that's
pop()
, not shift. That should be clear in the docs. -
In this case the latest item added to the array was "Jas_iPhone" the way I know is because in HA I am manually setting the state of the
device_tracker
asnot_home
This post is deleted! -
Sorry, no, the question is, what is the rule by which Reactor is expected to know which one was most recently added? That is, is the order by which the list/array is built in any way deterministic?
-
My overall goal for this rule is to notify the last person that left home if they left a garage open. I have to use HA conditions in my actions because I am using actionable notifications which aren't possible to configure in MSR.
Triggers are if any of the devices are not in the
Pabla Home
zone and any of the 4 garages are left open...It will trigger text to be filled into
input_text
. The text that will be filled in is the expression of the latest device that was marked asnot_home
+ some editing to make it aservice
to be used in HA. Then run an automation in HA. (none of this is an issue, all works as expected)I have an expression that identifies any devices that are
not_home
. Here is where my question comes from: from the array ('away_devices') I would like to select the LAST device that wasnot_home
(not shown currently in the screenshot since I am not sure how to do so). In other words the device that was home last.After that I convert the output to a string, replace/add some characters to output this service name
notify.mobile_app_<insert device name>
. From there that service is sent to HA to send a notification to the specific device that was last home.In my expressions screenshot I have only one device shown as
not_home
so you can see the final output ofreplace
.Hopefully this adds some clarity, I didn't want to originally explain the whole rule since its fairly complex and a little hard to explain.
-
My overall goal for this rule is to notify the last person that left home if they left a garage open. I have to use HA conditions in my actions because I am using actionable notifications which aren't possible to configure in MSR.
Triggers are if any of the devices are not in the
Pabla Home
zone and any of the 4 garages are left open...It will trigger text to be filled into
input_text
. The text that will be filled in is the expression of the latest device that was marked asnot_home
+ some editing to make it aservice
to be used in HA. Then run an automation in HA. (none of this is an issue, all works as expected)I have an expression that identifies any devices that are
not_home
. Here is where my question comes from: from the array ('away_devices') I would like to select the LAST device that wasnot_home
(not shown currently in the screenshot since I am not sure how to do so). In other words the device that was home last.After that I convert the output to a string, replace/add some characters to output this service name
notify.mobile_app_<insert device name>
. From there that service is sent to HA to send a notification to the specific device that was last home.In my expressions screenshot I have only one device shown as
not_home
so you can see the final output ofreplace
.Hopefully this adds some clarity, I didn't want to originally explain the whole rule since its fairly complex and a little hard to explain.
@pabla If there's no deterministic order to the list you are building (and it certainly appears that there is not), then you have to use set arithmetic: when
not_home
changes, you compute the difference between its new value and a copy of its previous value that you've stored. This will give you a new set (array) with the new element(s), and these would be the last ones to leave the house.Here's a simple test rule you can try that demonstrates this. It uses the
arrayDifference()
function for the array comparison:The
last_not_home
variable is used to keep a copy of the previous contents of the array, updated every time it changes, after the difference is computed. The difference is stored in new_not_home, so this is where you will find the new element(s) added to the array. You can try this by having your Expressions page open in one window, and this rule's detail open in another, and watch what happens when you changenot_home
(assumed to be global here). -
What a brilliant solution! I think that did the trick, the only modifications needed was to make the
new_not_home
andlast_not_home
variables global so I can use them in the original rule. Initial testing looks like this did the trick. Thanks again Patrick! -
P Pabla marked this topic as a question on
-
P Pabla has marked this topic as solved on
-
T toggledbits locked this topic on