HELP NEEDED, why doesn?t this code work?

#1

Hello, I?m a newbie and got a lua code but it doesn?t work. The scene should stop dependent from the temperature, what?s wrong?
I?ve a Vera plus and the temp comes from a Fibaro FGK 101 with temp probe.
Any help would be appreciated, thanks!

local WE_DEV = 20 --add your temp device id here
local WE_SID = “urn:upnp-org:serviceId:TemperatureSensor1”
local Temp1 = luup.variable_get(WE_SID,“CurrentTemperature”,WE_DEV)
if (Temp1 < 24 ) then
return false – this will stop the scene
end

0 Likes

#2

The problem is that all Vera device variables are, in fact, strings. However, you are comparing the value to a number.

In order to fix this, you need to coerce the string to a number. Try this:

local WE_DEV = 20 --add your temp device id here
local WE_SID = "urn:upnp-org:serviceId:TemperatureSensor1"
local Temp1 = luup.variable_get(WE_SID,"CurrentTemperature",WE_DEV)
if (tonumber(Temp1)  < 24 ) then
 return false -- this will stop the scene
end
0 Likes

#3

Hello Akbooer,

Thanks, I?ve adjusted the code as you described but it still ignores the temperature, the scene still starts on the input time.

I?ve also checked the WE_Sid and that?s also exactly the same…
Anymore ideas??

Thanks!

0 Likes

#4

Do you have the right device ID?
What value does the variable_get actually return?
Any error messages in the log?

It’s a bit hard to debug without the complete code and contextual information.

0 Likes

#5

The Vera log is located at [tt]/tmp/log/cmh/LuaUPnP.log[/tt] but can also be accessed (sometimes) via HTTP request. A forum search should help you do this.

A useful topic for conditional scenes, such as this, is here: Conditional Scene Execution: Some Examples

0 Likes

#6

Hello AKBooer

Thanks for your advice !
I grabbed the code below from the Conditional scenes but after putting this code in the scene it doesn’t stop the time triggered device (fibaro switch) mounted on the pump when the temp is out of this range…

local dID = 166 – Device ID of your temperature sensor
local tLow = 18 – Lowest temperature of range
local tHigh = 22 – Highest temperature of range
local allow = true – true runs scene when in range, false blocks it
local tCurrent = tonumber((luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”,“CurrentTemperature”,dID)))
return (((tCurrent >= tLow) and (tCurrent <= tHigh)) == allow)

0 Likes

#7

Well, I preferred your original code (with tonumber), but both look as though they should work.

You are saying that if you run the scene manually, then it still runs when the temperature is out of limits?
How do you normally trigger the scene… timer?

If you add

luup.log ("MY TEMPERATURE READING: " .. (tCurrent or '?'))

before the return statement, then you should find this in the log to help further diagnostics.

You do have this code in the scene Lua, yes?

0 Likes

#8

Also have a look at the doco here:

http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_variable_get

and the section:

“Caution - Incorrect usages”

0 Likes

#9

[quote=“akbooer, post:7, topic:205898”]Well, I preferred your original code (with tonumber), but both look as though they should work.

You are saying that if you run the scene manually, then it still runs when the temperature is out of limits?
How do you normally trigger the scene… timer?

If you add

luup.log ("MY TEMPERATURE READING: " .. (tCurrent or '?'))

before the return statement, then you should find this in the log to help further diagnostics.

You do have this code in the scene Lua, yes?[/quote]

Hello Akbooer,

Both LUA code are working but not in the Vera scene with a daily scheduled trigger.

The trigger is a daily schedule (09:00 -10:00) then a device is trigged (pump on) and the earlier mentioned LuA code is started.
According the LUA code the scene is false so the device shouldn?t be trigged but it does…

Hope this clears, thanks

0 Likes

#10

I don’t understand your use of a time range in the scene schedule?

Do you have another scene, perhaps left over from previous attempts, which turns it on?

Any luck with trying to look at the log, which is what you need for these things?

0 Likes

#11

Hello Akbooer,

Attached my Vera log from scene 37 in PDF format…probably you understand it.
I don?t I?m sorry…

Cheers,
Berry

0 Likes

#12

Well, this would seem to be the smoking gun…

01 03/16/19 12515500.101 LuaInterface::CallFunction_Scene Scene 37 failed attempt to call a nil value <0x7655e520>

To me, this doesn’t look like a Lua scene error per se, more of an issue with the Luup engine activating the scene? You’ve already confirmed that the Lua code works stand-alone.

One final thing, though, I’m still not sure what your scene actually is? Can you share the output of this URL from a web browser?

http://YOUR_VERA_IP/port_3480/data_request?id=scene&action=list&scene=37

Perhaps time to share this with Vera support, and see what they make of it?

0 Likes

#13

Hi Akbooer,

There you go.
Thanks!

0 Likes

#14

OK, I take back what I said about the scene not running. It clearly does, looking more closely at the log:

08 03/16/19 12515500.100 Scene::RunScene running 37 Pomp aan 07500 als Temp >24 <0x7655e520>
01 03/16/19 12515500.101 LuaInterface::CallFunction_Scene Scene 37 failed attempt to call a nil value <0x7655e520>
08 03/16/19 12515500.101 JobHandler_LuaUPnP::HandleActionRequest device: 51 service: urn:upnp- org:serviceId:SwitchPower1 action: SetTarget <0x7655e520>
08 03/16/19 12515500.102 JobHandler_LuaUPnP::HandleActionRequest argument newTargetValue=1 <0x7655e520> 06 03/16/19 12515500.122 Device_Variable::m_szValue_set device: 51 service: urn:upnp-org:serviceId:SwitchPower1 variable: Target was: 0 now: 1 #hooks: 0 upnp: 0 skip: 0 v:0xf216d8/NONE duplicate:0 <0x7655e520>
08 03/16/19 12515500.125 JobHandler_LuaUPnP::AlarmCallback Ran scene:37 Pomp aan 07500 als Temp >24 / timer: next at 1552821300 now 1552734900 <0x7655e520>

The scene JSON you sent is this (showing the action and the timer):


{
  "Timestamp":1552734350,
  "encoded_lua":1,
  "groups":[{
    "actions":[{
      "action":"SetTarget",
      "arguments":[{
        "name":"newTargetValue",
        "value":"1"
      }],
      "device":"51",
      "service":"urn:upnp-org:serviceId:SwitchPower1"
    }],
    "delay":0
  }],
  "id":37,
  "last_run":1552734900,
  "lua":"bG9jYWwgV0VfREVWID0gMTY2IC0tYWRkIHlvdXIgdGVtcCBkZXZpY2UgaWQgaGVyZQpsb2NhbCBXRV9TSUQgPSAid XJuOnVwbnAtb3JnOnNlcnZpY2VJZDpUZW1wZXJhdHVyZVNlbnNvcjEiCmxvY2FsIFRlbXAxID0gbHV1cC52YXJpYWJsZV 9nZXQoV0VfU0lELCJDdXJyZW50VGVtcGVyYXR1cmUiLFdFX0RFVikKbHV1cC5sb2cgKCJNWSBURU1QRVJBVFVSRSBS RUFESU5HOiAiIC4uICh0Q3VycmVudCBvciAnPycpKQppZiAodG9udW1iZXIoVGVtcDEpICA+IDI0ICkgdGhlbgogcmV0dXJ uIGZhbHNlIC0tIHRoaXMgd2lsbCBzdG9wIHRoZSBzY2VuZQplbmQKCg==",
  "modeStatus":"0",
  "name":"Pomp aan 07300 als Temp >24",
  "paused":0,
  "room":"2",
  "timers":[{
    "days_of_week":"1,2,3,4,5,6,7",
    "enabled":1,
    "id":"1",
    "last_run":1552734900,
    "name":"",
    "next_run":1552821300,
    "time":"1231530",
    "type":2
  }],
  "triggers":[],
  "triggers_operator":"OR",
  "users":""
}

and the decoded Lua code is:

local WE_DEV = 166 --add your temp device id here
local WE_SID = "urn:upnp-org:serviceId:TemperatureSensor1"
local Temp1 = luup.variable_get(WE_SID,"CurrentTemperature",WE_DEV)
luup.log ("MY TEMPERATURE READING: " .. (tCurrent or '?'))
if (tonumber(Temp1)  > 24 ) then
 return false -- this will stop the scene
end

There is a mistake here. Your temperature variable is called [tt]Temp1[/tt], but you’re trying to log [tt]tCurrent[/tt]. Even so, there’s no record of your luup.log message which should, under these circumstances show as ‘?’.

Sorry, can’t help much further.

0 Likes

#15

[quote=“akbooer, post:12, topic:205898”]Well, this would seem to be the smoking gun…

01 03/16/19 12515500.101 LuaInterface::CallFunction_Scene Scene 37 failed attempt to call a nil value <0x7655e520> [/quote]

Thanks so far for your expertise Akbooer, Ill sent the Vera log file to Vera Support. Probably they can find out what the problem is.
One last question; The problem is the quote above right? The lua should call a nil but it doesn?t …

Cheers

0 Likes

#16

I’ve really no idea what that message means, or whether it’s common.

All I use Vera for these days is a Zwave Bridge. Everything else, including scenes, runs on a RPi running openLuup.

0 Likes

#17

[quote=“BerrytH, post:15, topic:205898”][quote=“akbooer, post:12, topic:205898”]Well, this would seem to be the smoking gun…

01 03/16/19 12515500.101 LuaInterface::CallFunction_Scene Scene 37 failed attempt to call a nil value <0x7655e520> [/quote]

Thanks so far for your expertise Akbooer, Ill sent the Vera log file to Vera Support. Probably they can find out what the problem is.
One last question; The problem is the quote above right? The lua should call a nil but it doesn?t …

Cheers[/quote]

I am no expert at all, but to me it looks more like nil is not a permitted value to call…

C

0 Likes

#18

Yes, indeed. But the question is where, exactly, is that call being made?

0 Likes

#19

Yes, indeed. But the question is where, exactly, is that call being made?[/quote]

Like I said, I’m no expert ;D

I get what little understanding I have of reading logs when stuff breaks

C

0 Likes

#20

[quote=“akbooer, post:12, topic:205898”]Well, this would seem to be the smoking gun…

01 03/16/19 12515500.101 LuaInterface::CallFunction_Scene Scene 37 failed attempt to call a nil value <0x7655e520> [/quote]

This error means that the code is trying to evaluating something that is a nil value as a function.

The only part of the code that could cause this is the conditional…

if (tonumber(Temp1)  > 24 ) then

If the Temp1 variable has a nil or non-numeric value, the tonumber function will evaluate to nil and the code will fail with the “attempt to call a nil value” error.

You can try changing the conditional to:

if ((tonumber(Temp1) or 0) > 24 ) then

which will ensure that the left hand side of the conditional will evaluate to a number and not fail.

Then you will need figure out why the variable Temp1 is not numeric…

The luup.variable_get function will only return a nil value or a string. It will return a nil value if the variable does not exist… The SID and Variable you are using is correct… so check that the device id for your temperature sensor is correct. If the variable has never been initialized (ie: new device), it will return an empty string… and the code change will catch that…

0 Likes