Set variable not working


#1

Hi guys,

Please see attached a snapshot of my piston.

Everything works on it, all the complex stuff - but for some reason the simpliest thing doesn’t work. In the first part it runs a section based on if "dontCheck = 0"in that section it tries to set dontCheck to 1 - for some reason this never works though and it won’t set it as 1.

If I test it in the last part (where it resets dontCheck to 0 again just after midnight each night) I can change it to 0 or 1 no problem there. But for some reason it won’t work in the section near the start - which runs all the other API checks and sets the battery level variable with no problems.

Any ideas why, is this a bug?


#2

dontCheck is not 0, it is currently blank.

Just change it to ‘if dontCheck is NOT 1’ instead, to populate a value into the variable on the first run.

You can always change it back later, value will stick.


#3

For some reason i’ve had to set batteryLevel as having no value, if I put in a default value or put it as an integer (as it is always a number) for some reason it never updates. Let me try with dontCheck now - it actually reports a blank 0 right now.


#4

If you set a variable value in the define section, it’s locked and will never change.

If you want a variable variable you have to set the value in the script, as you are currently doing.


#5

Cant be both, it’s either blank or has a 0 value.


#6

53

It’s showing as this - it reads it as 0. When I take a snapshot it’s just empty.

It runs because that section because it see’s dontCheck as 0 = but then it won’t change it to 1.

Just to test i’ve changed it to integer and obviously if you enter nothing it starts at 0, so again the section reads it as zero, I tell it set the variable to 1, it says it’s done that but it doesn’t’ change it to 1. This is what was happening with the batteryLevel too, so I left it as dynamic variable with just a blank value and it updates.

But dontCheck won’t update in the middle of the code there - only the timed oned at the end seems to be able to write to it.


#7

If you want to see a surprising (and humorous) example of a webCoRE contradiction, take a peek at this old post


Note:
I have not analyzed the code above, but that link showcases how sometimes 0 is not 0.
(if we are using dynamic types)


#8

It appears as 0 but shown as it is in the second screenshot above. It was the only way I could get the batteryLevel to update - to set as a dynamic variable with no value. However it’s not updating the dontCheck one whilst running the code in the middle.


#9

This is a red flag to me. (as alluded to in my last link)

At a quick glance, it appears that {dontCheck} is either 0 or 1.

If this is true, then I would be direct, by choosing a specific variable type (anything except dynamic), and avoid using zero…

IE: Make {dontCheck} a boolean, and then make the conditionals based on true or false.


#10

Ok that makes sense so I went for boolean and use true or false instead.

However the set variable in the middle piece of code that runs won’t set it to true - there are even two parts trying to set it to true and neither does it. This is surely a bug somewhere? There’s no reason they shouldn’t set it to true when that section runs is there?


#11

Maybe I need to put some more coffee in me… but this part sounds a bit confusing:

IF variable is false
Then
    Set variable to true
    Do other stuff
END IF

Wouldn’t the entire thing abort the instant the true command was sent?
(IE: Shouldn’t “Set variable to true” be the absolute last command in that block?)


#12

I mean I could do that - but in theory it wouldn’t abort because it’s already in the “then” block. However I will say its been both first, middle and last in the command and at no point does it set the variable to true so that’s my real problem here - it just won’t set the variable. The log says it is, but it isn’t doing anything.


#13

This is what I am understanding… Please correct me if I am wrong.

  • Two lines are not executing: “Set variable {dontCheck} = true
  • But lines afterwards are correctly executing. (IE log shows [0].state)

I am asking about “[0].state” specifically, because that is the only unique command you have after the Set variable command.


Thinking out loud here…

I wonder if the self-reference is kind of locking it, or preventing the contents from updating.
(like how most apps must be closed before updating)
Honestly, I am not sure how temporary memory is handled in webCoRE.

In general, I try to avoid sending a command that would change the conditions of the block I am currently in. There are many reasons for a piston to re-check those conditions, and cancel out if anything has changed. (making a web request or looping is only two of the many possibilities)

I suspect this is the root of the issue…


#14

I don’t believe this - in my very last command after all the percent and amount checks - if nothing is matched I set the charging to off… stupidly I also set the variable back to false there as well. Duh!

So it’s fixed now. I’ve left the variable in two places at the minute, I could probably clean up that second “for” section - I basically want to do what the first one is doing where it loops checking for the car to come online and repeats up to 4 times if it’s not getting a reading. I found in my tests that despite the car being online when checking the information (and pulling in the charge level) occasionally you’d get a groovy error and end up with (null) - so I decided to make another IF block to cycle through checking until null wasn’t there (if it existed in the first place) it doesn’t seem to happen very often, maybe 1 in 20 times but it’s required error handling as (null) breaks everything later on.


#15

Don’t feel bad…

If we don’t occasionally get a good “Face-Palm” moment, then we aren’t really pushing our limits…