Split string to array

Trying to decode a message from mqtt that contains several values like this one:

{"CurrentLevel":1000,"DeviceId":18,"DeviceName":"_Light Sensor","DeviceType":"urn:schemas-micasaverde-com:device:LightSensor:1","OldCurrentLevel":0,"RoomId":1,"RoomName":"Garage","ServiceId":"urn:micasaverde-com:serviceId:LightSensor1","Time":1487402816,"Variable":"CurrentLevel"}

I created a script that can read one value that I use like this:

In Items I have this:

Number Lightlevel “Light [%d] Lux” (all) {mqtt="<[skynet:survey/Events/_Light Sensor:state:JS(getVeraMqtt.js)]"}

And in the script file getVeraMqtt.js:
(function(csv) {
var arr = csv.split(",");
var ret = arr[0].split(":");
return ret[1];

That return 1000 and thats good.
I would like to return a array with all the values: Can I do that and if so how do I do that?

That is a JSON-string!
Why don’t you use something like this in arule?

var int MyCurrentLevel = transform(“JSONPATH”, “$.CurrentLevel”, json)

where json is the sting containing your complete string(including the curly brackets).

1 Like

And make the Item as follows?

String Lightlevel “json string” (all) {mqtt="<[skynet:survey/Events/_Light Sensor:state:default]"}

Number Lightlevel “Light [%d] Lux” (all) {mqtt="<[skynet:survey/Events/_Light Sensor:state:JSONPATH ($.CurrentLevel)]"}

I think…

1 Like