smoother response from a BH1750

Hi all, i have a BH1750 attached to an esp-12 publishing the current LUX via MQTT to a node-red dashboard. i’m then going to use the Lux value to say if a light should come on or not, pretty basic stuff really.

but i cant get a smooth light level so to speak. the line on the chart goes all over the shop. for example if i have it in a conservatory as the sun goes in and out the bar graph goes high then low then high again quite quickly. so if it was on the the edge of the LUX level the light would be flicking on and off all over the place if that makes sense. i also want to use this for a blind control so when its bright pulls a blind down.

tried to get a smoother response by adding a few together then dived to get an average and then say unless it’s 10 either way do nothing. but it can drop drastic during high light levels but at low it doesn’t so much. so its not great either way as means lights take ages to come on if levels dont drop enough or in lighter levels blind would be going up and down all over the place!!

i could do a time delay for say 1 min but then if its a hall lamp and i turn the main lights on, the hall lamp would stay on for up to a minute if that makes sense and also it has to keep reconnecting to the mqtt server as its dropped out as not doing the client connected bit each time

any one got any ideas!!!

  float valf = 0;
  float newvalf = 0;
  for (int i = 1; i <= 10; i++) {
    valf = 0;
    if (BH1750_Read(BH1750_address) == 2) {
      valf = ((buff[0] << 8) | buff[1]) / 1.2;
      newvalf = newvalf + valf;
      delay(100);
    }
  }
  newvalf = newvalf / 10.0;
  newvalf = newvalf / 4.0;
  newvalue = ((int)newvalf);

  if (newvalue < previousvalf - 10 || newvalue > previousvalf + 10 ) {
    Serial.println(dtostrf(newvalf, 8, 2, tBuffer));
    client.publish(outTopic, tBuffer);
    previousvalf = newvalue;
  }