We have moved at community.getvera.com

Author Topic: HELP NEEDED, why doesn?t this code work?  (Read 3880 times)

Offline BerrytH

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
HELP NEEDED, why doesn?t this code work?
« on: March 14, 2019, 05:28:42 am »
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

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: HELP NEEDED, why doesn?t this code work?
« Reply #1 on: March 14, 2019, 06:22:04 am »
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:

Code: [Select]
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
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline BerrytH

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
Re: HELP NEEDED, why doesn?t this code work?
« Reply #2 on: March 15, 2019, 04:43:26 am »
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!

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: HELP NEEDED, why doesn?t this code work?
« Reply #3 on: March 15, 2019, 04:54:16 am »
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.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: HELP NEEDED, why doesn?t this code work?
« Reply #4 on: March 15, 2019, 06:49:26 am »
The Vera log is located at /tmp/log/cmh/LuaUPnP.log 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
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline BerrytH

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
Re: HELP NEEDED, why doesn?t this code work?
« Reply #5 on: March 15, 2019, 08:00:36 am »
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)

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: HELP NEEDED, why doesn?t this code work?
« Reply #6 on: March 15, 2019, 09:11:59 am »
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
Code: [Select]
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?

3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 872
  • Karma: +66/-8
Re: HELP NEEDED, why doesn?t this code work?
« Reply #7 on: March 15, 2019, 06:15:46 pm »
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"


Offline BerrytH

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
Re: HELP NEEDED, why doesn?t this code work?
« Reply #8 on: March 16, 2019, 04:47:45 am »
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
Code: [Select]
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?

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




Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: HELP NEEDED, why doesn?t this code work?
« Reply #9 on: March 16, 2019, 06:23:48 am »
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?
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline BerrytH

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
Re: HELP NEEDED, why doesn?t this code work?
« Reply #10 on: March 16, 2019, 07:27:47 am »
Hello Akbooer,

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

Cheers,
Berry
« Last Edit: March 16, 2019, 07:33:11 am by BerrytH »

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: HELP NEEDED, why doesn?t this code work?
« Reply #11 on: March 16, 2019, 07:54:12 am »
Well, this would seem to be the smoking gun...

Code: [Select]
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?

Code: [Select]
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?
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline BerrytH

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
Re: HELP NEEDED, why doesn?t this code work?
« Reply #12 on: March 16, 2019, 08:26:14 am »
Hi Akbooer,

There you go.
Thanks!
« Last Edit: March 16, 2019, 08:39:55 am by BerrytH »

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: HELP NEEDED, why doesn?t this code work?
« Reply #13 on: March 16, 2019, 09:55:30 am »
OK, I take back what I said about the scene not running.  It clearly does, looking more closely at the log:

Code: [Select]
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):
Code: [Select]

{
  "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:

Code: [Select]
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 Temp1, but you're trying to log tCurrent.  Even so, there's no record of your luup.log message which should, under these circumstances show as '?'.

Sorry, can't help much further.


3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline BerrytH

  • Sr. Newbie
  • *
  • Posts: 25
  • Karma: +0/-0
Re: HELP NEEDED, why doesn?t this code work?
« Reply #14 on: March 16, 2019, 10:33:53 am »
Well, this would seem to be the smoking gun...

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


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
« Last Edit: March 16, 2019, 10:50:29 am by BerrytH »