Finding maximum sensor data

Hi,
How do I convert Arduino code to Openhab code to find maximum sensor value?

Arduino code:

const int sensorPin = A0;
int sensorReading = 0; 
int maxReading = 0;

void setup() 
{
Serial.begin(9600);
}

void loop() 
{
  sensorReading = analogRead(sensorPin);
  if (sensorReading > maxReading) maxReading = sensorReading;
  
  Serial.print(maxReading);         
  Serial.print('\t');         
  Serial.println(sensorReading);
  
  delay(100); 
}

Openhab rule code:


rule "FSR Maximum"
        when
			Item itm_hj_fsranalog_mqtt received update
        then
        
        var int maxfsr = 0
        var int fsranalog123 = (itm_hj_fsranalog_mqtt.state as DecimalType).intValue 
		
		if (fsranalog123 > maxfsr){
		maxfsr = fsranalog123;
		postUpdate(Maxfsr, maxfsr) 
		}

end

I tried to convert the code. In Arduino, the sensors gave the maximum value.
But, in Openhab it followed the sensor real time value.
Can you help me, how to solve this?

Hello,

may be your problem ist in the line

You trigger this line on every update and maxfsr is set to 0 again.

Thats way your maxvalue is allways the same to your sensor value.

In your arduino code you make the inititalization just once outside your reading loop.