Tuya-cli get uses ' ' instead of " ", how to transform?

  • Platform information:
    • Hardware: Orangepi 3 LTS
    • openHAB version: 3.4
  • Issue of the topic:

Tuya-cli get provides measurement data from sensors in the following format

{ '1': 180, '2': 250, '4': 100 }

Openhab does not like this. I guess for it to be a proper json this should rather be

{"1": 180, "2": 250, "4": 100}

Can this transformation be done inside OpenHAB?

That should be perfectly fine. What errors are you seeing that indicate OH doesn’t like the single quotes?

This is what it churns out:

[ab.binding.mqtt.generic.ChannelState] - Incoming payload '{ '1': 183, '2': 249, '4': 100 }
' not supported by type 'NumberValue'

I did a quick and dirty hack to replace ’ with " like this. There has to be a more elegant way to do this, I guess:

radioHuone=radioHuone.replace(" '","\"").replace(": ",":").replace(" '","\"").replace("'","\"").replace("'","\"").replace(": ",":").replace(" '","\"").replace("': ","\":").replace(" }","}");

But it works…

So this is kind of solved?

OK, well that’s because you’ve created a Number Channel which expects a number or a String that can be parsed into a number and you are trying to give it a JSON.

The problem isn’t the ', the problem is you either need to change the Channel to a String type Channel or you need to use JSONPATH to extract one of those values from the JSON for the Channel to use.

1 Like

I tried, but gave up when JSONPath Online Evaluation threw a

JSON Parse Error

when I suggested

{ '1': 180, '2': 250, '4': 100 }

to it…

This does not help you, but out of curiosity I did a quick google search.

JSON does indeed only allow double quotes.
And so does the JSONPath Online Evaluation.

https://www.json.org/json-en.html#:~:text=wrapped%20in%20double%20quotes

I would suggest to file an issue at the Tuya-cli repository.

Issues · TuyaAPI/cli · GitHub (if this is the right one)

2 Likes

Done.

As a workaround:
Use a REGEX transformation to replace all ’ with ", see RegEx - Transformation Services | openHAB. Then use the transformation concatenation operator to append a JSONPATH transformation.

Unfortunately, since this is almost certainly coming from the Exec binding, the concatenation operator is not supported. To my knowledge, only the MQTT and HTTP bindings support that.

However, the REGEX could be defined on the Channel and a transform Profile could be applied on the Link.

If this is through executeCommandLine, a simple .replace("'", "\"") should be sufficient and then the transform Action can be called with JSONPATH.

var json = transform("JSONPATH", "$.1", executeCommandLine(...).replace("'", "\""))

You’d probably want to start by breaking that up so you can log out intermediate steps.