We have moved at community.getvera.com

Author Topic: Twine Interface Plugin  (Read 7869 times)

Offline SOlivas

  • Sr. Member
  • ****
  • Posts: 282
  • Karma: +1/-1
Twine Interface Plugin
« on: July 12, 2013, 12:57:55 am »
18 July - Updated to version 1.1 - adds Temperature device w/ Celsius option

(Now available as a plugin in the MiOS Apps Store)

After my last plugin, I decided that I wanted to try my hand at something a bit more complex.  This both helps me learn Lua and create something that I will use with my Vera 3.

This plugin will poll a single Twine device, storing the values returned for the specified device in variables in the plugin.  It can also optionally return a single value when the poll function completes.

For those who don't know, the Twine is a device from http://www.supermechanical.com/ and uses 2 AA batteries or a power adapter, connects to your wifi network and reports all sorts of things like device orientation, vibration, temperature, etc.

I've been using my Twine for a while to report the temperature of the room that it is in, using a shell script and sending the results to a channel on Thingspeak.com.  In turn, Thingspeak has been setup to send notifications of how warm the room is to my mobile devices via airgramapp.com.  More information about this is available here:  http://community.supermechanical.com/index.php?p=/discussion/189/idea-for-temperature-monitoring-app

To use this plugin you will:
1.  Need to have a Twine device
2.  Have an account on https://twine.cc
3.  Know the ID of the Twine device you want to use the plugin with.

Note:  This uses a hidden URL to read the Twine's device information and save it.  It may be broken if Supermechanical.com changes their interface.

- To find the ID of your Twine, login to Twine.cc.  Select the Twine you want to use from the "Twines" menu.  Your browser's URL will be updated to show something like:

twine.cc/000ffffffffffff/ruleset

Your Twine's ID is the part between the /'s after the FQDN and before /ruleset

- Put your user account and password into the User Account and User Password variables of the plugin
- Put the Twine ID that you wish to use in the Twine ID variable of the plugin

The plugin will display an error message if it is missing these items when it is initialized.

To use the plugin:

The plugin is called using the GetTwineData function with an optional Item argument passed.  The Item argument tells the function if you wish to have any information returned when the function exits.

For example.  If our plugin had a device id of 69, and we wished to know what the last temperature was that Twine reported, we would use:

Code: [Select]
local var = luup.call_action("urn:twineinterface-org:serviceId:TwineInterface1", "GetTwineData", { Item=2 },69)
If successful, var will contain a 4 digit value that represents the temperature.  The first two digits are only relevant.

The GetTwineData function will login to your twine.cc account, and using the session cookie, submit a query for the JSON data.  Here is what the data returned looks like:

{"gs_version": "Oct 31 2012-08:21:10", "age": 0.0002491474151611328,
"ts": 1369854336.0, "values": [["ZZZ00", "1.3.1"],
["ZZZ01", 7700], ["ZZZ03", "top"],
["ZZZ05", 55104], ["ZZZ06", 1],
["ZZZ07", 1], ["ZZZ04", 0],
["ZZZ02", 0]], "rssi": 196, "ruleset_id": "32537",
"last_poll": 1369854336.0}

* Where ZZZ is your Twine ID.

Here is a list of what each value is and what it does:

TField00 - firmware version
TField01 - temperature (in hundredths of F)
TField02 - vibration (in milli-Gs)
TField03 - orientation ("top", "bottom", "left", "right", "back", "front")
TField04 - vibration event (0 = not triggered, 1 = triggered)
TField05 - battery voltage (in microvolts - if it's below 1300000?V you're almost certainly on external power - the voltage reading is tuned for batteries)
TField06 - not used
TField07 - update mode (0 = normal, 1 = fast)
gs_version - radio version
age - how many seconds ago did the Twine update this data
ts - ? possibly a timestamp
rssi - wifi signal strength
ruleset_id - twine.cc generated number corresponding to the rulseset id
last_poll - ?

(A good explanation on all this, as well as using external sensors is found here:  http://community.supermechanical.com/index.php?p=/discussion/453#Item_1.  As of this version, external senors are currently not supported.  The information on the fields above and the JSON data example was taken from the information found at this link.)

Here is a list of Item codes and what the GetTwineData function will return with each:

 1  - TField00
 2  - TField01
 3  - TField02
 4  - TField03
 5  - TField04
 6  - TField05
 7  - TField06
 8  - TField07
 9  - gs_version
10 - age
11 - ts
12 - rssi
13 - ruleset_id
14 - last_poll


This is version 1.0. 

Please let me know what you think.

Enjoy!

The picture was added just to show how I use Twine with Thingspeak.  It really doesn't have anything to do with this plugin, except to illustrate what you can do with a Twine.

Once I do a few more things, I will submit this to the app store for approval.

Updates in version 1.1:

This version adds a child device using the Temperature service to your Vera called "Twine Temperature", which holds the current value of TField01 after a successful polling of the twine has been completed.

Once this plugin has been setup, by default, it will poll your twine every 5 minutes (300 seconds), store the data in the variables above and update the temperature child device (if the temperature has changed).

You can modify this behavior by changing the Refresh variable to a value between 60 and 3600.  If you set it to 0, it will not auto update after the initial poll that is done 30 seconds after the plugin is started.


Option to use Celsius temperature:

Set the following variable in the Twine Interface device:
Metric = 0 - Fahrenheit
              1 - Celsius

The temperature child device will display the temperature in Celsius, and you can then fetch the device's CurrentTemperature variable to retrieve the converted value.  (The value stored in TField01 will remain unchanged).


Update:  For a better understanding on how to use these variables, you can look at the code here:

https://twine.cc/static/js/controllers/rt2.js

« Last Edit: August 05, 2013, 10:21:05 am by SOlivas »
Vera 3 (1.5.622) / 9x GE/Jasco 45609 / 2x GE/Jasco 45612 / 2x GE/Jasco 45614 / 1x MIMO Lite
1x Twine (http://forum.micasaverde.com/index.php/topic,15617.0.html), DSC Security System, Honeywell  YTH8320ZW1007 Thermostat, 1x Fortrezz WWA-01, 1x CA9000 Wireless PIR Sensor

guest20975

  • Guest
Re: Twine Interface Plugin
« Reply #1 on: July 15, 2013, 04:50:23 am »
Help needed by noob.Downloaded files to Luup files created device using "urn:schemas-twineinterface-org:device:TwineInterface:1" and "D_TwineInterface1.xml" and "I_TwineInterface1.xml".
Added my Twine reference and login details in the Advanced tab. Rebooted and reloaded the browser window. No joy nothing seen in the fields. Am I doing something wrong?

Offline SOlivas

  • Sr. Member
  • ****
  • Posts: 282
  • Karma: +1/-1
Re: Twine Interface Plugin
« Reply #2 on: July 18, 2013, 02:24:20 am »
Help needed by noob.Downloaded files to Luup files created device using "urn:schemas-twineinterface-org:device:TwineInterface:1" and "D_TwineInterface1.xml" and "I_TwineInterface1.xml".
Added my Twine reference and login details in the Advanced tab. Rebooted and reloaded the browser window. No joy nothing seen in the fields. Am I doing something wrong?

I would kill any devices you have, then re-create.   Try to reload the Vera at least one before you enter in your data.

I should note that this was made on a Vera 3, so if you are using a previous version, I am not sure how it will work.

Also, you only need to specify the D_TwineInterface1.xml file when creating the device.  It will take care of setting up what is needed for use.  I've never tried specifying the schema or implementation file like you have -- that might be what is causing you problems.
Vera 3 (1.5.622) / 9x GE/Jasco 45609 / 2x GE/Jasco 45612 / 2x GE/Jasco 45614 / 1x MIMO Lite
1x Twine (http://forum.micasaverde.com/index.php/topic,15617.0.html), DSC Security System, Honeywell  YTH8320ZW1007 Thermostat, 1x Fortrezz WWA-01, 1x CA9000 Wireless PIR Sensor

guest20975

  • Guest
Re: Twine Interface Plugin
« Reply #3 on: July 18, 2013, 07:48:46 am »
Thanks for your reply. Have downloaded v1.1 and everything seems to be working now. Just need Twine to enable celsius by default in a forthcoming release.

Offline SOlivas

  • Sr. Member
  • ****
  • Posts: 282
  • Karma: +1/-1
Re: Twine Interface Plugin
« Reply #4 on: July 18, 2013, 10:13:47 am »
Glad to hear.

Yeah, I know that the Celsius temperature has been a request for a while now.   When I get a chance, I will add something that can be set via a variable to show the temperature in Celsius vice Fahrenheit, since it is a simple formula to convert:


C = (F - 32) * 5/9

If you need the temperature in that format, just get the value in TField01, strip off the first two chars using a string.sub, then do the conversion:



Code: [Select]
local F = luup.call_action("urn:twineinterface-org:serviceId:TwineInterface1", "GetTwineData", { Item=2 },69)

F = string.sub(F,1,2)

C = (F - 32) * 5/9

Note: 69 = whatever your device ID is.


Ok, I added a feature that will let you convert the temperature to Celsius before it is stored on the child Temperature device.  This will display the temperature in Celsius and allow you to fetch the value from the child device to use.
« Last Edit: July 19, 2013, 02:33:58 am by SOlivas »
Vera 3 (1.5.622) / 9x GE/Jasco 45609 / 2x GE/Jasco 45612 / 2x GE/Jasco 45614 / 1x MIMO Lite
1x Twine (http://forum.micasaverde.com/index.php/topic,15617.0.html), DSC Security System, Honeywell  YTH8320ZW1007 Thermostat, 1x Fortrezz WWA-01, 1x CA9000 Wireless PIR Sensor

Offline Aaron

  • Hero Member
  • *****
  • Posts: 1985
  • Karma: +61/-193
Re: Twine Interface Plugin
« Reply #5 on: July 20, 2013, 11:33:17 am »
Just looked at the Supermechanical web site... What am I missing?

It seems like in order to use any single/separate twine device you need 2 things - 1) twine box  2) module w/ cable connection to twice

If I'm reading this correctly then, when compared to Insteon, Zigbee, or Zwave devices...
 a) Twine devices are huge & ugly to have all around the house
 b) Twine devices they are expensive (you need a $100 controller for each one?)
 c) battery life is 2 months... REALLY poor!

I'm not trying to knock anything I have not used but I don't see any reason to use these if you have another control protocol?

Offline SOlivas

  • Sr. Member
  • ****
  • Posts: 282
  • Karma: +1/-1
Re: Twine Interface Plugin
« Reply #6 on: July 20, 2013, 12:34:09 pm »
Just looked at the Supermechanical web site... What am I missing?

It seems like in order to use any single/separate twine device you need 2 things - 1) twine box  2) module w/ cable connection to twice

If I'm reading this correctly then, when compared to Insteon, Zigbee, or Zwave devices...
 a) Twine devices are huge & ugly to have all around the house
 b) Twine devices they are expensive (you need a $100 controller for each one?)
 c) battery life is 2 months... REALLY poor!

I'm not trying to knock anything I have not used but I don't see any reason to use these if you have another control protocol?

I got one since I was a supporter of their Kickstarter project.

For a Twine, all you need is a Wifi connection.  It is a small little box that runs off 2 AA batteries.  the device by itself has the ability to report temperature, vibration and orientation of the device (up, down, top, bottom, etc.)  With the optional senors, you can detect water, opening/closing of things (magnetic sensor), a breakout board that you can make to interface your simple projects to, an Arduino Cloud Shield for interfacing to your Arduino, and an external temperature sensor.

The device itself can be programmed via their web interface to add rules for it to do certain things.

Triggers can be anything that it detects, either by the base device itself, or via the sensors.  The actions can be:  HTTP request, flash the LED, send an e-mail, send a message on Twitter, send an SMS message, or make a phone call.

If you don't have an HA controller, and just want to have something that is somewhat weather proof to monitor things, you can use this.  You can keep the batteries in it and use a power supply as well, so that if the power goes out, you still have the device on-line.  The current model doesn't charge the batteries.

As a supporter of the Kickstarter project, when I received mine, I also got all the three initial sensors (water, magnetic, and breakout). 

They aren't that bad, a small box about half the size of an IPhone. 

I use it mainly as a temperature sensor, and that was the motivation behind making this plugin, so that I can use that temperature information with my Vera.


I should also note that I had this device well before I even moved into my current residence, since at the last place I lived, the landlord wouldn't have appreciated me converting the lights and other fixtures over to Z-Wave (I was in Singapore, so it would have also been a different voltage and frequency as well).

The Twine, like many other products like this, are essentially Geek toys.  The same could be said about the Vera and its Z-Wave network.
Vera 3 (1.5.622) / 9x GE/Jasco 45609 / 2x GE/Jasco 45612 / 2x GE/Jasco 45614 / 1x MIMO Lite
1x Twine (http://forum.micasaverde.com/index.php/topic,15617.0.html), DSC Security System, Honeywell  YTH8320ZW1007 Thermostat, 1x Fortrezz WWA-01, 1x CA9000 Wireless PIR Sensor

Offline SOlivas

  • Sr. Member
  • ****
  • Posts: 282
  • Karma: +1/-1
Re: Twine Interface Plugin
« Reply #7 on: July 26, 2013, 02:53:26 am »
Here are some examples of device usage -- you will have to scroll down since it is the development company's blog:

http://supermechanical.tumblr.com/
Vera 3 (1.5.622) / 9x GE/Jasco 45609 / 2x GE/Jasco 45612 / 2x GE/Jasco 45614 / 1x MIMO Lite
1x Twine (http://forum.micasaverde.com/index.php/topic,15617.0.html), DSC Security System, Honeywell  YTH8320ZW1007 Thermostat, 1x Fortrezz WWA-01, 1x CA9000 Wireless PIR Sensor

Offline itchyballs

  • Sr. Newbie
  • *
  • Posts: 40
  • Karma: +0/-0
Re: Twine Interface Plugin
« Reply #8 on: March 02, 2014, 11:26:18 pm »
Thanks for this plugin it works for me!

I use Twine to detect when my laundry dryer cycle is done using vibration rules and an action to send me Prowl/Pushover/e-mail notification. I also set up an IFTTT recipe to to scan my gmail account for the notification which then flashes Philips Hue lights to red. But IFTTT is awfully slow. Can I use your plugin to possibly run a Vera scene to flash my Philips Hue and play audible alert on my Sonos? Of course my Vera is already integrated with my Hue controllers and Sonos speakers.