Use URL to manipulate "contacts"

141 views
Skip to first unread message

Tommy Sharp

unread,
Feb 28, 2015, 3:48:13 PM2/28/15
to
I've had a read of the wiki and can manipulate switches with the method below...

But I can't seem to be able to change a "contact" to either CLOSED or OPEN. I keep seeing in the log that it's an invalid command....

http://localhost:8080/CMD?Contact_B_GarageDoor1=OPEN

Received unknown command 'OPEN' for item 'Contact_B_GarageDoor1'

Seems to work fine for lights and switches, but not contacts...

I tested further by creating a switch and then a rule that uses that switch to manipulate my contact. In the rule, this command works perfectly and updates the contact state.

sendCommand(Contact_B_GarageDoor1, OPEN)

Tommy Sharp

unread,
Mar 16, 2015, 10:07:36 PM3/16/15
to ope...@googlegroups.com
Is anyone able to confirm if a contact should in fact be able to be updated through HTML or is it not possible?

Jason Brunk

unread,
Mar 17, 2015, 3:25:54 PM3/17/15
to ope...@googlegroups.com
Have you you tried using ON and OFF  instead just to test?  

Jason

Tommy Sharp

unread,
Mar 17, 2015, 6:34:44 PM3/17/15
to ope...@googlegroups.com
Hi Jason, ON and OFF doesn't work either..... Just says Unrecognized Command in the logs...

Mark

unread,
Mar 17, 2015, 6:51:38 PM3/17/15
to ope...@googlegroups.com
If I'm reading the code correctly, I'd say you cant send a ContactItem any type of Command, at least when they're validated by the calling entity.  If Thomas or Kai indicate this is ok to tweak/add, then I'll submit a PR for it...  

I'm not 100% familiar with this part of the code (yet), so I could be way off base.


Relevant files:

The CmdServlet, where it looks up the acceptedCommandType list of the Item, and gives a warning ("Received unknown command '{}' for item '{}'") if it cannot find/parse it...

The SwitchItem, where it pre-populates the OnOffType as being acceptable command type:

The ContactItem, where it doesn't add anything to the list of acceptable command types (I expect it just needs "OpenClosedType to be pre-populated here):

The OpenClosed type, more for reference:

Ben Jones

unread,
Mar 17, 2015, 6:58:20 PM3/17/15
to ope...@googlegroups.com
Yes - Mark could well be right here - have you tried sending an UPDATE with OPEN/CLOSED? 

From the openHAB REST API WIKI page...

Likewise, you can send a status update using the HTTP verb PUT to the same uri, passing the new state as a plain string argument in the body (encoding text/plain). 
In order to send a command to an item, you would use the item uri (http://localhost:8080/rest/items/Temperature_FF_Office) and send an HTTP POST with the according command in the request body as text/plain.

Josenivaldo Benito Junior

unread,
Mar 18, 2015, 10:27:07 AM3/18/15
to ope...@googlegroups.com
Guys,

It does not make sense to send a command to a contact. A contact is a passive two state kind of device, it is in the state Open or Close according to its physical state. Think about a magnet sensor at your garage door. You cannot command the sensor to open or close, it is open or close depending on the door position (since door carriers the magnet).

If you need to command something it is a Switch. A switch has two states like contacts but it receives commands to change its actual state. Think a light switch, you can poll it to know if light is on or off and you can command it to be in a specific state. Like: turn on  (and light will turn on if it was off or stay on if it is already on). A garage opener actuator is also a switch (not a contact). It is either ON of OFF state (and you can map to OPEN or CLOSE to have a better mean)  and you can change its state. However a garage door sensor (contact) can only know the actual state but not actuate over it.

So, if you are trying to command something it is a switch, if you only read the actual state it is contact. Again, at least looking to concept of a contact, there is no sense in it receive a command, this would turn it in a switch.

Regards,
Benito

Mark

unread,
Mar 18, 2015, 11:30:41 AM3/18/15
to ope...@googlegroups.com
Maybe, but if I have one or more Item definitions that aren't bound to a data-source, like this:

Contact  FamilyMotionZoneTripped "Family Motion (Zone 28) [MAP(en.map):%s]" (GContact,GMotion,GPersist)


and I want to use an external entity as the source of truth for the Contact Item's value, then what is the best way to have that entity [remotely] push that value in?

For example, I have a Linux Shell script, or a Spark Core, and it can make the necessary lightweight wget/curl and/or TCPClient call into openHAB to push the value change.


I understand that it doesn't make sense for a UI User to access this, but some mechanism is needed for [external] entities to push changes into the Item... otherwise it never gets a value (except by Rule).  

So perhaps the question morphs into what that mechanism is, if not the CmdServlet?

Mike Child

unread,
Mar 18, 2015, 12:13:16 PM3/18/15
to ope...@googlegroups.com
If you had an item definition like that then you can't send a command to the device as there is no device bound to the item.  Also Contacts can't receive commands.  What you want to do it send a status update as previous mentioned in this thread.  So in the wiki it says  that to do that you would ...  "Likewise, you can send a status update using the HTTP verb PUT to the uri, passing the new state as a plain string argument in the body (encoding text/plain)."  The uri for you example would be "http://localhost:8080/rest/items/FamilyMotionZoneTripped/state"

Mark

unread,
Mar 18, 2015, 12:20:03 PM3/18/15
to ope...@googlegroups.com
Sure, that works.

I think that's all the OP wanted, was the blessed manner to make a state change from an external entity.

Tommy Sharp

unread,
Mar 18, 2015, 2:33:00 PM3/18/15
to ope...@googlegroups.com
Hi Mark, yes my original problem is that I have all my reed switch garage door contacts connected to a Raspberry Pi running Domoticz. This is my old automation system..... I want Domoticz to update OpenHAB with the status of the contacts every time they change.

I cannot see a way in Domoticz to send an HTTP request with the verb "put", it can just access a URL directly.

For now my solution is a switch in openHAB which is updated via http from Domoticz. And then I have a rule in openHAB which monitors changes to this switch and then updates the contact.

I was just hoping there was a more simple way :-)

Thanks for all your input.

Mark

unread,
Mar 18, 2015, 2:38:13 PM3/18/15
to ope...@googlegroups.com
Hey Tommy,
If your old system can launch OS processes, and you're not doing it too frequently, you may also be able to hobble-by using something akin to:

wget --method=PUT \
     
--verbose \
     
--body-data="OPEN"  \
     
--header="Content-Type: text/plain" \
     http
://localhost:8080/rest/items/FamilyMotionZoneTripped/state


That's the CLI representation of what Mike was referring to above, and will give you access to HTTP's PUT Method.


eric

unread,
Mar 18, 2015, 8:27:27 PM3/18/15
to ope...@googlegroups.com
Mark, 
This is exactly my problem as well.  However, the mechanism I'm trying to use to update a contact value is coming from a PHP script.  I'm pretty new to PHP, so I haven't figured out how to modify the existing HTTP POST method in the PHP REST example to update a contact value.

If anyone here knows how to do this, perhaps by just writing a new short PHP function for "doPutRequest" instead of "doPostRequest", I'd appreciate the assistance.
Reply all
Reply to author
Forward
0 new messages