TCP Binding - REGEX help

Hi gents,

I have setup a tcp server using the tcp binding that retrieves status input from my roomba vacuum cleaner every minute.

The string looks like this:

{ “ROOMBA”: { “status”: “3”, “cleaning”: “0”, “battery”: “100”, “temp”: “37”, “error”: “0”, “button”: “0” } }

Can somebody please explain to me how I can use the REGEX feature in an item to extract each element so I can have items like “Roomba Battery: 100%”, “Roomba temperature: 37C” etc?

Maybe I need to explain myself a little better.

My item look like this:

Number	Roomba_Kitchen_Battery	"Battery [%s %%]"   <flatBattery>   (gRoomba)	{ tcp="<[192.168.11.11:*:'REGEX((.*))']" }

The TCP string I’m trying to process looks like this:

{ "ROOMBA": { "status": "3", "cleaning": "0", "battery": "100", "temp": "37", "error": "0", "button": "0" } }

According to regex101.com I can extract the battery value from the string by using this expression:

.*?battery.:..(\d{1,3}).,

I’ve tried using that expression in the TCP binding, but it’s not allowed and throws me an error when executed.

So my question is; what is the correct use of REGEX in the TCP binding to extract only the battery value from that string?

Anyone?

Will you include the error you are seeing and actual binding with the full regex?

Thanks for your response steve1. :slight_smile:

Item binding:

Number	Roomba_Kitchen_Battery	"Battery [%s %%]"  <flatBattery>   (gRoomba)	{ tcp="<[192.168.11.11:*:'REGEX(.*?battery.:..(.{1,3}).,)']" }

Error in openhab.log:

2015-10-25 01:40:56.966 [ERROR] [i.internal.GenericItemProvider] - Binding configuration of type 'tcp' of item ‘Roomba_Kitchen_Battery‘ could not be parsed correctly.
org.openhab.model.item.binding.BindingConfigParseException: couldn't create Command from '192.168.11.11' 
	at org.openhab.binding.tcp.protocol.internal.ProtocolGenericBindingProvider.createCommandFromString(ProtocolGenericBindingProvider.java:198) ~[na:na]
	at org.openhab.binding.tcp.protocol.internal.ProtocolGenericBindingProvider.parseBindingConfig(ProtocolGenericBindingProvider.java:164) ~[na:na]
	at org.openhab.binding.tcp.protocol.internal.ProtocolGenericBindingProvider.parseAndAddBindingConfig(ProtocolGenericBindingProvider.java:98) ~[na:na]
	at org.openhab.binding.tcp.protocol.internal.ProtocolGenericBindingProvider.processBindingConfiguration(ProtocolGenericBindingProvider.java:79) ~[na:na]
	at org.openhab.binding.tcp.protocol.internal.TCPGenericBindingProvider.processBindingConfiguration(TCPGenericBindingProvider.java:1) ~[na:na]
	at org.openhab.model.item.internal.GenericItemProvider.internalDispatchBindings(GenericItemProvider.java:348) [org.openhab.model.item_1.7.1.jar:na]
	at org.openhab.model.item.internal.GenericItemProvider.internalDispatchBindings(GenericItemProvider.java:324) [org.openhab.model.item_1.7.1.jar:na]
	at org.openhab.model.item.internal.GenericItemProvider.processBindingConfigsFromModel(GenericItemProvider.java:171) [org.openhab.model.item_1.7.1.jar:na]
	at org.openhab.model.item.internal.GenericItemProvider.modelChanged(GenericItemProvider.java:390) [org.openhab.model.item_1.7.1.jar:na]
	at org.openhab.model.core.internal.ModelRepositoryImpl.notifyListeners(ModelRepositoryImpl.java:159) [org.openhab.model.core_1.7.1.jar:na]
	at org.openhab.model.core.internal.ModelRepositoryImpl.addOrRefreshModel(ModelRepositoryImpl.java:100) [org.openhab.model.core_1.7.1.jar:na]
	at org.openhab.model.core.internal.folder.FolderObserver.checkFolder(FolderObserver.java:142) [org.openhab.model.core_1.7.1.jar:na]
	at org.openhab.model.core.internal.folder.FolderObserver.run(FolderObserver.java:99) [org.openhab.model.core_1.7.1.jar:na]
2015-10-25 01:40:57.118 [INFO ] [.service.AbstractActiveService] - TCP Refresh Service has been shut down

Have you tried using the JSON Transform instead? Here’s a Rule-based version that extracts the Battery information:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

rule "JSON Test"
  when
    Time cron "0/10 * * * * ?"
  then
    var String json = '{ "ROOMBA": { "status": "3", "cleaning": "0", "battery": "94", "temp": "37", "error": "0", "button": "0" } }'

    var String type = transform("JSONPATH", "$.ROOMBA.battery", json)
    logInfo("json", type)
end

I’m away from home so I can’t try this out myself. However, is it legal to specify a * for the port number. I don’t know TCP would work without a specified port. Also, you may need to add a .* at the end of the regex so that it matches the entire line.

OTOH, I agree with the suggestion to consider the json transform.