Code Conundrum

You can’t print() the function’s local variables outside the context of the function–the local variables the function defines and uses aren’t there to print (most–you added a new one ORIGpos and didn’t declare it local, so it’s therefore a global and works somewhat by accident).

But there’s enough here to see where my error is… change st.position1 to st.shade.positions.position1 everywhere it’s used.

Then remove your other changes. The function should not return st, but rather:

   st = json.decode( res )
   return st.shade.positions.position1
end

Then your code needs to receive the returned value correctly:

pos = getStateStatus(42654)
print("Shade position is ", pos)

BINGO! thanks

I’m drawing a conclusion that the st.shade.positions.position1 change is because the status string shows that “position1” is a ‘subset’ of “positions”, which in turn is a subset of “shade”

In the future, I’ll need to keep track of those “{” brackets!

As for the “local” variables, I learned that lesson early on. I deliberately kept the variable non-local, along with the table though I also see that the “local” variable managed to come through the return statement.

Could you clarify the issue about the

“local _, res”
vs
“local status, res”

thanks again.

Correct! That was my fault–I rushed reading the response specs.

When a function returns more than one value (which is something Lua can do and is often used/handy), sometimes you don’t want to keep the first value(s), you only care about the second (or third, or fourth). You use “_” as the placeholder for ignored values. In your example, you could receive both status and res and ignore status just by not using it, and that’s fine. It’s really more of a style issue. There are some sanity-check tools that many of us use (luacheck for example) that will warn you about an unused value unless you use “_”, with the reasoning there being that you may have mis-typed the name (e.g. Status,res = get(...) followed by print(status) won’t work… notice the case change on the first letter–luacheck will call your attention to that by its message).

I didn’t realize that “status” in that use context was actually a variable! Another morsel!

One last question, I promise.
In the code there’s the line -

shadeStatus[testnum] = st.shade.positions.position1 or -1 – save current position

I understand we’re putting the contents of st.shade.positions.position1 into the shadeStatus array. What does the “or -1” do? Is it some sort of error check?

Yes. If it happens that position1 is not a member of positions, the result would be nil, which in the context of or is treated as boolean false, so the result is that -1 would result.

This statement would actually need to be written more carefully now that we’re dealing with a multi-level reference (as opposed to the old st.position1 that was previously there. So in practice, with the new deeper structure being traversed, this statement is unlikely to succeed in supplying a default on error. The much more complex = (((st or {}).shade or {}).positions or {}).position1 or -1 would be more correct. This somewhat daunting construct provides a default empty array at every step until the last, so if any part is missing, the default -1 results. Another way one might write that is:

if st and st.shade and st.shade.positions and st.shade.positions.position1
    shadeStatus[testnum] = st.shade.positions.position1
else
    shadeStatus[testnum] = -1
end

But the effect is the same.

Just a note of appreciation for both of you. I have newly installed HD Powerview and was also making my first dive into lua and luup. Your conversation answered so many questions for me and had made it “almost” trivial to write the code to talk to the PV hub and open/close/status of the shades.

THANKS a TON to both of you.