Code check (previous attempts don't work and I'm new to coding) [ANSWERED]

I’m using the following code to accomplish the following:

IF the nest is set to home AND the nest temperature is less than 70 deg F, THEN turn on a zwave switch (that controls the fireplace)

Would the following code accomplish that? I’m very new to coding and trying to figure it out and piece together what I can find :slight_smile:

local lul_tmp = luup.variable_get("urn:upnp-org:serviceId:HouseStatus1", "GetOccupancyState", 19) if (lul_tmp == "Occupied") then local lul_tmp = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "GetCurrentTemperature", 20) else end if (lul_tmp == "<70") then luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "1"}, 43) else end

Close, but the detail does matter.
It would be more like this:

local occupancy = luup.variable_get("urn:upnp-org:serviceId:HouseStatus1", "GetOccupancyState", 19)
if (occupancy == "Occupied") then
  local temperature = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "GetCurrentTemperature", 20)
  if (tonumber(temperature) < 70) then
    luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "1"}, 43)

Points to note:

[ul][li]all device variables are strings and need to be converted for numeric comparison[/li]
[li]meaningful variable names improve code readability[/li]
[li]the book “Programming in Lua” is worth a read[/li][/ul]

You got close…

You seem to be having issue with the format of the if/then/else/end structure AND with conditional comparisons AND with variable scope AND with UPnP variable names versus action names…

First, the if/then/else/end structure…

The conditional “else” is optional… and can be omitted for clarity…
Also, the structures can (and in your case should) be nested…

What you have coded is:
IF the occupancy state is “Occupied” then read the temperature into lul_temp
IF lul_temp is less than 70, turn on the switch

Your first occurrance of “else end” closes the first if structure… AND closes the scope for the local lul_temp variable you set inside the if structure…

Your second “if” statement is outside of the first “if” structure, and is totally independant of the logic you start in the first “if” statement.
At this point, even IF the temperature was obtained in the first “if” statement block, the block is no longer in scope, and the variable lul_tmp will still contain the value from the initial variable_get command.

You second “if” statement does a conditional comparison…
If the value of the comparison evaluates to TRUE, the commands will be executed…
Your conditional is “==”, so you are testing if the contents of the variable (in this case the representation of the temperature) is “<70”, assuming that it means “the value is less than 70”… You are actually saying "Is then value in lul_tmp the string “<70”…

The values returned by the variable_get function are (almost always) a string value, and the UPnP variables usually contain only the string representation of the actual value. (IE: a CurrentTemperature variable will hold “70”, or “85”, or “90”, etc)

To determine the value of a string representation of a number, you need to convert it to a number… Once converted to a number, you can do a numeric comparison…

local lul_string = "73"
local lul_number = tonumber(lul_string,10)  -- the "10" in this function is the number base to use
if (lul_number == 75) then
  luup.log("the string represents the number 75")
elseif (lul_number >= 70) then
  luup.log("the string represents a number of 70 or greater")
  luup.log("the string represents the number less than 70")

Lastly, the variable names you are using (“GetOccupancyState” and “GetCurrentTemperature”), look more like UPnP action names than UPnP variable names… The variable names are used in the variable_get function, wile the action names would be used in a call_action function.

You will find the variable names used by a device under the advanced tab on the devices setup page. You can also find the correct serviceId to use for the variable by hovering the mouse over the variable name.

Your code should look more like this:

local lul_tmp = luup.variable_get("urn:upnp-org:serviceId:HouseStatus1", "OccupancyState", 19)
if (lul_tmp == "Occupied") then
  local lul_temp = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 20)
  if (70 > tonumber(lul_temp,10)) then
    luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "1"}, 43)

You guys rock. I’m going to read this a few more times to let it sink in, each time I read that reply something new makes sense :). I see what I did wrong now with the end command.

I probably need to check out that book to find more out about the strings as that is a new term for me.