Author Topic: Help with LUA code and suggestions  (Read 341 times)

Offline MikaelR

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Help with LUA code and suggestions
« on: August 15, 2017, 12:43:07 pm »
Hi, I have setup my mailbox sensor with the following code:

Code: [Select]
local SONOS_DEV = 113
local LS_SID = "urn:micasaverde-com:serviceId:Sonos1"
luup.call_action(LS_SID, "Say", {Text = string.format("Posten"),Language="sv",Volume=50}, SONOS_DEV)

I wonder if someone can help me with the completion of the code or other solution to the following.

As it is now, it talks to me in my sonos speaker every time someone lifts the lid to the mailbox.

I would like it so that when it tells me that there is mail, the next time you lift the lid to pick up the mail, it will not say anything, and this should only be done once a day between certain times.

I think this is the smartest solution, but please come with suggestions, as I have no knowledge at all in LUA programming, I am forever grateful for your help!

Offline jlind

  • Full Member
  • ***
  • Posts: 196
  • Karma: +7/-5
Re: Help with LUA code and suggestions
« Reply #1 on: August 15, 2017, 02:15:37 pm »
Code: [Select]
--Put in the startup lua script
gtblMyVariables = {}
gtblMyVariables.gblnHasBeenOpened = false

--Put in a scene that runs at midnight every day
gtblMyVariables.gblnHasBeenOpened = false

--Put in your scene that's triggered
local SONOS_DEV = 113
local LS_SID = "urn:micasaverde-com:serviceId:Sonos1"
if (gtblMyVariables.gblnHasBeenOpened ~= true) then
    luup.call_action(LS_SID, "Say", {Text = string.format("Posten"),Language="sv",Volume=50}, SONOS_DEV)
    gtblMyVariables.gblnHasBeenOpened = true
end


If the device reboots you may get another notification because the startup with reset the boolean value to "false".
VeraLite/VeraPlus with UI7, Multiple GE switches, GE Outlets, Aeon Smart Switches, Minimote, GE Portable outlets  Apps: (Pentair Autelis Plugin, Weather Underground, Honeywell WiFi Thermo, System Monitor, AlternateUI)

Offline MikaelR

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Re: Help with LUA code and suggestions
« Reply #2 on: August 19, 2017, 06:41:08 am »
Code: [Select]
--Put in the startup lua script
gtblMyVariables = {}
gtblMyVariables.gblnHasBeenOpened = false

--Put in a scene that runs at midnight every day
gtblMyVariables.gblnHasBeenOpened = false

--Put in your scene that's triggered
local SONOS_DEV = 113
local LS_SID = "urn:micasaverde-com:serviceId:Sonos1"
if (gtblMyVariables.gblnHasBeenOpened ~= true) then
    luup.call_action(LS_SID, "Say", {Text = string.format("Posten"),Language="sv",Volume=50}, SONOS_DEV)
    gtblMyVariables.gblnHasBeenOpened = true
end


If the device reboots you may get another notification because the startup with reset the boolean value to "false".

Thank you so much! This is working great!

I have thought a little. If you're not home.
Is it possible to get the message in the sonos speaker repeated every 15 minutes until the cover is lifted again, then it will not be read more that day?

Offline jlind

  • Full Member
  • ***
  • Posts: 196
  • Karma: +7/-5
Re: Help with LUA code and suggestions
« Reply #3 on: August 21, 2017, 10:19:57 am »
Of course it's possible.  Basically, you'll have to have 2 global parameters (or virtual switches/parameters which will keep persistant if rebooted) that signal when mail is delivered and when mail is picked up.  Create a timer routine that starts and sends the message once the "mail is delivered" variable is true and then shuts off if the "mail is picked up" parameter is true. 

Something this small and simple can all be done in the startup script if you wanted.  Here's how you call a timer:
Code: [Select]
luup.call_timer("fInit", 1, "15", "", "")

This would call a function called "fInit" in 15 seconds.  If you wanted minutes, modify it to be "15m".  Within the function, you'll have to check the parameters and recall the same function again.  Stop recalling the function when the mail has been picked up.  Remember, references to functions within the startup script need to be done after the function is created.  So in my startup I do this:

Code: [Select]
--Initiate all of my global variables

function fInit()
     luup.call_timer("fSetupTriggers", 1, "30", "", "")
end

function fSetupTriggers()
   
end

luup.call_timer("fInit", 1, "15", "", "") --Intializes variables that need to be polled, must be called after all the other functions are initialized.   


VeraLite/VeraPlus with UI7, Multiple GE switches, GE Outlets, Aeon Smart Switches, Minimote, GE Portable outlets  Apps: (Pentair Autelis Plugin, Weather Underground, Honeywell WiFi Thermo, System Monitor, AlternateUI)

Offline MikaelR

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Re: Help with LUA code and suggestions
« Reply #4 on: August 21, 2017, 12:53:39 pm »
Lol. Do not understand how you mean. I don?t understand lua coding :)

I'm using the plugin vera alert and wondering if I can limit this to just sending out 1 push message that there is mail. Now that I lift the lid the second time, I get one more.

Offline MikaelR

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Re: Help with LUA code and suggestions
« Reply #5 on: August 22, 2017, 08:51:59 am »
Or even better would be if the second push message from vera alert could tell me that the mail is retrieved, and then no more messages that day.

many thanks!

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9572
  • Karma: +729/-136
    • RTS Services Plugins
Re: Help with LUA code and suggestions
« Reply #6 on: August 22, 2017, 08:10:30 pm »
You could do this with PLEG.
The condition would trigger a nptification.

Sent from my SAMSUNG-SM-G935A using Tapatalk


Offline jlind

  • Full Member
  • ***
  • Posts: 196
  • Karma: +7/-5
Re: Help with LUA code and suggestions
« Reply #7 on: August 24, 2017, 09:27:42 am »
If you don't understand coding or Lua, I'd probably take Richard's suggestion and just use PLEG.  I've never used it personally, but it sounds like it's a good product.  Because you're getting into a more complicated script, me helping you code it would likely just cause more questions. 

If you really want to get into Lua, start by going through the tutorial online at:
https://www.lua.org/pil/1.html
VeraLite/VeraPlus with UI7, Multiple GE switches, GE Outlets, Aeon Smart Switches, Minimote, GE Portable outlets  Apps: (Pentair Autelis Plugin, Weather Underground, Honeywell WiFi Thermo, System Monitor, AlternateUI)