A more efficient way to do this?


#1

Hi everyone. I’m pretty new to WebCore but not to programming. I’ve learned a great deal in the last few weeks just reading questions and answers here. So a big thanks to everyone asking and answering questions.

This code simply gets the status of the batteries in all my devices and reports any that are below 20 percent. I have more devices that are not included in this code yet and have some more sensors ordered so before I go any farther, I was wondering if there was a more efficient way to do this. I would like to incorporate this as part of my “Alexa, start my day” routine.

Currently as you can see, I have an if statement for each device that populates a variable with any that matches the criteria. It works, and runs pretty fast but I was wondering if there was a simpler way to cycle through the devices and add text to the variable.

Any advice would be greatly appreciated!


#2

Hi @Bluzeman
I use this piston. (I have around 100 devices that use battery so I had to do it with a global variable)
the piston is originally written by @WCmore while working on my house. I just tweaked it a little bit.


#3

Thanks! I"m on my way out but I’ll look at that when I get back!


#4

I just got back and took a look at your code sample. I like that, much more efficient than the way I was doing it.

I’ve only been working with WebCore for around 3 weeks to a month and so I haven’t figured out how to set a global variable. I’ll look into it though. I’m also not exactly sure how your code cycles through them but I’ve only looked at it briefly. I’ll study it more in-depth later this evening.

Thanks a lot!


#5

When in editing mode, on the right side you will see “Add a new global variable” option.

if any device (belong to global variable) reports lower battery level than 20 (20 is my preference) that triggers the IF block. Piston records everything that falls into that category.
40%20AM


#6

Great! I feel kind of stupid now, it was right in front of me!

Got a busy day today but later on I’ll get on re-writing what I have.

Thanks Ike2018!


#7

Oh man, talk about stuff that were just in front of me that I didn’t see:)))) :joy::joy::joy:


#8

I just got time to play around with an attempt at using your suggestion. Works great! I want to incorporate the Alexa Speaks into it and when I’m finished I’ll post the final code I come up with.

I’m guessing the scope of the Global Variable is just the Piston you use it in correct?


#9

Global variables can be accessed by any pistons within the same webCoRe instance.


#10

Thanks. Right after I posted that I noticed it was available in another Piston I was working on.

Appreciate the input! This is a pretty cool platform, I’m enjoying the learning experience!