Send TCP on ItemStateChangedEvent?

Hello, I have a Wemo smart plug that has an energy (power) monitor, which returns a Number of watts being consumed by the connected load. I would like to forward this value to a remote system via TCP when it changes.

I have an Item set up with a TCP binding and linked to the Thing, but it does not call the TCP binding when value changes. The TCP works when commanded - but that’s not the event that happens when the Thing value is updated.

When the Wemo binding reads the value, the log records an “ItemStateChangedEvent” and the value is displayed on HABmin correctly. But no TCP transmission.

I suspected that the behavior may be different if the Item was Commanded rather than just having a StateChange.

I tested this by using the REST API documentation with a POST (Command) vs. a PUT (StateChange). When the item received a Command, the TCP was called and the value was received by the remote system. When I used the PUT (State Change), the TCP transmission did not occur (but the value still displays properly in HABmin)

Here is my Item:

Number iWasherPower "Washer Power is [%d]" { tcp=">[]" }

and the DEBUG when the service starts:

2016-12-18 22:20:52.829 [DEBUG] [ing.tcp.AbstractSocketChannelBinding] - Setting up the outbound assigned channel C hannel [item=iWasherPower, command=0, direction=OUT, remote=/, buffer=, isBlocking=false, isRecon necting=false, channel=::/, host=, port=7777]
2016-12-18 22:20:52.832 [INFO ] [ing.tcp.AbstractSocketChannelBinding] - The channel for / is now  connected

In my Item I have tried using an asterisk as the first parameter of the TCP argument (prior to the 192, with a colon) as the asterisk is supposed to 'catch; all commands. Even though the DEBUG of the startup changes command=0 to command=*, the results are the same.

Here is the DEBUG when the Item receives a Command (POST) (I sent the number ‘2’)

2016-12-18 22:25:40.717 [DEBUG] [ing.tcp.protocol.internal.TCPBinding] - transformed response is '2'
2016-12-18 22:25:40.855 [DEBUG] [ing.tcp.AbstractSocketChannelBinding] - Picked WriteBufferElement [Channel=Channel  [item=iWasherPower, command=0, direction=OUT, remote=/, buffer=, isBlocking=false, isReconnectin g=false, channel=/, host=, port=7777], buffer=2
, isblocking=false] from the queue
2016-12-18 22:25:40.858 [DEBUG] [ing.tcp.AbstractSocketChannelBinding] - Sending 2
 for the outbound channel />/

But when the number is updated via the Wemo Binding the Event log reads

2016-12-18 22:43:38.710 [ItemStateChangedEvent     ] - iWasherPower changed from 89 to 88

but no TCP transmission and nothing in the DEBUG corresponding to that event.

How can I have the number sent out via TCP when there is a StateChangedEvent?

Thank you

Most 1.x bindings don’t listen for item state updates, even though doing so would make your use case work very cleanly. Maybe a rule would work, where you have an item bound to the TCP binding, let’s call it iTcpCommand.


Number iTcpCommand "Washer Power (outbound)" { tcp=">[]" }


rule SendWatts
  Item iWasherPower changed

Thank you for the quick response. I suspected a Rule may be in order, but I’m brand new to OH and haven’t dug into them quite yet. Is the .sendCommand method (it’s called a method, right? sorry i’m new to Java) ‘built-in’ or do I have to define it and include it from somewhere else?

It is called a method :grinning: and it is built into the item object! My example above is all you would need in a .rules file.