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="<[*:'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 I can extract the battery value from the string by using this expression:


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?


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="<[*:'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 '' 
	at org.openhab.binding.tcp.protocol.internal.ProtocolGenericBindingProvider.createCommandFromString( ~[na:na]
	at org.openhab.binding.tcp.protocol.internal.ProtocolGenericBindingProvider.parseBindingConfig( ~[na:na]
	at org.openhab.binding.tcp.protocol.internal.ProtocolGenericBindingProvider.parseAndAddBindingConfig( ~[na:na]
	at org.openhab.binding.tcp.protocol.internal.ProtocolGenericBindingProvider.processBindingConfiguration( ~[na:na]
	at org.openhab.binding.tcp.protocol.internal.TCPGenericBindingProvider.processBindingConfiguration( ~[na:na]
	at org.openhab.model.item.internal.GenericItemProvider.internalDispatchBindings( [org.openhab.model.item_1.7.1.jar:na]
	at org.openhab.model.item.internal.GenericItemProvider.internalDispatchBindings( [org.openhab.model.item_1.7.1.jar:na]
	at org.openhab.model.item.internal.GenericItemProvider.processBindingConfigsFromModel( [org.openhab.model.item_1.7.1.jar:na]
	at org.openhab.model.item.internal.GenericItemProvider.modelChanged( [org.openhab.model.item_1.7.1.jar:na]
	at org.openhab.model.core.internal.ModelRepositoryImpl.notifyListeners( [org.openhab.model.core_1.7.1.jar:na]
	at org.openhab.model.core.internal.ModelRepositoryImpl.addOrRefreshModel( [org.openhab.model.core_1.7.1.jar:na]
	at org.openhab.model.core.internal.folder.FolderObserver.checkFolder( [org.openhab.model.core_1.7.1.jar:na]
	at [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"
    Time cron "0/10 * * * * ?"
    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)

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.