JSONPATH transform returning unexpected results

  • Platform information:
    • Hardware: Raspberry Pi Model 3B+
    • OS: Linux devpi 5.10.103-v7+ #1529 SMP Tue Mar 8 12:21:37 GMT 2022 armv7l GNU/Linux
    • Java Runtime Environment: OpenJDK Runtime Environment Zulu11.54+25-CA (build 11.0.14.1+1-LTS)
    • openHAB version: 3.2.0 - Release Build

BACKGROUND: I am writing a rule to turn on/off a collection of switches based on the power being generated by my solar panels - the basic objective is to use all the power being generated and not export any to the grid. The sorts of devices being turned on/off are pool pumps, hot tub heaters, underfloor fans etc.

The user gets to configure up to 10 switches with details such as:

  1. Watts drawn by the device plugged into the switch
  2. Switch priority (high, medium, low) - used by my rule to determine what to turn on first
  3. Minimum minutes it should stay on once turned on (to prevent flapping)
  4. Minimum minutes it should stay off once turned off (to prevent flapping)
  5. Maximum minutes it should be on in one day

Once the configuration is complete, the data needs to be stored in a “database” that I can query easily. I have chosen a JSONPATH string to store this data (let me know if you think there is a better way). I have validated my JSONPATH string OK. It looks like this:

{
	"Consumers": [{
		"ID": 0,
		"DisplayName": "Pool Pump",
		"ItemName": "Poolpumps_switch1",
		"Priority": 0,
		"WattsDrawn": 1000,
		"MaxDailyMins": 0,
		"MinONMins": 10.0,
		"MinOFFMins": 10.0
	}, {
		"ID": 1,
		"DisplayName": "Hot Tub",
		"ItemName": "Hottub_switch1",
		"Priority": 1,
		"WattsDrawn": 210,
		"MaxDailyMins": 0,
		"MinONMins": 10.0,
		"MinOFFMins": 10.0
	}, {
		"ID": 2,
		"DisplayName": "Underfloor Fans",
		"ItemName": "Underfloorfans_switch1",
		"Priority": 2,
		"WattsDrawn": 50,
		"MaxDailyMins": 0.0,
		"MinONMins": 10.0,
		"MinOFFMins": 10.0
	}, {
		"ID": 3,
		"DisplayName": "unused",
		"ItemName": "unused",
		"Priority": null,
		"WattsDrawn": null,
		"MaxDailyMins": null,
		"MinONMins": null,
		"MinOFFMins": null
	}, {
		"ID": 4,
		"DisplayName": "unused",
		"ItemName": "unused",
		"Priority": null,
		"WattsDrawn": null,
		"MaxDailyMins": null,
		"MinONMins": null,
		"MinOFFMins": null
	}, {
		"ID": 5,
		"DisplayName": "unused",
		"ItemName": "unused",
		"Priority": null,
		"WattsDrawn": null,
		"MaxDailyMins": null,
		"MinONMins": null,
		"MinOFFMins": null
	}, {
		"ID": 6,
		"DisplayName": "unused",
		"ItemName": "unused",
		"Priority": null,
		"WattsDrawn": null,
		"MaxDailyMins": null,
		"MinONMins": null,
		"MinOFFMins": null
	}, {
		"ID": 7,
		"DisplayName": "unused",
		"ItemName": "unused",
		"Priority": null,
		"WattsDrawn": null,
		"MaxDailyMins": null,
		"MinONMins": null,
		"MinOFFMins": null
	}, {
		"ID": 8,
		"DisplayName": "unused",
		"ItemName": "unused",
		"Priority": null,
		"WattsDrawn": null,
		"MaxDailyMins": null,
		"MinONMins": null,
		"MinOFFMins": null
	}, {
		"ID": 9,
		"DisplayName": "unused",
		"ItemName": "unused",
		"Priority": null,
		"WattsDrawn": null,
		"MaxDailyMins": null,
		"MinONMins": null,
		"MinOFFMins": null
	}]
}

ISSUE: I have used jsonquerytool.com to construct a query to get a list of IDs that do not have a DisplayName of “unused”. The query is $..[?(@.DisplayName!="unused")].ID and in the query tool it returns [ 0, 1, 2 ] as expected.

In the openHAB rule, I have the following:

str =  transform("JSONPATH", '$..[?(@.DisplayName!="unused")].ID', MPConsumersString.state.toString)
str2 = transform("JSONPATH", '$..[?(@.DisplayName=="unused")].ID', MPConsumersString.state.toString)
logInfo("MP", "str=" + str)
logInfo("MP", "str2=" + str2)

The output I see in the log is:

str=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
str2=[3, 4, 5, 6, 7, 8, 9]

str2 is as expected (IDs 3-9 have a DisplayName of “unused”)
str is unexpected. I expected just str=[0, 1, 2].
I don’t know why I am getting ID’s 0-9 and 0-2.

Can someone see what I am doing wrong?

Thanks in advance.

I’m not smart enough to solve this problem, other than to say that the documentation recommends using a different online validator.

Please try this pattern:

'$.Consumers[?(@.DisplayName!="unused")].ID'
1 Like

Thanks! That fixed it.