Error during the execution of rule: Java heap space

I often get the following error:

Error during the execution of rule 'Get Bee_Sensors': An error occurred during the script execution: Java heap space

Does anyone have any idea what the problem might be with this rule?

rule "Get Bee_Sensors"
when
	Time cron "*/1 * * * * ?"
then
	var response = sendHttpGetRequest(strUrlGateway + 'sensors/', 800)
	var String str_state1
	var String str_lastupdated
	var DateTime date_lastupdated


	if (response !== null) {

		/* ----------------------------------Bee_Sensor_HueTap------------------------------------ */
		str_state1  = transform('JSONPATH', '$.' + iBee_Sensor_HueTap + '.state.buttonevent',  response)
		str_lastupdated  = transform('JSONPATH', '$.' + iBee_Sensor_HueTap + '.state.lastupdated',  response)

		// Init-Value
		if (Bee_Sensor_HueTap_Last.state == null) { Bee_Sensor_HueTap_Last.sendCommand(now.toString) }

		// nur, wenn Einlesen der Zeit möglich ist
		if (str_lastupdated !== null) {
			date_lastupdated = new DateTime(str_lastupdated, DateTimeZone.UTC);
			date_lastupdated = date_lastupdated.toDateTime(DateTimeZone.forID("Europe/Berlin"))

			// nur innerhalb eines Zeitfensters von 15 Sekunden und muss ungleich der Zeit des letzten Tastendrucks sein
			if (date_lastupdated.isAfter(now.minusSeconds(15)) && date_lastupdated.isBefore(now) &&
				(new DateTime((Bee_Sensor_HueTap_Last.state as DateTimeType).getZonedDateTime.toEpochSecond * 1000) != date_lastupdated)) {
				
				Bee_Sensor_HueTap.sendCommand(str_state1)
				Bee_Sensor_HueTap_Last.sendCommand(date_lastupdated.toString)
			}		
		}
		/* --------------------------------------------------------------------------------------- */

		/* -----------------------------Bee_Sensor_HueMotionLightLevel---------------------------- */
		str_state1  = transform('JSONPATH', '$.' + iBee_Sensor_HueMotionLightLevel + '.state.daylight',  response)
		str_lastupdated  = transform('JSONPATH', '$.' + iBee_Sensor_HueMotionLightLevel + '.state.lastupdated',  response)

		// Init-Value
		if (Bee_Sensor_HueMotionLightLevel_Last.state == null) { Bee_Sensor_HueMotionLightLevel_Last.sendCommand(now.toString) }

		// nur, wenn Einlesen der Zeit möglich ist
		if (str_lastupdated !== null) {
			date_lastupdated = new DateTime(str_lastupdated, DateTimeZone.UTC);
			date_lastupdated = date_lastupdated.toDateTime(DateTimeZone.forID("Europe/Berlin"))

			// nur innerhalb eines Zeitfensters von 15 Sekunden und muss ungleich der Zeit des letzten Tastendrucks sein
			if (date_lastupdated.isAfter(now.minusSeconds(15)) && date_lastupdated.isBefore(now) &&
				(new DateTime((Bee_Sensor_HueMotionLightLevel_Last.state as DateTimeType).getZonedDateTime.toEpochSecond * 1000) != date_lastupdated)) {
				
				if ((str_state1 == "true") && (Bee_Sensor_HueMotionLightLevel.state != ON)) {
					Bee_Sensor_HueMotionLightLevel.sendCommand(ON)
				} else if ((str_state1 == "false") && (Bee_Sensor_HueMotionLightLevel.state != OFF)) {
					Bee_Sensor_HueMotionLightLevel.sendCommand(OFF)
				}

				Bee_Sensor_HueMotionLightLevel_Last.sendCommand(date_lastupdated.toString)
			}	
		}
		/* --------------------------------------------------------------------------------------- */

		/* ------------------------------Bee_Sensor_HueMotionPresence----------------------------- */
		str_state1  = transform('JSONPATH', '$.' + iBee_Sensor_HueMotionPresence + '.state.presence',  response)
		str_lastupdated  = transform('JSONPATH', '$.' + iBee_Sensor_HueMotionPresence + '.state.lastupdated',  response)

		// Init-Value
		if (Bee_Sensor_HueMotionPresence_Last.state == null) { Bee_Sensor_HueMotionPresence_Last.sendCommand(now.toString) }

		// nur, wenn Einlesen der Zeit möglich ist
		if (str_lastupdated !== null) {
			date_lastupdated = new DateTime(str_lastupdated, DateTimeZone.UTC);
			date_lastupdated = date_lastupdated.toDateTime(DateTimeZone.forID("Europe/Berlin"))

			// nur innerhalb eines Zeitfensters von 15 Sekunden und muss ungleich der Zeit des letzten Tastendrucks sein
			if (date_lastupdated.isAfter(now.minusSeconds(15)) && date_lastupdated.isBefore(now) &&
				(new DateTime((Bee_Sensor_HueMotionPresence_Last.state as DateTimeType).getZonedDateTime.toEpochSecond * 1000) != date_lastupdated)) {
				
				if ((str_state1 == "true") && (Bee_Sensor_HueMotionPresence.state != ON)) {
					Bee_Sensor_HueMotionPresence.sendCommand(ON)
				} else if ((str_state1 == "false") && (Bee_Sensor_HueMotionPresence.state != OFF)) {
					Bee_Sensor_HueMotionPresence.sendCommand(OFF)
				}

				Bee_Sensor_HueMotionPresence_Last.sendCommand(date_lastupdated.toString)
			}	
		}
		/* --------------------------------------------------------------------------------------- */
		
	}
end

You are running this rule every second!
It can take OH more that a second to execute this rule especially as you have an httprequest and lots on persistence queries and the rule up on top of itself and you run out of threads…

Run it slower, try every 10s

Time cron "*/10 * * * * ?"

Problem is, I’m polling a motion sensor. If the interval’s too long, I’ll be in the dark for a long time. :slight_smile:

Use the http binding to pull the value in an item and cache it
https://docs.openhab.org/addons/bindings/http1/readme.html#example-of-how-to-configure-an-http-cache-item
This way the polling will happen in the binding

Create a String item with the binding
Change your rule trigger to a change on that item

Okay, that’s a very good idea! Thanks for the tip.

How is it possible to store the complete HTTP GET response in a string and then run JSONPATH over it?

The original HTTP GET response looks like this:

 {
     "alert": "none",
     "on": false,
     "reachable": true
}

Stored in the string item it looks like this. But JSONPATH doesn’t work with it anymore.

{alert=none, on=false, reachable=true}

What is your http.cfg?
What is your item definition?

beeCache.url=http://LightLinkPi/api/5D7E8C715E/
beeCache.updateInterval=1000
String  Bee_Light_Gang_State   { http="<[beeCache:2000:JSONPATH($.lights.7.state)]" }

Try:

String  Bee_Light_JSON   { http="<[beeCache:2000:REGEX((.*))]" }

Then trigger the rule above on

rule "Get Bee_Sensors"
when
	Bee_Light_JSON changed
then
	var response = Bee_Light_JSON.toString()