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

If anyone is interested, I posted a series of videos on YouTube, final 2 of 6 below with my PLEG based AC diversion load project:

It’s been working quite well for the last few months. With it, I can implement a “grid zero” type system with my hybrid solar power inverter, soaking up any excess power in the diversion load. In the winter, that diversion load will be used to help heat the house with the excess power.

I’ve looked into Reactor and from what I can see, it can only deal with time intervals down to 1 minute. Is there any way to get down to say 10 or 15 second resolution in Reactor?

If you need to perform actions with delay between, resolution is one second. The one minute lower limit applies to the Interval condition type only, but that is not the only timing device in Reactor.

Thanks for the reply Patrick. I’m using a PLEG “timer” function to set up a callback mechanism in my automation code. I have power readings taken every 15 seconds or so. Based on that reading, my automation action code makes some computations and then may or may not change the setting of a device and then it re-starts that timer. When that timer interval expires, that event causes the action code run to check the results of the previous setting change against the current power reading and thus this repeats until the end of the schedule set up for that action, at which time the interval timer isn’t restarted. So is there a way to do something like this in Reactor?

You would do that differently in Reactor. A common way of people handling this is to create a time series of the power readings and react to thresholds in the average or trend, for example. Reactor is more event driven, so your implementation changes accordingly.

Best Home Automation shopping experience. Shop at getvera!

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