How To Randomize scenes

How would I go about randomizing scenes? Here’s what I’m thinking: create a bunch of scenes with no trigger. Then have a different scene with a trigger that for an action randomly chooses between one of the previously created scenes.

A scene can run another scene with some luup code.

You must create all the slave scenes, then in the master scene, you should run a math.random() ([url=http://lua-users.org/wiki/MathLibraryTutorial]http://lua-users.org/wiki/MathLibraryTutorial[/url]) to choose a number between 1 and “how many slave scenes you have”. After that, the number found is affected to a slave scene into an if/then/else if loop to launch the proper slave scene with the code below (replace the XXXX with the scene IDluup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "XXXX"}, 0)

Let me show you an exemple with 4 slave scenes with the id 4, 6, 7 and 11 :

[code]local tmp_random_number = math.random(1,4)

if (tmp_random_number == 1) then
scene_id_to_launch = “4”
elseif (tmp_random_number == 2) then
scene_id_to_launch = “6”
elseif (tmp_random_number == 3) then
scene_id_to_launch = “7”
elseif (tmp_random_number == 4) then
scene_id_to_launch = “11”
end

luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = scene_id_to_launch}, 0)[/code]

Or more concisely:

local scenes = {"4", "6", "7", "11"}
local tmp_random_number = math.random(1,4)
local  scene_id_to_launch = scenes[tmp_random_number]
luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = scene_id_to_launch}, 0)

OK. After studying these scripts I’m beginning to understand them a bit. Two questions though: What does the 0 indicate at the very end and how can I make one run at night and one during the day only? I currently have different greetings for day and night. Oh, and also, can I have it not fire a scene randomly some times? I’m thinking that I’d like to have space ship noises or barking dogs or something when turning on a light but only some times. Could I just list a bunch of scene ID’s that don’t exist?

EDIT: After browsing the tutorial isn’t the 1 in the math.random call unnecessary? Wouldn’t only the upper number need to be listed?

math.random(upper) generates integer numbers between 1 and upper.

@S-F,
All [tt]luup.call_action[/tt] invocations are executed against a particular deviceId, which is represented as the last parameter.

The [tt]RunScene[/tt] Action is exposed by the internal “Vera” device… which is deviceId=0.

[quote=“guessed, post:5, topic:173925”]@S-F,
All [tt]luup.call_action[/tt] invocations are executed against a particular deviceId, which is represented as the last parameter.

The [tt]RunScene[/tt] Action is exposed by the internal “Vera” device… which is deviceId=0.[/quote]

Thanks you sir. It all makes sense now.

Could I just list a bunch of scene ID's that don't exist?
[code] local scenes = {"4", "6", "7", "11"} local tmp_random_number = math.random(1,6) local scene_id_to_launch = scenes[tmp_random_number] if (scene_id_to_launch) then luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = scene_id_to_launch}, 0) end [/code] Here the random numeber is between 1 and 6. So there is a 66% probability it will fire one of the four listed scenese.

Ooooooh yeah. This is working like a charm! This is giving the house some personality!

About the day/night issue.

Just tacking

if ( luup.is_night() ) then return true else return false

On the beginning doesn’t work. The scene fails. How does one concatenate these two scripts?

[quote=“S-F, post:8, topic:173925”]About the day/night issue.

Just tacking

On the beginning doesn’t work. The scene fails. How does one concatenate these two scripts?[/quote]

It’s more of an insertion than a concatenation. Any code after a return doesn’t run, so you have to insert your actions between the if and the else.

if ( luup.is_night() ) then local scenes = {"4", "6", "7", "11"} local tmp_random_number = math.random(1,6) local scene_id_to_launch = scenes[tmp_random_number] if (scene_id_to_launch) then luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = scene_id_to_launch}, 0) end return true else return false end

Don’t forget that in Lua the end is mandatory. Your original if-statement is lacking it, making it a badly-formed program.

@futzle,

That code gives me a lua error.

Exactly what I have:

if ( luup.is_night() ) then local scenes = {"9","41", "42", "43"} local tmp_random_number = math.random(1,4) local scene_id_to_launch = scenes[tmp_random_number] luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = scene_id_to_launch}, 0) end return true else return false end

EDIT:

Removing the first “end” fixes the issue. I swapped the “return true” and the “return false” to create day only scenes. Wasn’t there talk of (luup.is_day) a while back? It’s probably in the new firmware which is slated for release somewhere around the time those pigs start flying.

[quote=“S-F, post:10, topic:173925”]That code gives me a lua error.
Exactly what I have:[/quote]

… Which is missing a line compared with my sample code.

I swapped the "return true" and the "return false" to create day only scenes.

You may also have to move your action code to after the else. The value you are returning will only affect any actions that the scene performs in the GUI (on the Devices tab). Those actions inside the if-statement are still happening at night, because that’s your condition.

Wasn't there talk of (luup.is_day) a while back?

if (not luup.is_night()) then

What exactly are you doing in these “scenes” that are randombly being chosen?

All the scenes have are commands for VerAlerts. For example, I have five scenes that say greetings for when I come home. I have another scene which is triggered by me entering my PIN which has the above code to randomly select one of the five scenes. So when I come home I get on of five prearranged greetings which is chosen at random.

I have many different triggers all built into one scene (garage, doorbell, vehicle etc) each with their own text announcements, and now (see my TTS Mad Libs post) they can randomize their response. Its a very clean setup.

Yes I like how simple you setup is. I would honestly rather not have to screw around with dozens of scenes to accomplish what I have but until I am reborn as a programmer this is what I need to do.

The thing about your madlib approach which I’m not feeling is that it’s still a set framework with just the nouns and verbs replaced. That would bore me after a while also. I have all kinds of things being said here. For example one of the scenes for my daughter entering is VerAlerts playing a very flatulent .mp3 and then saying “Oh S&*t, not you again”. This has played for her twice now and she’s gotten no end of enjoyment from it.

Lol, that’s great!
For my framework just delete all the extra text and use one variable and make that the entire message, done!

Ahhhhhh. I see. Is there a way to make it randomly decide to not do anything? Previously in this thread RTS illustrates how to do this with my current method. This is important because getting a message all the time is also boring. I mean, who always greets you as you walk into a room? Some times people are up to things and don’t immediately have the time. I could also create scenes which fire after a few minutes when the proverbial person is done with their highly involved task and can devote a minute to me I guess. That said I recently had to whack a bunch of announcements due to poor WAF. She was just getting pissed with constant rote computerized speak. I have to mix it up with sound files and randomization to: A. keep my hobby viable in my house and, B, not piss everyone (including myself) off.

I like the not say anything part. … keeps people on their toes!
Check out my latest post.
I added another phrase with the text as " " for nothing.
It’s the same result just consolidating into one scene
[URL=http://forum.micasaverde.com/index.php?topic=13921.0]http://forum.micasaverde.com/index.php?topic=13921.0[/URL]

See attached for single scene. Setup triggers and paste the luup code in each for actions.