Help me write a plugin for a 3rd party web controlled relay

Posted on
Sat Jul 16, 2016 4:24 am
ravedog offline
Posts: 14
Joined: Jun 27, 2016
Location: lasVegas.nv.us.na.ea.ss.mw

Help me write a plugin for a 3rd party web controlled relay

Hello everyone,

New to Indigo (moving from an ISY).

I'm a bit overwhelmed. I wish there were documentation for walking thru creating a plugin for a sample device. I have no experience with Python, however, if I could get some of you to walk me thru the process from end to end - then I can apply that to other things I'd like to do. I'm hoping that asking for help in thsi way its not too offputting - it's how I learn.

I have this device (several), Its a ControlByWeb Relay X-310 (http://www.controlbyweb.com/x310/). It's a 4 in 4 out relay that can be accessed thru a web interface. It also can be controlled by issuing HTTP GET requests for control or query status.

I want to be able to turn each output relay (1-4) into virtual devices so they can be mapped to a KPL or just accessed thru the WEB INTERFACE. So I need to be able to issue commands to change states (on or off) - and also be able to query the device's state.

Inputs should also be able to be queried as well (in my case, some will be temperature (1-wire) and some will be digital input).

I can successfully issue commands using cURL in the terminal and see the response. But that is as far as I can go.

I'm not sure if this should be a plugin per se or just a series of scripts. As I said, I'm totally lost as to how to do this successfully in Indigo. (I did have this working in the ISY but in an extremely limited fashion: I can issue a HTTP Get request to change a relays state but cannot query the device to parse the value back in the ISY.... it's sort of a one-way interface... command goes out and thats it).

Also, there is a way to configure the device to push out changes to an external server submitting an XML file based on state change.

Thank you ahead of time for your consideration. If someone wants to teach me the process, it would be a big help in my wrapping my head around this.

Posted on
Sat Jul 16, 2016 4:56 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help me write a plugin for a 3rd party web controlled re

I would strongly suggest that you NOT write a plugin for this. Yet.

It's kind of like learning to drive with a Ferrari. You could do it, but there's a very good reason why most people don't. I would suggest starting off with Python scripts. Depending on your level of familiarity with programming, you might want to start off with something simple like:
Code: Select all
x = "Hello world."
indigo.server.log(x)

Look for the Indigo Software Development Kit. It contains examples of simple plugins that you could start with and modify.
Look for the Indigo Plugin Developer Documenter plugin. It shows the program flow.
Read the docs here: http://wiki.indigodomo.com/doku.php?id=indigo_6_documentation:plugin_guide and here: http://wiki.indigodomo.com/doku.php?id=indigo_6_documentation:plugin_extending_tutorial

I understand learning through trial and error; it's how I learn too. Speaking from experience, I think you should start off with a less ambitious plan.

Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sat Jul 16, 2016 7:21 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Help me write a plugin for a 3rd party web controlled re

I can't remember the model but I've got a ControlByWeb sat here unused at the moment, so I might be able to play with it with/for you.

But I agree, take the time to learn Indigo first before jumping in to plugins head first.


Sent from my iPhone using Tapatalk

Posted on
Sat Jul 16, 2016 8:53 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Help me write a plugin for a 3rd party web controlled re

ravedog wrote:
I wish there were documentation for walking thru creating a plugin for a sample device

There are SO many of those, honestly. First off, there is an entire Wiki devoted to just that topic and then, as mentioned earlier, an API with really easy to follow and well commented code to give you that step-by-step you are talking about.

ravedog wrote:
I'm hoping that asking for help in thsi way its not too offputting

We are here to help but it's hard to teach someone Python and plugin coding purely via a forum. Get your hands dirty in the samples and Wiki and when you hit a wall then ask on the forum and get the answers. That's how many of us learned how to do it.

ravedog wrote:
It's a 4 in 4 out relay that can be accessed thru a web interface.

I don't know if this will work for you but I wrote a plugin called Device Extensions that includes a URL device so you can easily send commands to a web-enabled device. It's purpose is to turn on/off/toggle that device but a URL is a URL so you could maybe use one or more of those URL devices to accomplish what you want.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Sat Jul 16, 2016 10:48 pm
ravedog offline
Posts: 14
Joined: Jun 27, 2016
Location: lasVegas.nv.us.na.ea.ss.mw

Re: Help me write a plugin for a 3rd party web controlled re

Hey everyone,

Awesome suggestions. I kind of realized after I posted this that it sounded a bit like "do it for me..." which it kind of was. I guess I was looking for how the concept would work end to end. Yes, there are a lot of examples everywhere. But thats just it - its overwhelming. Just telling me that I should so it with python scripts instead of the plugin is a huge help. (For the record I learned how to drive on a Ferarri... just kidding). I understand that this may seem ambitious, but the problem is I'm trying to move from an ISY where I have this working (although its one way) and I want to move to Indigo.

THanks for the links. I am having problems with the documentation and some what seem like incomplete links:

http://wiki.indigodomo.com/doku.php?id=indigo_6_documentation:plugin_extending_tutorial#monitoring_device_state_changes_from_a_plugin (That one just seems to have a lot of placeholders for future documentation.

I did find the plugins page and some links to sample plugins but again they take me back to a sub-forum where people are discussing the various plugins but not how they were made. I was hoping for a lynda-type how-to that walked thru the process of a sample device from beginning to end and discussed all the concepts and whys and wherefores. A lot of whats up here is sort of mid to high level stuff. (it's like the difference between a owners manual that walks you thru every menu and button and dryly describes them vs. heres a great way to do things using these tools...)

When I wrote the OP, it was out of desperation and confusion. As it is I have to buy a Mac Mini (it has to be located near the panel where I have solid communications), a new PLM and another 200 for the software.About 800 investment to move AWAY from the ISY and I am just having a freak out. So while this may seem a bit "deep end of the pool" for me (and it is)... I have to go there to move my system over. I So let me rephrase it a bit to hopefully grok a good workflow/mindset.

Conceptually speaking, If you had this "NETRELAYDEVICE" on a network, that has multiple relays or switches on it. How would you make that a virtual device within Indigo? If it's not a plugin, do you have a script do the backend communication that pushes the states of those relays into variables that represent a relays state? Then use those variables as devices? Pretend I don't know anything. (I used the ControlbyWeb relay as an example because I also have Digital Loggers Web Power switches, arduinos that are sensors, but all are networked and all basically work in the same way). I did find someone who said that they too had a Digital Loggers switch and then said they figured it out but posted no results. Looking at all the plugins, it's hard to find one that may be similar that I can look at and perhaps modify.

I have no problem with the insteon stuff. That stuff I can migrate over easily. Programs for those, a little more effort. It's the extensibility that I need a push on - the concepts. Thanks for that link for Indigo Plugin Developer's Guide v1.4... I don't know how I missed it....

I looked at your DeviceExtensions plugin and well - I definitely need to re-read all of Indigo's documentation as I'm still lost. I must have missed or glossed over a whole section on extensibility.

Thanks again.

G

PS - One last question.... have any of you or anyone on the forums shared their home databases as examples of advanced systems. The one that comes built-in to Indigo demo is good but is basic.

Posted on
Sun Jul 17, 2016 8:43 am
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Help me write a plugin for a 3rd party web controlled re

try this:
create a http statement that does what you like
then that try it with
curl http ...
in a terminal window
then use that curl statement in an action group / server action/execute script with python
Code: Select all
import subprocess
subprocess.Popen("/usr/bin/curl your curl statement here", shell=true)

then you have your first python script that opens/ closes your remote rely.


Karl
a simple curl is eg
curl http://www.cnn.com
you will get a lot of stuff back but it shows how it works

Posted on
Sun Jul 17, 2016 9:30 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Help me write a plugin for a 3rd party web controlled re

Or, use Python's urlopen...

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Jul 17, 2016 9:54 am
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Help me write a plugin for a 3rd party web controlled re

Yes that would be the next step. And then add some ifs and actions.


Sent from my iPhone using Tapatalk

Posted on
Sun Jul 17, 2016 10:10 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help me write a plugin for a 3rd party web controlled re

ravedog wrote:
I understand that this may seem ambitious, but the problem is I'm trying to move from an ISY where I have this working (although its one way) and I want to move to Indigo.

Yeah, what you're suggesting is a pretty heavy lift -- especially if you don't know Python. When I came over to Indigo from Vera, I didn't know Python existed and hadn't done any programming since school (a LONG time ago.) I started with basic stuff to learn my way around the language--parsing values from JSON and XML files into variables, reformatting strings to make them pretty, and other stuff like that. Then I got cocky and built my first plugin. :D

But more on point, what's your familiarity with Python? Once you start to become comfortable with the language, it becomes much easier to work with from the standpoint of knowing how the various pieces work together. I know what you mean about the documentation being overwhelming if you don't understand the way that Python works. Like this:

Objects of the date type are always naive.

An object of type time or datetime may be naive or aware. A datetime object d is aware if d.tzinfo is not None and d.tzinfo.utcoffset(d) does not return None. If d.tzinfo is None, or if d.tzinfo is not None but d.tzinfo.utcoffset(d) returns None, d is naive. A time object t is aware if t.tzinfo is not None and t.tzinfo.utcoffset(None) does not return None. Otherwise, t is naive.

The distinction between naive and aware doesn’t apply to timedelta objects.

What does that mean? The documentation assumes you understand the stuff before you read it. How helpful is that? Well, once you start to get the hang of things, the language above makes perfect sense. There is a lot of advice online that basically says, "Read the documentation." Yeah? Thanks. It might as well be written in Klingon. But once you start to understand Klingon, the documentation is actually tremendously helpful. There are other places to learn like online tutorials or Stack Overflow.

As for your NETRELAYDEVICE, how does it communicate (get calls, RESTful API, telnet...)? How often to do need to check its state? Like has been suggested, you could write a Python script that Indigo will execute every (seconds, minutes, hours, days) which queries the device and puts the result into Indigo variables. Get it where you like it, and then consider building a plugin around that. To answer your question about having the relays display in Indigo like other devices, you will definitely need a plugin for that. Plugins are a Python wrapper around Python programs that take advantage of hooks into the Indigo system.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Jul 18, 2016 2:59 am
ravedog offline
Posts: 14
Joined: Jun 27, 2016
Location: lasVegas.nv.us.na.ea.ss.mw

Re: Help me write a plugin for a 3rd party web controlled re

These are all amazing replies. I have now gone out and bought a Python book and I'm working my way thru it. I love how mature it is and the infinite resources available. I'm loving the challenge ahead already. I am fairly adept at AppleScript and I have written some bash scripts that act as CGI's on my raspberry pi server that call out other bash routines and had decent luck. I have known about Python but have never touched it.... until now...

As a matter of fact, I wrote some "gateway" bash scripts that allowed me to act as an intermediary between the ISY and the WEB RELAY but never put them into service because I really did not want to pursue the ISY any further. They allowed me to control the relay but then also query it and shove the relays state back into the ISY as variables using the REST interface. (Background... I got the web relays a while back to replace the EZIO8 which is a horrible product (inputs are never pushed back to the ISY and a lot of times commands would be sent out and acknowledged but sometimes relays not switched.) I needed something a little more robust).

I want to tell you thanks for really being nice and helpful and encouraging and patient with your responses. I realize my questions were "beggy" and vague, but you still managed to make me feel at home without a hint of condescension which is too prevalent on a lot of forums these days. I think I'm in the right place.

Quick question: Using El Capitan which uses Python 2.7.x as a default. Will this be OK for Indigo? Also using BBEdit to write code and test it. Good enough?

Posted on
Mon Jul 18, 2016 4:16 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help me write a plugin for a 3rd party web controlled re

ravedog wrote:
Quick question: Using El Capitan which uses Python 2.7.x as a default. Will this be OK for Indigo? Also using BBEdit to write code and test it. Good enough?

El Cap will work for both an Indigo server and client, but there are some nuances. This bears a bit of explaining. Depending on how you arrive at El Capitan on the machine makes a difference. If you were to buy one brand new, be happy and move on your way. However, if you have upgraded to El Capitan from an earlier version, you may have some small niggles with the way that Apple requires applications to be signed. For my server which was originally Leopard (I believe) i have upgraded to Mavericks and when I reboot the server, I'm required to acknowledge that I want to allow network connections for Python and PostgreSQL. There are workarounds to sign these applications and get rid of the messages. It's important to point out that this is an issue with OS X and not Indigo's fault (and there's nothing the authors can do about this.) IMO, the single best workaround beyond signing the apps is to make sure that your Indigo server is on a UPS.

Right now, Indigo requires Python 2.6 so any scripts that you write need to be Python 2.6 compatible or you need to write an external call to Python 2.7 and pipe the result back. There are some folks on the boards that are using Python 2.7 for select applications. I tend to stick to 2.6 if possible.

BBEdit is perfect for editing your scripts. Many folks here use BBEdit (including the Indigo authors) and TextWrangler seems to be the most popular tool. I have started using PyCharm IDE for my main coding but still use TextWrangler to bang out simple scripts and as a scratchpad.

Cheers,
Dave

p.s. One comment above bears a note that often trips up new Indigo users. Indigo has two components--a server and a client. The server installs as a daemon and doesn't need special settings to run -- don't add it to your startup items in OS X. The client application is what's launched when you open the Indigo app. The client and server don't need to be on the same machine and can be on different versions of OS X (but the server version and client versions must always be the same to work properly.)

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests