Author Topic: Yale Smart Alarm Sensor Status - Custom Vera Plugin  (Read 489 times)

Offline OliA

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
Yale Smart Alarm Sensor Status - Custom Vera Plugin
« on: April 05, 2017, 04:58:34 am »
I'm trying to integrate my Yale Smart Alarm sensors in to Vera, essentially replicating what Daniel McDougall produced for Smartthings.

I have very little programming knowledge but have managed to write some code that can retrieve the status of a specified door sensor by pulling the information from the Yale hub using http and a cookie for authentication.

I now want to create a device in Vera that can show the status of the sensor. I'm hoping that it can re-run the code every x seconds to refresh the status and essentially act like a z-wave door sensor. I've had a look through some existing plugins but I'm finding them hard to understand.

Any help in putting this together would be most appreciated.

Code: [Select]

-- **************************************************************************
-- USER INPUTS
-- **************************************************************************
local YaleDeviceID = 3
local UsrID = "xxx"
local UsrPass = "xxx"
local URL1 = "https://www.yalehomesystem.co.uk/homeportal/api/login/check_login/"
local URL2 = "https://www.yalehomesystem.co.uk/homeportal/api/panel/get_devices/"


-- **************************************************************************
-- LOGIN TO YALE HUB AND RETREIVE ALL YALE DEVICES AND STATUS IN JSON STRING
-- **************************************************************************
curlcommand1 = "curl -c '/tmp/log/cmh/cookie.txt' -d 'id="..UsrID.."&password="..UsrPass.."'  "..URL1
local handle1 = io.popen(curlcommand1)
local result1 = handle1:read("*a")
handle1:close()

curlcommand2 = "curl -b '/tmp/log/cmh/cookie.txt' -d 'id="..UsrID.."&password="..UsrPass.."'  "..URL2
local handle2 = io.popen(curlcommand2)
local result2 = handle2:read("*a")
handle2:close()


-- **************************************************************************
-- PARSE YALE DEVICE STATUS JSON STRING
-- **************************************************************************
json = require ("dkjson")
local result = json.decode(result2)


-- **************************************************************************
-- CHECK SPECIFIED YALE DEVICE FRIENDLY NAME AND STATUS
-- **************************************************************************
luup.log(result.message[YaleDeviceID].name)
if (result.message[YaleDeviceID].status1 == "system.permission_denied") then
luup.log("***** FAULT / ACCESS DENIED *****")
else if (result.message[YaleDeviceID].status1 == "device_status.dc_open") then
luup.log("************** SENSOR IS OPEN **************")
else if (result.message[YaleDeviceID].status1 == "") then
luup.log("** SENSOR IS CLOSED **")
end
end
end



Offline jswim788

  • Sr. Member
  • ****
  • Posts: 378
  • Karma: +14/-2
Re: Yale Smart Alarm Sensor Status - Custom Vera Plugin
« Reply #1 on: April 05, 2017, 12:41:02 pm »
You could look at this post from futzle where she describes a virtual temperature sensor and you could perhaps use the same methodology as it polls the sensor periodically and updates the value: http://forum.micasaverde.com/index.php/topic,7549.msg48644.html#msg48644

Although you might want to start with a different device and add polling to it.  Hopefully others will chime with more information.


Offline OliA

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
Re: Yale Smart Alarm Sensor Status - Custom Vera Plugin
« Reply #2 on: April 08, 2017, 05:19:54 pm »
Thanks for the info. I've pretty much cracked it all except the auto refresh/polling.

I believe I've enabled the polling state variable in the services file, have set the polling interval to 10 seconds and have set the Poll action in the implementation file to call the refresh function. I can manually trigger the refresh by pressing the Poll button in the AltUI action menu but it doesn't happen automatically in the background.

I tried call_timer unsuccessfully to repeat the function (I probably got the code wrong), I did manage to get it working by using the sleep command and recalling the function at the end, however the Vera web interface constantly flags that the device is running startup lua, so I figured that's not the recommended way to achieve this as it's an infinite loop.

What is the recommended process to run a refresh/poll function? Is it call_timer as per futzle's plugin or should the system actually poll at the specified interval and have I not enabled polling properly?

Many thanks

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 378
  • Karma: +14/-2
Re: Yale Smart Alarm Sensor Status - Custom Vera Plugin
« Reply #3 on: April 08, 2017, 08:55:45 pm »
call_timer() should work as it is used in many plugins.  Do you see anything in your logs when you try to use it?

The alternative is to run a scene periodically that gets the data you want and pushes it into the device you've created (luup.variable_set()).  It's less elegant, but it should work fine.

call_sleep() is a bad idea as you discovered for that and other reasons.  Better to use one of the above (or see if anyone else has a better idea).

Offline OliA

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
Re: Yale Smart Alarm Sensor Status - Custom Vera Plugin
« Reply #4 on: April 09, 2017, 05:01:16 pm »
Thanks for the reply. Managed to get the call_timer working, had put it in the L_xxx.lua file but it always returned an error. I then put the call_timer in the implementation file startup function which seems to have done the trick.

So lastly, I was thinking that rather than having 10 devices all making http requests to the Yale hub API (which returns the status of all the Yale devices on each request) that I could set just one device to do the http request every 10 seconds for itself but also store the JSON string somewhere so that the other 9 devices can retrieve their status from there instead.

I figured this would be less demand on the API and probably a bit more efficient on the Vera.

Would you agree with the above and if so how would you recommend this is efficiently implemented - store it in a variable somewhere or perhaps a txt file stored in /tmp/log/cmh?

Many thanks


Offline jswim788

  • Sr. Member
  • ****
  • Posts: 378
  • Karma: +14/-2
Re: Yale Smart Alarm Sensor Status - Custom Vera Plugin
« Reply #5 on: April 09, 2017, 10:26:42 pm »
You might take a look at the "Wunderground Weather Plugin" because I think it might be doing something similar to what you want.  See http://forum.micasaverde.com/index.php?topic=11983.0  The weather plugin calls the weather service periodically, and it distributes the data to child devices (temperature, humidity, forecast high/low, etc.).  Maybe that would be something to use as a starting point if it is close to what you are trying to do?  Seems like it would be better to push the data from the master as you receive it instead of storing it and letting the others retrieve it, but I'm sure the latter could work too.

You are getting beyond my expertise in lua, but I think you could have a global variable defined in the startup lua that could be picked up by various other devices as needed.


Offline davethehedgehog

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
Re: Yale Smart Alarm Sensor Status - Custom Vera Plugin
« Reply #6 on: May 12, 2017, 11:44:30 am »
Hi, do you have any code or info you can share on your plugin? I got my Vera last week, and my Yale alarm just this week. I'd be interested to see what you've got an possibly extend/contribute
« Last Edit: May 12, 2017, 11:48:50 am by davethehedgehog »