Author Topic: Combine Inet WGet with Conditional Execution  (Read 316 times)

Offline richardjgreen

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
Combine Inet WGet with Conditional Execution
« on: April 24, 2017, 11:02:29 am »
Hi all,

First of, apologies if the title is useless, I wasn't really sure how to word it.

I have two blocks of Luup code that both work independently however when I combine them, it generates a Luup error in Vera so hoping someone can tell me what glue I am missing to put them together? The first bit of code is from the excellent Conditional Scene Execution sticky that allows the scene only to run at certain times of day. The second bit of code is a Inet WGet to send a notification to a Plex PHT client.

I tried using an if statement wrapped around the Inet WGet so that if allow == true it would run but that didn't work.

Conditional Scene Execution:
Code: [Select]
local function doorbellDaytimeOnly()
local pStart = "06:45"   -- Start of time range to block doorbell.
local pEnd = "19:45"     -- End of time range to block doorbell.
local allow = true       -- true allows the scene to run, false blocks it.
local hS, mS = string.match(pStart,"(%d+)%:(%d+)")
local mStart = (hS * 60) + mS
local hE, mE = string.match(pEnd,"(%d+)%:(%d+)")
local mEnd = (hE * 60) + mE
local tNow = os.date("*t")
local mNow = (tNow.hour * 60) + tNow.min
if mEnd >= mStart then
return (((mNow >= mStart) and (mNow <= mEnd)) == allow)
else
return (((mNow >= mStart) or (mNow <= mEnd)) == allow)
end
end
return doorbellDaytimeOnly()

Plex PHT Notification
Code: [Select]
luup.inet.wget("http://1.2.3.4:3005/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22GUI.ShowNotification%22,%22params%22:{%22displaytime%22:20000,%22image%22:%22info%22,%22title%22:%22Front%20Door%22,%22message%22:%22Someone%20has%20rung%20the%20front%20doorbell.%22},%22id%22:1}")
Thanks in advance,
Richard
« Last Edit: April 24, 2017, 11:05:11 am by richardjgreen »

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9497
  • Karma: +721/-133
    • RTS Services Plugins
Re: Combine Inet WGet with Conditional Execution
« Reply #1 on: April 24, 2017, 12:36:16 pm »
You did not show how you combined them.

You can't put it AFTER the last return statement.

Offline richardjgreen

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
Re: Combine Inet WGet with Conditional Execution
« Reply #2 on: April 26, 2017, 06:41:09 am »
You did not show how you combined them.

You can't put it AFTER the last return statement.

Hey Richard. I guess how I did it is largely irrelevant as it didn't work  :'(.
The question should really be how should I combine them?

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9497
  • Karma: +721/-133
    • RTS Services Plugins
Re: Combine Inet WGet with Conditional Execution
« Reply #3 on: April 26, 2017, 08:44:34 am »
As I stated if you added the luup.inet ....   after the return of the other fragment then that is illegal  syntax!

There are multiple ways to integrate this:
1) luup.inet  runs all the time.
2) luup.inet runs only during the day
3) luup.inet runs only at night

So unless we see what your intentions are an what you are exactly trying to do ... we are only able to guess!

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 427
  • Karma: +15/-2
Re: Combine Inet WGet with Conditional Execution
« Reply #4 on: April 26, 2017, 11:46:01 am »
I'm going to guess that you want the luup.inet.wget to run whenever the check for daytime is true?  If so, you could try this.

Code: [Select]
local function doorbellDaytimeOnly()
local pStart = "06:45"   -- Start of time range to block doorbell.
local pEnd = "19:45"     -- End of time range to block doorbell.
local allow = true       -- true allows the scene to run, false blocks it.
local hS, mS = string.match(pStart,"(%d+)%:(%d+)")
local mStart = (hS * 60) + mS
local hE, mE = string.match(pEnd,"(%d+)%:(%d+)")
local mEnd = (hE * 60) + mE
local tNow = os.date("*t")
local mNow = (tNow.hour * 60) + tNow.min
if mEnd >= mStart then
return (((mNow >= mStart) and (mNow <= mEnd)) == allow)
else
return (((mNow >= mStart) or (mNow <= mEnd)) == allow)
end
end
if doorbellDaytimeOnly() then
    luup.inet.wget("http://1.2.3.4:3005/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22GUI.ShowNotification%22,%22params%22:{%22displaytime%22:20000,%22image%22:%22info%22,%22title%22:%22Front%20Door%22,%22message%22:%22Someone%20has%20rung%20the%20front%20doorbell.%22},%22id%22:1}")
    return true
else
    return false
end