Extracting a value from a String

I’m trying to extract a status value from a string, and JSONPATH doesn’t seem to work, I think because the format doesn’t put each item inside quotes, I would like to extract the value of “cycle” and also “phase” from the string below, can anyone advise how I can do this from a rule?

{ ok:
{ flags: 4,
 cycle: 'dock',
 phase: 'hmUsrDock',
 pos: { theta: 0, point: [Object] },
 batPct: 93,
 expireM: 0,
 rechrgM: 0,
 error: 0,
 notReady: 0,
 mssnM: 0,
 sqft: 0 },
id: 1 }

A regular expression like the following should work.

REGIX(.*cycle\: \'(.*)\'.*)

You don’t need a rule for this. If you do it from a rule, the best way is to use a regular expression also so you may as well save the step.

I’m not sure I totally understand how to do this.
I have in the rule:

var String Status = executeCommandLine ('"node" "/home/pi/data/status.js"', 10000)
var String Cycle = <not sure what to put in here>
var String Phase = <not sure what to put in here>

The first line generates the string I put in the first post, lines 2 and 3 are going to extract the information which will be used for further processing in the rule

The way you worded your original posting I assumed you were using the transform on your Item definition.

From the wiki:

RegEx transformation service

The Regex transformation service matches a string against a regular expression with at least one capturing group. The first capturing group becomes the result of the transformation. Note that the provided regular expression will be anchored at the beginning and end of the string ("^" and “$” will be added around the provided expression).

Example item configuration:

Returns the string.
http="<[http://example.com/var/value:60000:REGEX((.*))]" }

Extract a integer (e.g., “Result: 123” ==> “123”)
http="<[http://example.com/var/value:60000:REGEX(.?([0-9]+).)]" }

Example in rules:

transform(“REGEX”, “STATUS=(.*)”, “STATUS=OK”)

So you would use:

var String Cycle = transform("REGEX", ".*cycle\: \'(.*)\'.*")

Thanks, that makes more sense :slight_smile: