Lua Coding Advice

Is it possible to run a continuous Lua function along the lines of the following ?

Start
iterate over device list
do something e.g. log device status
suspend for some time period
return to Start

The above loop continues until some external event causes it to terminate.

I don’t know how threading is working inside Lua but I am assuming that some non-blocking mechanism is available for the loop suspension. A reasonable assumption? If so, how is it implemented?

You can achieve this within a scene but instead of looping run n minute.

There are options for threading when creating a plugin.

However, when utilizing the LUA windows for a scene (Either the lua for a trigger or the scene’s main lua window) you are left to define a single threaded block of lua code. If this block of code returns false … subsequent processing stops … if it returns true, or nothing ; subsequent processing continues.

Typical processing involves the following steps (As appropriate)

[ol][li]Trigger or Schedule or Button Click from the UI[/li]
[li]Triggers LUA Code[/li]
[li]Scene’s LUA Code[/li]
[li]Scene’s Commands[/li][/ol]

This code gets executed as a result of the corresponding trigger being initiated or the scene being activated. It is assumed that this LUA code will not run longer than 10 seconds … otherwise it will reset the Vera unit.

As Brientim has indicated … you can code this as a single block of lua code for a scene and setup a schedule for the scene that runs at a periodic interval. The smallest interval is 1 minute.

If I wanted to retain some state across calls from the scheduler, would I create a virtual device in which I would declare variables to hold that state?

You can use the variable container plugin. Then within the code just write this in as well.

It might be advantageous if you provided more details of what your doing and what you want to achieve as the desired outcome.

I have put together some code to send sms direct from Vera (via a usb /serial cable and old mobile). I am looking at how to run a heart beat check on the Internet to check if it is up. If not, and an event occurs which I want to be informed off, I will send an sms from the mobile rather than using email or sms gateway.

Hi @twostep - what you’re doing sounds vey interesting, are you able to share more? Maybe consider starting up a specific thread that highlights your project "SMS From Vera (via old mobile phone) " - I’m always interested to hear how people have integrated non HA devices into Vera.

BTW - I’m still waiting for the plugin where Vera will call me up and speak to me :wink:

Hi parkerc,

The phone I have only accepts sms messages in PDU format (a kind of 7 bit packing of the message text). The code to do this is no doubt unattractive from a Lua perspective as it is the first code I have written in Lua and my first attempt to write something in Vera. I am now waiting for a Wavecom gsm modem (a mobile with no keypad or screen but with a power supply so no battery) to test. Once I have tested with the modem I will post some documentation and the code.

I look forward to you next update :wink:

I’d personally like to see something that works wirelessly, requirements sent by IP to device and then converted to SMS and sent via the phone.

Have you also looked at Internet based SMS services as an option too?

I have not found any FREE gateways … that’s probably why MCV has a limit.
It costs them to send SMS messages.
Some Phone providers provide a gateway to phones connected to their service.
But that’s not universal … and many have a CAPTCHA challenge to make it impossible to use as a web service.

I have now received the Wavecom gsm modem and it works fine. I am in the UK so the MCV alert options are not so attractive. I have also tested with an sms gateway service using the company aql (www.aql.com). It has a pay as you go option so I bought 50 sms for £7.50 (calls to UK mobiles) and had it working from Vera within a couple of hours.

I have ordered another Wavecom for a friend that has a Vera. When I install it I will document the process and inventory all the files involved (the process will remind me what I have used as this is all very new) and post them as an attachment. I guess that is the way to do it?