Expression to randomly run Global Reactions ?
-
OK understood I will change the expression back to how it was and remove the set variable in the set reaction area.
I will increase the trigger repeat to 5 minutes and test it one evening when its dark outside.
Ideally I'd like for a different colour each time to be selected, but I guess the expression for the random numbers could select the same number again and therefore the colour would not change for that cycle.
@cw-kid said in Expression to randomly run Global Reactions ?:
Ideally I'd like for a different colour each time to be selected, but I guess the expression for the random numbers could select the same number again and therefore the colour would not change for that cycle.
I use this to generate a random number that has not been used last time. In the reaction use express variable randomVal, once the reaction is over set randomVal to
${{int(split(join(split("1234567", randomVal || 0),""),"")[floor(random()*6)])}}
Then every time you get a different number. -
You've got me curious now: What make/model of garden lights do you have? And what is their ultimate color palette? Have you tried setting them to intermediate colors using the [Set_Color_RGB] action in MSR, for instance?
-
@cw-kid said in Expression to randomly run Global Reactions ?:
Ideally I'd like for a different colour each time to be selected, but I guess the expression for the random numbers could select the same number again and therefore the colour would not change for that cycle.
I use this to generate a random number that has not been used last time. In the reaction use express variable randomVal, once the reaction is over set randomVal to
${{int(split(join(split("1234567", randomVal || 0),""),"")[floor(random()*6)])}}
Then every time you get a different number. -
@elcid said in Expression to randomly run Global Reactions ?:
I use this to generate a random number that has not been used last time
That's what I needed!
@cw-kid This is probably more efficient
${{int(substr(replace("1234567", randomVal || 0,""),floor(random()*6),1))}}
Then I looked at moving this to MSR as I have it in Reactor, . I found the remove function and this is now easily expanded to larger number and you can easily set a range
${{remove( 1..7, randomVal - 1)[floor(random()*6)]}}
@toggledbits , possible bug
list(1..7]) gives [[1,2,3,4,5,6,7]] a nested array.
Is that expected. -
@cw-kid This is probably more efficient
${{int(substr(replace("1234567", randomVal || 0,""),floor(random()*6),1))}}
Then I looked at moving this to MSR as I have it in Reactor, . I found the remove function and this is now easily expanded to larger number and you can easily set a range
${{remove( 1..7, randomVal - 1)[floor(random()*6)]}}
@toggledbits , possible bug
list(1..7]) gives [[1,2,3,4,5,6,7]] a nested array.
Is that expected.@elcid said in Expression to randomly run Global Reactions ?:
@toggledbits , possible bug
list(1..7]) gives [[1,2,3,4,5,6,7]] a nested array.
Is that expected.Yes, because
1..7
on its own produces an array. So wrapping it inlist()
gives you an array containing an array.FWIW,
list()
is basically deprecated, because lexpjs lets you use common array notation (e.g.[1,2,3,4,5,6,7]
) to define an array. -
@cw-kid This is probably more efficient
${{int(substr(replace("1234567", randomVal || 0,""),floor(random()*6),1))}}
Then I looked at moving this to MSR as I have it in Reactor, . I found the remove function and this is now easily expanded to larger number and you can easily set a range
${{remove( 1..7, randomVal - 1)[floor(random()*6)]}}
@toggledbits , possible bug
list(1..7]) gives [[1,2,3,4,5,6,7]] a nested array.
Is that expected.@elcid said in Expression to randomly run Global Reactions ?:
@cw-kid This is probably more efficient
${{int(substr(replace("1234567", randomVal || 0,""),floor(random()*6),1))}}
Then I looked at moving this to MSR as I have it in Reactor, . I found the remove function and this is now easily expanded to larger number and you can easily set a range
${{remove( list(1..7)[0], randomVal - 1)[floor(random()*6)]}}
GENIUS! Gonna borrow this.
@toggledbits , possible bug
list(1..7]) gives [[1,2,3,4,5,6,7]] a nested array.
Is that expected.Yes, that's expected, since
1..7
itself creates an array – namely,[1,2,3,4,5,6,7]
– whilelist()
(now deprecated = avoid in MSR) also turns its argument into an array. I suggest omitlist
and see if things improve. -
@elcid said in Expression to randomly run Global Reactions ?:
@toggledbits , possible bug
list(1..7]) gives [[1,2,3,4,5,6,7]] a nested array.
Is that expected.Yes, because
1..7
on its own produces an array. So wrapping it inlist()
gives you an array containing an array.FWIW,
list()
is basically deprecated, because lexpjs lets you use common array notation (e.g.[1,2,3,4,5,6,7]
) to define an array.@toggledbits just discovered that as you posted, is it mentioned in the docs, as I had not come acreoss it?
-
Yup. That's in the docs.
-
Yup. That's in the docs.
@toggledbits
Its a bit confusing, i read this
"[5,7,9], so this function is now obsolete and may be removed later)"
and tried [1..7], when that failed, I assumed the only way to spread was list(1..7)
May be add a note about 1..7 being the same as list(1..7). As I found it some what unclear. -
@toggledbits
Its a bit confusing, i read this
"[5,7,9], so this function is now obsolete and may be removed later)"
and tried [1..7], when that failed, I assumed the only way to spread was list(1..7)
May be add a note about 1..7 being the same as list(1..7). As I found it some what unclear.@elcid , it says:
I think this is pretty clear. The range operator is documented separately.
May be add a note about 1..7 being the same as list(1..7).
But it's not. That's how we got started.
-
@elcid , it says:
I think this is pretty clear. The range operator is documented separately.
May be add a note about 1..7 being the same as list(1..7).
But it's not. That's how we got started.
@toggledbits said in Expression to randomly run Global Reactions ?:
May be add a note about 1..7 being the same as list(1..7).
But it's not. That's how we got started.
I mean 1..7 being what I assume list(1..7) would produce a single array. That's why I found it confusing, I think that I expected a array to have [ ] around them.
Maybe add "also see range operator". -
@toggledbits said in Expression to randomly run Global Reactions ?:
May be add a note about 1..7 being the same as list(1..7).
But it's not. That's how we got started.
I mean 1..7 being what I assume list(1..7) would produce a single array. That's why I found it confusing, I think that I expected a array to have [ ] around them.
Maybe add "also see range operator".@elcid said in Expression to randomly run Global Reactions ?:
I mean 1..7 being what I assume list(1..7) would produce
Funny you should mention this, since all yesterday, I grappled with the question, "How can I find the minimum of an array in MSR?" (Answer: Not easily at all!) Turns out the
min()
function only works on a list with 2 elements, not arrays of arbitrary length. In other words,min(1,2,3)
yields1
without a hitch, whereasmin([1,2,3])
returnsnull
(for good reason: There's no guarantee the argument will always be a 1-dimensional numeric array!). Same formax()
.Which led me to the realization that there's no direct way in MSR to "convert" an array into a list. Seems your brain encountered the same type of mental collision while trying to work out your imported expression.
-
@elcid said in Expression to randomly run Global Reactions ?:
I mean 1..7 being what I assume list(1..7) would produce
Funny you should mention this, since all yesterday, I grappled with the question, "How can I find the minimum of an array in MSR?" (Answer: Not easily at all!) Turns out the
min()
function only works on a list with 2 elements, not arrays of arbitrary length. In other words,min(1,2,3)
yields1
without a hitch, whereasmin([1,2,3])
returnsnull
(for good reason: There's no guarantee the argument will always be a 1-dimensional numeric array!). Same formax()
.Which led me to the realization that there's no direct way in MSR to "convert" an array into a list. Seems your brain encountered the same type of mental collision while trying to work out your imported expression.
-
What makes the expression run and generate a new number after a period of time?
This is what I have so far.
Trigger is the virtual switch in Vera.
Pulse every 1 seconds and repeat every 60 seconds.Set Reaction - Groups that look at the expression named "ChooseColour" and its current number value
and then runs a Global Reactionnow directly sets that colour on the LED strip.The random number generator expression named "ChooseColour" this is a rule expression not a Global Expression.
I added an 1 minute interval to the triggers and the virtual switch is true trigger is also pulsing every minute.
The expression is now changing numbers every minute, but the reactions don't seem to be running as the colour is stuck and not changing.
Also I think this expression generates the number zero which I don't have assigned to any reaction.
@cw-kid Just so you know, I have a "Color Cycler" rule that uses the [Entity Action] [Set Color RGB] action with this expression inside each field ("R", "G" and "B"):
${{ floor(random()*255) }}
which makes the associated light (a Hue bulb) adopt a perfectly random color. Just something you might consider trying.
-
Thanks I will try the "Color Cycler" in a brand new rule to test it out.
I haven't as yet looked at updating my current rule with the new expression to always guarantee a new random number that hasn't been repeated etc.
I will have a look over the weekend. Sure I will have questions.
Cheers
-
@cw-kid This is probably more efficient
${{int(substr(replace("1234567", randomVal || 0,""),floor(random()*6),1))}}
Then I looked at moving this to MSR as I have it in Reactor, . I found the remove function and this is now easily expanded to larger number and you can easily set a range
${{remove( 1..7, randomVal - 1)[floor(random()*6)]}}
@toggledbits , possible bug
list(1..7]) gives [[1,2,3,4,5,6,7]] a nested array.
Is that expected.@elcid said in Expression to randomly run Global Reactions ?:
remove( 1..7, randomVal - 1)[floor(random()*6)]
I've just tried using this in my rule set, I added it as the expression for the rule. When I press the play button next to it, it seems to work and changes to different numbers between 1 to 7 etc.
However when I restart Reactor it says it cannot find the Global Expression named "randomVal".
So I then created it as a Global Expression, the error has now gone away, but I don't understand what this Global Expression is for?
Thanks
-
-
T toggledbits locked this topic on