Can I Have webCoRE direct Output to the Last Active Echo Speaks Alexa Device?

The global variable is only there for me because I send messages from other pistons to that piston. If your piston and message are within the same piston, (like your example), then no global is needed.

1 Like

Instead of having that code duplicated so many times (once for every custom msg piston I want to create), I think I might have the updating happen in a separate piston, and just call it from each msg piston.

Is that a viable option for something like this?

If it works the way you want, then it’s viable. :slight_smile:

1 Like

Thanks for this thread. Took me a couple days of playing around but I finally got this working. I also learned some new techniques in Webcore.

2 Likes

lol haha, right?

@mebejedi
Hey, one more question…

What is this other local variable actually for?

device Echos =

I don’t see it referenced anywhere else (haha, unless I missed it).

Same here.
I always love these kinds of active learning sessions.
There is so much up in here that I wasn’t able to even take it all in on this go-around. So, I will be coming back at some point to go through it all again, and learn how to do some of the other stuff eventually.

OK, now there’s another quirky thing lol

When I split it into two pistons (one with the updating mechanism, and one with the speak command, etc), it acts really odd-like, in two very specific ways…

  1. The update is so delayed that it’s never ready in time for the speak command, even though I have a WAIT in the speak piston. However, the longest WAIT I’m able to have (because of #2 below) is 5 seconds, and that’s not long enough for the update to happen, and it never works right; it just speaks out of the one that was the current one at the time the piston fired. After it fires, if I run it again, then it works out of the correct one.

  2. If I make the WAIT longer than 5 seconds, the speaking part never happens at all. lol

So, unless somebody has any wisdom on why these things are happening, and how to mitigate, I will probably just have to have all of the code duplicated in every speak piston.

Oh well. It’s not ideal, but even with that goofiness (and the fact that it seems to only work on native Alexa devices, and not the Windows 10 Alexa app), this is still a really dang cool automation. :slight_smile:

I was probably going to have WC search the string for devices, but I ended up not doing that. It’s unnecessary in the piston. Go ahead and delete it.

Also, if you changed your pistons, it would help to see the new pistons.

And I’m still running Win7, so I can’t help with the Alexa app. :frowning:

1 Like

Right-on. Thank you.
It’s gone. :slight_smile:

Aha. Well, I tested just the section that pays attention to the Alexa device, and did it on multiple different kinds of Windows 10 computers, and on all of them, that trigger never happens. So, I assume that edition of the Alexa app is just not equipped to set that particular flag like the native ones are. No big deal.

Variable Updating Piston
20200117_webCoRE_EchoSpeaks_Current

Speaking Piston
20200117_webCoRE_EchoSpeaks_Welcome

Me-to-Alexa: “Thank you”
Alexa: turns on SmartThings switch (108 here)
webCoRE: runs Speaking Piston
Speaking Piston: calls Variable Updating Piston
Variable Updating Piston: updates global variable
Speaking Piston: waits 5 seconds (longest period possible without totally disabling the whole thing; when 5 seconds is already way too long to wait to get “You’re welcome” lol)
Speaking Piston: speaks a customized version (British voice, Emma) of “You’re welcome” to the last spoken to native Alexa device

My guess is that it’s not updating the global. When you setup an execute piston the text that appears mentions that globals in the calling piston don’t get updated until the piston is complete.

In my piston a Alexa routine sets a simulated switch. The switch triggers the piston and the first thing I do is call the piston to set the current Alexa device, mine is virtually identical to yours. It sets a global that contains the last echo spoken to.

Then I call a another piston that sends out the message to the echo I spoke to. Initially I was writing my message into a global variable that would be used in the speaking piston, but that did not work. This is not quite what you are doing but I think it failed for the same reason. The global variable that had the message did not get updated until the piston ended. So the message that got sent was the last value set in the global prior to running this piston. So I then changed the piston so that I passed the message to the speaking piston via an argument.

It then started working. So I think you will need to rearrange things. Your speaking piston calls the routine that sets the global CurrentAlexadevice, but it won’t get updated with the new device until the piston stops. So it may work sometimes if the global happens to contain the correct Alexa device when the piston starts.

This of coarse may be completely wrong but that is how I have interpreted what was happening to me.

3 Likes

I tend to agree. Globals are an odd beast, so I wouldn’t set up those pistons as @sgnihttrams did.

Try putting lines 25-32 of the Variable Update into the Speaking piston and remove the “Execute Piston” and see if that works.

If you really want a separate speaking piston, then you might be better off with my version.

First Piston:
If switch 108 changes to on, then @Message = “You’re Welcome”
Set variable @PistonTrigger = $Now
This puts the message in a global, and the change in pistontrigger activates the Speaking Piston

Speaking Piston:
If @PistonTrigger changes, then with Music Player 3 and Music Player 4 getDeviceActivity.
If Any of Music Player 3 and Music Player 4 wasLastSpokenTo is “true”, then save matching devices to (LastEcho)
With (LastEcho), speak (@Message)

Then, any other piston that you create can end with
@Message = “Whatever the message is”
Set variable @PistonTrigger = $Now

And will activate the Speaking Piston and send the @message to the appropriate device.

For example, while this piston designates the echos differently, it still sets up the message to be sent to the Speaking Piston:

1 Like

@Terminal is exactly correct in his analysis of global variables and why the pistons are behaving why they are. It’s documented behavior on the webcore wiki

1 Like

Thanks, everybody.

Due to my particular neuroatypicality, sometimes I need to take things in tiny bites. So, I’m going to pick one or two small areas to focus on here first, and see how that goes.

TBH, once in a while, I really wish I were able to have discussions like this in verbal form, because sometimes, trying to follow written comments like these (not that the difficulty is in the comments, but in me) is reminiscent of those ‘word picture’ math problems as a kid. I was like, OK, I already get the math itself, but then they put it in a context that has other levels of complexity for me (reading), and I stumble. lol

Anyway…
One point I think I’m hearing is that, even though I am calling another piston to update the global variable completely outside of my speaking piston, the variable isn’t getting updated before the speaking part, because (with the way I have it above) it can’t actually happen until the speaking piston is finished firing.

Regardless of whether I have that understood correctly (or whether I understand how to work around it correctly even if I do understand the problem correctly), I have another idea I’m going to try next to see how it helps (or doesn’t), and learn and go on from there.

I’m thinking something like this…

Me-to-Alexa: “Thank you”
Alexa: Routine turns on msg-specific-switch1
webCoRE: runs msg-specific-piston to put relevant msg into @MSG global variable
webCoRE: notices that msg global variable has changed and runs lastSpokenTo Variable Updating Piston (So, I guess @MSG changing is the trigger that causes the lastSpokenTo updater to run)
lastSpokenTo Variable Updating Piston: updates lastSpokenTo global variable
webCoRE: Variable UPDATED Piston notices the change in the global variable, and turns on switch2
webCoRE: notices switch2 turn on, and runs universal-Speaking Piston
Speaking Piston: (no delay needed, because the automation already has confirmation that there has been a change in the global variable) speaks @MSG to @lastSpokenTo device

I don’t know how much, if any, delays I may need in there, but I will try this as-is first and go from there.

I think I’m actually starting to see what is meant by some of the suggestions, and why some elements I wasn’t understanding before are in there.

So, at this point, either I have something that will work, or at least I’m a little further along in my learning process, and will be better able to build on this.

I will give this a try, and report back (Then, only after doing that, I will go through your suggestions again, and see if I can get more of my mind around more of those details)…

UPDATE:
Haha, OK, this clearly won’t work. lol

When I got around to building it out, I realized that, with the way I have it here, the automation will fire every time the lastSpokenTo global variable changes (which is obviously not desired).

@mebejedi
I’m thinking your use of $Now may be related to mitigating for this issue, but I’m just not totally clear yet on how that all works.

I’m going to try a different approach next, and maybe get back to this part later.

I have settled on using the combined version.
It works.

When I have more time and brain energy at some point, I will pick this back up, and work on it more to try to fully understand and then build my own version of this system that has a separate, centralized lastSpokenTo global variable updater piston and smaller speaking pistons, but for now, I will just have fat speaking pistons that have all of the necessary code right in them.

Thanks a lot to everybody for working with me on this.
How I have it for myself now is not perfect, but it does the trick, and that’s pretty freakin cool!

p.s. Even though the thing that did it for me was seeing it in @mebejedi 's piston screenshot (and actually importing it), I gave the ‘Solution’ to @nathancu , because, if I’m understanding it correctly, even though I didn’t understand it at the time, that is who first stated the concept in this thread. If that’s not right, please let me know.

Oh ya, here is a green screenshot of my final (for now) piston…

p.s. I modified the title of this thread to more accurately reflect what I actually wanted (AND what this thread is actually all about). :slight_smile:

2 Likes

@mebejedi
I’m looking through this again, and I think I’m getting it.
Building test pistons while watching the game. :slight_smile:

1 Like

So I am taking the easy road here and asking this question before I dig into the forums…:slight_smile:

Will I be able to use the Echo Speaks functionality to do the following :

Walk into a room and say “Alexa, play my eighties playlist”, and using webcore, be able to turn on speakers in that room? As opposed to “Alexa, play my eighties playlist in the kitchen”. In essence, can webcore know which Alexa received the command to play music?

I would say yes.

If echo_1: wasLastSpokenTo=true, then with speaker A, turn on.

If echo_2: wasLastSpokenTo=true, then with speaker B, turn on.

This is probably how Alexa links lights to Echos. Now with ST/WC, you can do it with anything. In my house, I have Fan, Master Fan, {Kid 1} Fan, and {Kid 2} fan. Now, each person could just say, “Turn on Fan” in whichever room they’re in. I’d have to rename Fan to Living Room Fan, and then have an Alexa routine for “Turn on Fan” that throws a switch in ST to launch the WC piston.

1 Like

You can definitely do this. That is why I searched for this, because I had a piston that I wanted to be able to activate in any room and respond in the room I ran it. It was a response and not music but that could work just as well. .

1 Like

@mebejedi

OK, I mapped it all out so I knew what I was going for, disabled all other related pistons, created my desired global variables, and built the two pistons, but it’s not working.

Here they are…

Message-specific piston:

Speaking piston:

I could post logs as well, but first, can you see anything in these that seems like it’s misconfigured?

I would not make the Alexa_AllNativeDevices a global variable. Globals should be used with caution. Besides, you don’t even use the variable on line 34 anyways (which wouldn’t work anyways, because the properties for a group are generic, rather than device-specific), so it’s unnecessary.

Try this:

Line 29
With Music Player 1, Music Player 2, Music Player 3, Music Player 4, Music Player 5, Music Player 9, Music Player 10, getDeviceActivity

[EDIT] I just realized…how the heck did you get getDeviceActivity with the global? Could you post a green piston snapshot so I can look at that line? It shouldn’t be possible.

[EDIT] Oh, I see. you set it for a single Echo, then go back and edit it with the variable. I wonder if that’ll work.

[EDIT] Nope…doesn’t work. Line 29 needs to list all the Echos.

Line 29
With Music Player 1, Music Player 2, Music Player 3, Music Player 4, Music Player 5, Music Player 9, Music Player 10, getDeviceActivity

1 Like

@mebejedi

I changed that one line over to using a global variable, simply because…I don’t know. lol
I’m always trying to make pistons smaller (even if only visually), and I thought using a variable there might be cool.

I confirmed, by trying it with the still working ‘combined’ version piston, that it in fact does NOT work with that in there like that (so I changed it back).

However, apparently, that issue was not the only thing keeping it from working, because it’s still not working after changing it back to the list format.

Anything else seem wrong?