Clearing a list?


#1

@ady624 i know you have presence on your mind and fingers right now which is awesome! but i was running in to the need for a way to clear list[] variables and also seen others on the forum asking about how to clear list. i added these 3 lines of code to webcore-piston and if i do set variable listName[*CLEAR] it seems to clear the list[] variable without any secondary issues … that i can tell. :slight_smile:

        if (var.index == "*CLEAR")	{
        	variable.v.clear()
        } else {

any unexpected side effects i should be aware of? heres the full method for quick context:

private Map setVariable(rtData, name, value) {
	def var = parseVariableName(name)
	name = sanitizeVariableName(var.name)
	if (!name) return [t: "error", v: "Invalid empty variable name"]
	if (name.startsWith("@")) {
    	def variable = rtData.globalVars[name]
    	if (variable instanceof Map) {
        	//set global var
            variable.v = cast(rtData, value, variable.t)
            Map cache = rtData.gvCache ?: [:]
            cache[name] = variable
            rtData.gvCache = cache
            return variable
        }
	} else {    	
		def variable = rtData.localVars[var.name]
        if (variable instanceof Map) {
            //set value
            if (variable.t.endsWith(']')) {
            	//we're dealing with a list           
                variable.v = (variable.v instanceof Map) ? variable.v : [:]
                if (var.index == "*CLEAR")	{
                	variable.v.clear()
                } else {
	                Map indirectVar = getVariable(rtData, var.index)
                //indirect variable addressing
	                if (indirectVar && (indirectVar.t != 'error')) {
    	                var.index = cast(rtData, indirectVar.v, 'string', indirectVar.t)
        	        }                
                	variable.v[var.index] = cast(rtData, value, variable.t.replace('[]', ''))
                }
            } else {
            	variable.v = cast(rtData, value, variable.t)
            }
            if (!variable.f) {
            	def vars = state.vars
                vars[name] = variable.v
                state.vars = vars
                atomicState.vars = vars
            }
            return variable
            
		}
	}
   	result = [t: 'error', v: 'Invalid variable']
}

Lists & Arrays - documentation?
Deleting an array element
Need your feedback on a new key-value piston input type in webCoRE
#2

Thanks @bangali, I needed to clear list variables too.

I assume it clears both keys and values right?


#3

sure thing. yep.


#4

I took your code and updated the local version of the piston app, published it for me, and changed a piston and it didn’t work. I ended up with a list entry with a key of *CLEAR. What did I do wrong?


#5

just verified again using this piston seems to continue to work: (posted image with the trace version so you can see that strings is cleared post execution)

can you please post the code for the updated setVariable method? thanks


#6

Here is the updated code:

private Map setVariable(rtData, name, value) {
def var = parseVariableName(name)
name = sanitizeVariableName(var.name)
if (!name) return [t: "error", v: "Invalid empty variable name"]
if (name.startsWith("@")) {
	def variable = rtData.globalVars[name]
	if (variable instanceof Map) {
    	//set global var
        variable.v = cast(rtData, value, variable.t)
        Map cache = rtData.gvCache ?: [:]
        cache[name] = variable
        rtData.gvCache = cache
        return variable
    }
} else {    	
	def variable = rtData.localVars[var.name]
    if (variable instanceof Map) {
        //set value
        if (variable.t.endsWith(']')) {
        	//we're dealing with a list           
            variable.v = (variable.v instanceof Map) ? variable.v : [:]
            if (var.index == "*CLEAR")	{
            	variable.v.clear()
            } else {
                Map indirectVar = getVariable(rtData, var.index)
                //indirect variable addressing
                if (indirectVar && (indirectVar.t != 'error')) {
                    var.index = cast(rtData, indirectVar.v, 'string', indirectVar.t)
                }
            }
            variable.v[var.index] = cast(rtData, value, variable.t.replace('[]', ''))
        } else {
        	variable.v = cast(rtData, value, variable.t)
        }
        if (!variable.f) {
        	def vars = state.vars
            vars[name] = variable.v
            state.vars = vars
            atomicState.vars = vars
        }
        return variable
        
	}
}
result = [t: 'error', v: 'Invalid variable']

}

And this is my crappy code - This is a test piston I was working on to figure out when a door contact was last opened/closed versus when a presence sensor indicated we last arrived home. The whole purpose is to put this code into another piston I have that does a number of things if we arrive home but haven’t entered the house. For example, unlocking the door when motion is detected from my Ring Pro, pushed a button on the garage door and open it.


#7

thanks. let me compare the code.

meanwhile, would you mind running the simple example i posted above and post the results from it? thanks.

if you want to import it the piston restore code is ztcr


#8

And you don’t have to… I screwed up

I had the closing bracket on your new code BEFORE the evaluation of the variable.v = cast(rtDate, value, variable.t)

Fixed it and reran my test and it worked great

Sorry!


#9


#10

it wouldnt be sunday morning without a little closing bracket mishap :slight_smile:

good job spotting it. if you find any glitches using it do please let me know. i wouldnt want to leave this thread/code sitting out there if there is any sort of a glitch.

thank you.


#11

thanks, yes found that.


Stuffing a list: *ALL
#12

Just curious, was anything like this ever implemented? I was wondering if something like this (and STUFF) might be available.


#13

i have both implemented on my local version of webcore-piston.

https://community.webcore.co/t/stuffing-a-list-all/1368?u=bangali


#14

Ok, thanks. I was wondering if it had been officially adopted or if I need to implement it manually.


#15

nope, not officially adopted.


#16

Do you know if it’s on track to be officially adopted? I have a piston in mind that depends on whether this is implemented at some point or not. Thanks!


#17

@ady624 or @ipaterson?


#18

Improving list operations is pretty high up in my wishlist as well. This is a convenient workaround since the patch is small but I wouldn’t expect this specific syntax to be published as an official update. An approach to “list operations” (of which Clear List is one of the simplest and most crucial so would probably be among the first to get added) would need to be properly integrated into the UI especially for operations that require parameters like push and unshift.

Definitely no timeline for that from my end but I also feel the pain of lists being awesome but falling short on occasion due to things like this.

@bangali, do you have a fork of webCoRE that folks who need this could switch to for updates? Otherwise I’m sure it’s a bit inconvenient for everyone to re-apply this patch on each webCoRE smart app update.


#19

the fun part is my changes requires no further change to the UI and works consistently.

nah dont want to do a fork thats likely an inconvenience for most users. that would also need to be kept updated on wC smart app updates.

secondly that would also tempt me to keep adding other stuff which i think could be done better. want to avoid that temptation when it comes to webcore.


#20

Absolutely, I’m glad we’re on the same page.