Using weather plug in to switch off the light

I have moved the querry to this location

I am looking to use the weather plug in status ‘‘sunny’’ to swich off the light of an aquarium.
The light is activated in the morning and when the sky is clear and sunny the light can be switched off
If the status of the weather plug in changes back the light need to be switched on
In the evening by an event (time) it will be swiched off again

I need some guidance / help for this luup option
option should be only working from 0900 hrs until sunset

update;
I am working on the following luup, on which i would like your advise

local DEVICE_ID_WEATHER_PLUGIN = 20

=====
local condition = luup.variable_get(“urn:upnp-micasaverde-com:serviceId:Weather1”, “condition”,
DEVICE_ID_WEATHER_PLUGIN )

if (condition = “sunny”)

then

luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue = “0”}, 17 — aquarium off

else
return false
end

I can’t help you on the Weather Plugin code, but this is the code I use for my Kids remote to only allow them to power on the TV outlet between certain hours, if that helps.

[code]local startTime = “08:00”
local endTime = “20:30”

local hour = tonumber( startTime:sub( startTime:find(“%d+”) ) )
local minute = tonumber(startTime:sub(-2))

if hour and minute then
startTime = hour * 100 + minute
else
luup.log(“ERROR: invalid start time”)
return false
end

hour = tonumber( endTime:sub( endTime:find(“%d+”) ) )
minute = tonumber(endTime:sub(-2))

if hour and minute then
endTime = hour * 100 + minute
else
luup.log(“ERROR: invalid end time”)
return false
end

local currentTime = os.date(“*t”)
currentTime = currentTime.hour * 100 + currentTime.min

luup.log("startTime = " … startTime … "; currentTime = " … currentTime … "; endTime = " … endTime)

if startTime <= endTime then
if startTime <= currentTime and currentTime <= endTime then

luup.call_action( “urn:upnp-ap15e-com:serviceId:SND1”, “SendMail”, { subject = ‘F) Kids Room HA09 #5 TV On’, body = ‘Scene 51’ }, 158 )

    return true
end

else
if not (endTime < currentTime and currentTime < startTime) then
return true
end
end

return false[/code]

Also note that the variable is called “[tt]Condition[/tt]”, not “[tt]condition[/tt]”. These are case sensitive so that needs to be corrected.

The [known] values are listed on this page:
Installation-UI4 – Weather Plugin

These are also case-sensitive, so ensure they’re used with the exact case listed.

update

i have made the code as follows and have no error message any more’;

local DEVICE_ID_WEATHER_PLUGIN = 20

local condition = luup.variable_get(“urn:upnp-micasaverde-com:serviceId:Weather1”, “Condition”, DEVICE_ID_WEATHER_PLUGIN)

if (condition.find(“Clear”) ~= nil) or (condition.find(“Sunny”) ~= nil) or (condition.find(“Mostly Sunny”) ~= nil)

then

luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue = “0”}, 17) — aquarium off

else

return false

end

i have read in the weather plug in repley than the option also could be used

if (condition == “Sunny”)
or
if (condition.find(“Sunny”) ~=nil

which one is better?

[quote=“huib, post:4, topic:171768”]i have read in the weather plug in repley than the option also could be used

if (condition == “Sunny”)
or
if (condition.find(“Sunny”) ~=nil

which one is better?[/quote]
Either is reasonable. The latter would also trigger if Google decided to start returning “[tt]Not Sunny[/tt]”. It doesn’t seem likely as response but since their service is largely undocumented, anything’s possible 8)

Also note that it should be using a colon character like [tt]condition:find(“Sunny”)[/tt] or [tt]string.find(condition, “Sunny”)[/tt]. Not sure if the other one is really working as you’d expect it.

Here a better function that return 1 if the current time is between start and end times.

[code]function inTime(startTime, endTime)

local hour = tonumber(startTime:sub( startTime:find("%d+")))
local minute = tonumber(startTime:sub(-2))

if hour and minute then
    startTime = hour * 100 + minute
else
    luup.log("inTime: ERROR: invalid start time")
    return 0
end

hour = tonumber( endTime:sub( endTime:find("%d+") ) )
minute = tonumber(endTime:sub(-2))

if hour and minute then
    endTime = hour * 100 + minute
else
    luup.log("inTime: ERROR: invalid end time")
    return 0
end

local currentTime = os.date("*t")
currentTime = currentTime.hour * 100 + currentTime.min

local intervalTime = (endTime - startTime) % 2400
if (((currentTime - startTime) % 2400) < intervalTime) then
    -- I'm in
    return 1
end

-- I'm out
return 0

end
[/code]

i have made the luup as follows

the first if querry is if the condition is to swich off the light
the second querry is to swich on the light if beteen 0900 and 1800 if the weather is not good
but i got an error message
please advise

===========================
local DEVICE_ID_WEATHER_PLUGIN = 20 – weather plug in

local t = os.date(‘*t’)
local current_time = t.hour * 3600 + t.min * 60 + t.sec – time now
local begin_day_time = 9 * 3600 – 09:00
local end_day_time = 18 * 3600 – 18:00

local condition = luup.variable_get(“urn:upnp-micasaverde-com:serviceId:Weather1”, “Condition”, DEVICE_ID_WEATHER_PLUGIN)

  if ( condition == "Sunny" ) or ( condition == "Clear" ) or ( condition == "Mostly Sunny" )

then

  luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "0"}, 17) --- aquarium off

else
return false

  if ( condition == "Sunny" ) or ( condition == "Clear" ) or ( condition == "Mostly Sunny" )

then

  return false 

else
if ( current_time <= end_day_time ) and ( current_time >= begin_day_time )

then

  luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "1"}, 17) --- aquarium on if after 0900 or before 1800

  return false

end

update

the code as i have it now looks ok but drives me crazy (every time error message)
so your kind advise is requested

================================
local DEVICE_ID_WEATHER_PLUGIN = 20 – weather plug in
local t = os.date(‘*t’)
local current_time = t.hour * 3600 + t.min * 60 + t.sec – time now
local begin_day_time = 9 * 3600 – 09:00
local end_day_time = 18 * 3600 – 18:00
local condition = luup.variable_get(“urn:upnp-micasaverde-com:serviceId:Weather1”, “Condition”, 20)

 if (current_time <= begin_day_time)                        -- time before 0900

then
return false – do nothing
end – stop

else

 if (end_day_time <= current_time)                          -- time after 1800  

then
return false – do nothing
end – stop

else

  if ( condition == "Sunny" ) or ( condition == "Clear" ) or ( condition == "Mostly Sunny" )

then

  luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "0"}, 17) --- aquarium off
  return false

else
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue = “1”}, 17) — aquarium on
return true

end – stop

Your if… then… else…end blocks aren’t correct.

From the Lua documentation (Lua 5.1 Reference Manual)
if exp then block {elseif exp then block} [else block] end

If these don’t match correctly, then you’ll get syntax errors overall. As a first stab, try indenting your code somewhat to match the structure (and using the [ code ] … [ / code ] tag wrappers in postings) as it may become more obvious to you where the problem is.

Thanks guesed

I found it and it seems to work now
and also thanks for the link the lua site

==============

local DEVICE_ID_WEATHER_PLUGIN = 20 – weather plug in
local t = os.date(‘*t’)
local current_time = t.hour * 3600 + t.min * 60 + t.sec – time now
local begin_day_time = 9 * 3600 – 09:00
local end_day_time = 18 * 3600 – 18:00
local condition = luup.variable_get(“urn:upnp-micasaverde-com:serviceId:Weather1”, “Condition”, 20)

 if (current_time <= begin_day_time)                        -- time before 0900

then
end – stop

 if (end_day_time <= current_time)                          -- time after 1800  

then
end – stop

  if ( condition == "Sunny" ) or ( condition == "Clear" ) or ( condition == "Mostly Sunny" )

then

  luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "0"}, 17) --- aquarium off

else
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue = “1”}, 17) — aquarium on

end – stop

Note that the first two IF statements don’t stop the third one from running.

You can put the following statement
[tt]return true[/tt]

inside them if you want to stop.

the luup is running smoothly and well
so thanks for the advice and guidance and also from the fish

I’d like to turn on the sprinklers not just based on the rainfall in the past 24 hours, but also take the predicted precipitation into account. Can the Google Weather plugin access the predictions? (Google’s API does offer them IIRC).

The forward looking data isn’t currently exposed. Given the recent apparent shutdown of the feed (I hope this is an accident) I may have to switch feed providers so it may get added anyhow.