Issue with a variable dimmer logic action

I have a logic action to reduce a light dimmer (DiversionLoad1) percentage by a fixed amount (5% in this example) and also checks to make sure it doesn’t go below 0:

DiversionLoad1[142] SetLoadLevelTarget newLoadlevelTarget={((pDiversionLoad1 >= 5) and (pDiversionLoad1 - 5) or 0)}

When run, I end up with the literal code in the device parameter:

pDiversionLoad1 DiversionLoad1[142] LoadLevelStatus[urn:upnp-org:serviceId:Dimming1] {((pDiversionLoad1 >= 5) and (pDiversionLoad1 - 5) or 0)}

I have similar code that increases the dimmer percent (without the bounds check):

DiversionLoad1[142] SetLoadLevelTarget newLoadlevelTarget={(pDiversionLoad1 + 5)}

and that works as expected, every time it’s run, the dimmer increases by 5% and the pDiversionLoad1 device parameter has the numeric value of the dimmer, e.g. 5 after the first run:

pDiversionLoad1 DiversionLoad1[142] LoadLevelStatus[urn:upnp-org:serviceId:Dimming1] 5

Reason for the “less than 0” check is that the dimmer seems to jump up to 100% if you send it a negative number. I’ll likely need to add a “greater than 100” check in the in increase action. I’ve run the “less than 0” checking code in the Lua interpreter and it seems to yield the expected result. If I start at something like 6, it drops to 1, if I start at 4, it goes to 0, etc.

I’ve not worked with the logic expressions in actions before, so maybe I’m missing something obvious or perhaps there’s an easier way to do this. Any assistance appreciated.
Thanks!

I can run this code in the Lua.org interpreter and it works as expected:

pDiversionLoad1 = 4
result = ((pDiversionLoad1 >= 5) and (pDiversionLoad1 - 5) or 0)
print(result)

Tried a few other things. One was adding some increment/decrement functions to the PLEG startup lua, function returns a numeric value. Tried calling those from the action, for example {(do_increment(pDiversionLoad1, 5)} but I end up with that string in the device property, not the return value. Also tried putting the same function in the Action LUA code with the same result.

So for now, I’ve just added some range checking code to my condition logic to only increment or decrement the dimmer setting if the result will exceed the 0-100 range. That seems to be working.

I found some older posts that confirm you’re not able to access Lua functions or variables from the action logic section. Also, it seems that only “simple” expressions are allowed, so I can do {(setting + 5)} but much more than that and I just get the literal string and not the result of the expression.
Based on a few other older posts, I set up a Counter to keep track of the dimmer setting and then use Increment Counter (with a positive or negative) increment to modify the value. Then set the dimmer to the value of the counter.
So far that seems to be working well. I also have a self-trigger timer that runs after every dimmer set to trigger a read of the power on the circuit and then make another adjustment to the dimmer and repeat.

I can’t help wondering how many Vera owners still use the PLEG plug-in, which is no longer supported. Having switched to Reactor in 2019 and now Multi System Reactor (MSR), I’m much happier with its easy of automation, expressions and handling of things like Lua.

Thanks, I’ll have to check out Reactor and MSR. I have a lot of PLEG code from over the years in my system and it’s still working well. I’m in the process of trying to integrate my Vera system with my Synology NAS and it looks like there’s a Docker image of MSR that I might try out.

1 Like

Best Home Automation shopping experience. Shop at getvera!

© 2021 Ezlo Innovation, All Rights Reserved. Terms of Use | Privacy Policy | Forum Rules