GUVA-S12SD UV sensor

Hi, im using a GUVA-S12SD for measuring UV index.

The fluxuation is crazy from 1 to 10 in the space of 1 minute, changing all the time.

Any ideas why this is?

Thanks

sensor:
  - platform: adc
    pin: GPIO33 # use your desired input pin here
    name: "ADC voltage"
    update_interval: 60s
    id: "uv_index_source"
    accuracy_decimals: 3
    internal: true

  - platform: template
    name: "UV index"
    accuracy_decimals: 0
    unit_of_measurement: ""
    icon: "mdi:sun-wireless"
    lambda: |-
          if (id(uv_index_source).state < 0.05)
            return 0;
          if (id(uv_index_source).state <= 0.227)
            return 1;
          if (id(uv_index_source).state <= 0.32)
            return 2;
          if (id(uv_index_source).state <= 0.4)
            return 3;
          if (id(uv_index_source).state <= 0.5)
            return 4;
          if (id(uv_index_source).state <= 0.6)
            return 5;
          if (id(uv_index_source).state <= 0.69)
            return 6;
          if (id(uv_index_source).state <= 0.8)
            return 7;
          if (id(uv_index_source).state <= 0.88)
            return 8;
          if (id(uv_index_source).state <= 0.97)
            return 9;
          if (id(uv_index_source).state <= 1.08)
            return 10;
          return 11;

Hey there, not sure if you figured it out yet? Wether by yourself or with any help…
Although I’m not expert in programming, I think it will help if you alter your template.
Why…? Because 0.30 is <= 0.32 but also <=0.4/0.5/0.6 and so on…
Not sure how but maybe something like:

if (id(uv_index_source).state >= 0.69) and (id(uv_index_source).state <= 0.8

  • return 8*
    You want the “if”-condition to return 0.8 when voltage >=0.69 AND voltage <=0.8!

That is probably not the problem. The lambda is using the early return pattern. There are multiple schools of thought on that pattern. The code above seems to use it excessively, but it likely isn’t incorrect.

I suspect the real problem is a combination of the ADC in the esp32 is not that good and really needs to have conditioning and averaging to be anything close to usable. Also the sensor itself might not be that accurate.

https://forums.adafruit.com/viewtopic.php?t=197086

Also,
there is nothing on that code presented updating the template sensor.
And attenuation is left on default.
And if you don’t like to leave the return pattern “in hands of esphome”, you can use else if instead of if.