Help Implementing Lua Code / "<=' logic in Reactor

Well, I am in the process of transferring my scenes over to reactor. I have a number of scenes which switch my heating on depending upon the outdoor temperature (average outdoor temperature over the past 4 hours). So basically I set up scenes to turn the heating on and off at times of the day but only if the outdoor temperature is below a set value. To do this in a scene I use the following lua code:

local HET = 873
local MSH = 872

local averageTemp = luup.variable_get(“urn:upnp-org:serviceId:VContainer1”, “Variable1”, MSH)
local rad_evening_on_temp = luup.variable_get(“urn:upnp-org:serviceId:VContainer1”, “Variable4”, HET)

if (tonumber(averageTemp) <= tonumber(rad_evening_on_temp))
then
return true
else
return false
end

Should I use this lua code? Would it work in Reactor the same way scenes execute lua code? or is there a good way to do this in a condition (i.e. <= condition based on two variables stored in containers)?

Thanks in advance.

[quote=“Talisker, post:1, topic:205239”]Well, I am in the process of transferring my scenes over to reactor. I have a number of scenes which switch my heating on depending upon the outdoor temperature (average outdoor temperature over the past 4 hours). So basically I set up scenes to turn the heating on and off at times of the day but only if the outdoor temperature is below a set value. To do this in a scene I use the following lua code:

local HET = 873
local MSH = 872

local averageTemp = luup.variable_get(“urn:upnp-org:serviceId:VContainer1”, “Variable1”, MSH)
local rad_evening_on_temp = luup.variable_get(“urn:upnp-org:serviceId:VContainer1”, “Variable4”, HET)

if (tonumber(averageTemp) <= tonumber(rad_evening_on_temp))
then
return true
else
return false
end

Should I use this lua code? Would it work in Reactor the same way scenes execute lua code? or is there a good way to do this in a condition (i.e. <= condition based on two variables stored in containers)?

Thanks in advance.[/quote]

First, apologies… I’m not sure how I missed this post, but I’m just seeing it this morning. So sorry!

You can do this in Lua if you wish, but Expressions may give you a result that works as well and is easier to debug/track/watch, once you get the hang of them.

  1. Add a variable called [tt]averageTemp[/tt] with the following expression: [tt]getstate( 872, “urn:upnp-org:serviceId:VContainer1”, “Variable1” )[/tt]

  2. Add a variable called [tt]rad_evening_on_temp[/tt] with [tt]getstate( 873, “urn:upnp-org:serviceId:VContainer1”, “Variable4” )[/tt]

  3. Add a variable with expression [tt]tonumber( averageTemp ) <= tonumber( rad_evening_on_temp )[/tt] (give it a name that makes sense to your purpose).

Then Save, and go back to the Status tab. There you will see your three variables listed with their current values (you may need to hit the “Restart” button to kick things off). The ease-of-use aspect I referred to earlier is here: with the values displayed on the status page, you can see what’s going on without adding debug/log statements and digging to find the messages. And of course, it’s possible to do the above as a single expression, if you’re into the whole brevity thing, but that fights the debugging and doesn’t add significantly to efficiency–do that when it enhances readability and maintainability of your logic.

From there, you can make a Reactor “service” condition in the ReactorSensor that looks at itself: just select the ReactorSensor itself as the device, and you’ll see these variables listed. Choose the result boolean you created in step 3. For operator and value, you can use equals (0 or 1), not equals, or is TRUE/is FALSE (recommended).

Two other tips:

  • There’s a “getstate()” insert tool among the buttons in the Expressions tab to help you build getstate() function calls. Give it a try.
  • Instead of the device number, you can pass the (quoted string) device name as the first argument to getstate(), and it will find the device by name.

Thanks rigpapa. With a bit of guidance from you all seems pretty straight forward!

One question. Are the expression variable names local to each reactor or are they global? i.e. can I use the same variable name in a number of reactors?

[quote=“Talisker, post:3, topic:205239”]Thanks rigpapa. With a bit of guidance from you all seems pretty straight forward!

One question. Are the expression variable names local to each reactor or are they global? i.e. can I use the same variable name in a number of reactors?[/quote]

Expression variable names are local to each ReactorSensor.