We have moved at community.getvera.com

Author Topic: [SOLVED] wason [check Waterpump for Sprinklers]  (Read 1627 times)

Offline nullx8

  • Hero Member
  • *****
  • Posts: 870
  • Karma: +9/-3
  • Trouble !
[SOLVED] wason [check Waterpump for Sprinklers]
« on: January 27, 2014, 09:20:46 am »
hope someone can help me on this .. there is a bug in the luup (i suspect at the vriable for the pump)

device 71 is my water pump and 158 is the sprinkler virtual switch.

i want the waterpump going back off (if it was off before)
everything works .. exept that the pump is no turning off.

Code: [Select]
local wason = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 71)

local cg = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1", "ConditionGroup", 26)
local c = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1", "Condition", 26)
local h = tonumber((luup.variable_get("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", 30)) or "0")

-- do this only if no rain
if  ( string.find(cg,"sleet") or string.find(cg,"rain")
  or string.find(c,"rain") or string.find(c,"thunderstorm") )
  and (h>69) then
    -- rainy .. doing nothing
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },158)
  return false
else
 -- water ON
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },71)
 
  -- sprinklers on
  -- rechts
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },158)
end

function soff51()
  if (wason == "0") then
     -- water back off .. because it was off
     luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },71)
  end
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },158)
  return true
end

luup.call_delay( 'soff51()', 15)

any idea ?
(the 15 sec are just for testing) ;)
« Last Edit: January 31, 2014, 04:16:19 am by nullx8 »
1 VeraLite, 2 RaspberryPI, Harmony Remote, openLuup, 85 Devices, 34Nodes, 63 Triggers, 49 Scenes

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-12
Re: wason not checking ..
« Reply #1 on: January 28, 2014, 03:29:02 am »
The function soff51() does not see the local variable wason when it is called as a global function by luup.call_delay(...). You could either make wason a global variable or (preferrred) pass it as the optional string parameter with luup.call_delay("soff51",15,wason) and change your function to:

Code: [Select]
function soff51(wason)
  if (wason == "0") then
     -- water back off .. because it was off
     luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },71)
  end
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },158)
  return true
end
« Last Edit: January 28, 2014, 04:26:06 am by RexBeckett »

Offline nullx8

  • Hero Member
  • *****
  • Posts: 870
  • Karma: +9/-3
  • Trouble !
Re: wason not checking ..
« Reply #2 on: January 28, 2014, 08:20:43 am »
i try that before posting here ...
but that gives me
Code: [Select]
LuaInterface::CallFunction_Timer-5 function soff51() failed attempt to call a nil value
tried a tonumber (strange array problem we had before) ... but that di not the trick either.


1 VeraLite, 2 RaspberryPI, Harmony Remote, openLuup, 85 Devices, 34Nodes, 63 Triggers, 49 Scenes

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-12
Re: wason not checking ..
« Reply #3 on: January 29, 2014, 03:57:36 am »
I tried this slightly modified version of your code. It works fine on my system.

Code: [Select]
local wason = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 71)

local cg = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1", "ConditionGroup", 26)
local c = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1", "Condition", 26)
local h = tonumber((luup.variable_get("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", 30)) or "0")

-- do this only if no rain
if  ( string.find(cg,"sleet") or string.find(cg,"rain")
  or string.find(c,"rain") or string.find(c,"thunderstorm") )
  and (h>69) then
    -- rainy .. doing nothing
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },158)
  return false
else
 -- water ON
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },71)
 
  -- sprinklers on
  -- rechts
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },158)
end

function soff51(oldstate)
  if (oldstate == "0") then
     -- water back off .. because it was off
     luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },71)
  end
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },158)
  return true
end

luup.call_delay( 'soff51', 15, wason)

Offline nullx8

  • Hero Member
  • *****
  • Posts: 870
  • Karma: +9/-3
  • Trouble !
Re: wason not checking ..
« Reply #4 on: January 29, 2014, 06:56:12 am »
I tried this slightly modified version of your code.

thanks alot ... it seems like the variable container inside and outside the function can not have the same name.
with your change to another name (oldstate) its working just perfectly!


thanks alot !
1 VeraLite, 2 RaspberryPI, Harmony Remote, openLuup, 85 Devices, 34Nodes, 63 Triggers, 49 Scenes

Offline nullx8

  • Hero Member
  • *****
  • Posts: 870
  • Karma: +9/-3
  • Trouble !
Re: wason [check Waterpump for Sprinklers]
« Reply #5 on: January 31, 2014, 04:15:57 am »
here is the complete script i came up with now.
maybe it has some use for someone else as well.

final version does store the status of the waterpump, starts the sprinklers, stop and restart them and at last turn the pump back off (if it was off before)
all of that happens only if
- no sleet
- no rain
- no thunderstorm
- and the humidity is less than 69

Code: [Select]
luup.log("##51: Sprinkler Controller")
local wason = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 71)
local cg = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1", "ConditionGroup", 26)
local c = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1", "Condition", 26)
local h = tonumber((luup.variable_get("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", 30)) or "0")

function s51end(oldstate)
  luup.log("##s51end oldstate: " .. oldstate)
  if (oldstate == "0") then
     -- water back off .. because it was off
     luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },71)
  end
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },158)
  return true
end

function s51on()
  luup.log("##s51on")
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },158)
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },71)
  return true
end

function s51off()
  luup.log("##s51off")
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },158)
  return true
end

-- do this only if no rain
if  ( string.find(cg,"sleet") or string.find(cg,"rain")
  or string.find(c,"rain") or string.find(c,"thunderstorm") )
  and (h>69) then
    luup.log("##s51 Rainy .. do nothing")
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },158)
  return false
else
  luup.log("##s51 No Rain Stating Sprinklers")
  luup.log("##s51 Waterpump Pre Status: " .. wason)
  luup.call_delay( 's51on', 2)
 -- water ON
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },71)
 
  luup.call_timer("s51off", 1, "25m")
  luup.call_timer("s51on", 1, "27m")
  luup.call_timer("s51end", 1, "50m", "", wason)
end

luup.call_timer("s51off", 1, "55m") -- this is just to be safe the sprinklers are really off
return true
« Last Edit: January 31, 2014, 04:18:31 am by nullx8 »
1 VeraLite, 2 RaspberryPI, Harmony Remote, openLuup, 85 Devices, 34Nodes, 63 Triggers, 49 Scenes