Why do HA keep rounding my esphome sensor

I have this pulse counter in esphome:

  - platform: pulse_counter
    icon: 'mdi:weather-rainy'
    id: greenhouse_rain_counter
    pin: 
      number: 18
      mode: INPUT_PULLUP
    unit_of_measurement: 'mm/min'
    name: 'Greenhouse Rain Rate per min'
    filters:
      - debounce: 0.2s
      - lambda: 'return x * 0.28;'
    total:
      icon: 'mdi:weather-rainy'
      unit_of_measurement: 'mm/day'
      name: ' Greenhouse Total Rain per day'
      id: greenhouse_total_rain
      filters:
        - debounce: 0.2s
        - lambda: |-
            ESP_LOGD("custom", "Pulse count: %d", x);
            float result = x * 0.28;
            ESP_LOGD("custom", "Total Rain (before rounding): %.5f mm", result);
            result = round(result * 100.0) / 100.0;  // Ensure two decimal places
            ESP_LOGD("custom", "Total Rain (rounded): %.2f mm", result);
            return result;

Both of the sensors seems to be working fine in the esp.
The logs shows something like:

[05:00:57][D][custom:202]: Total Rain (before rounding): 0.28000 mm
[05:00:57][D][custom:204]: Total Rain (rounded): 0.28 mm

Which is great. But as soon as I take a look at the HA sensors it has rounded the total counter:

Why?

Click the 🛈 icon next to the sensor. Then click the gear icon :gear: at the top of the pop-up card.

Adjust the Display Precision option to what you need.

Untitled

Hi tom ,

I have tried that . It just goes from
image

To

image

But the esp still says 0.28 right now …

What happens if you remove the logging from the lambda filter?

Remove it completely so no lambda at all just raw counter ?
Or
Just do like:

- lambda: 'return x * 0.28;'

Then I go back to why I added the logging . I wondered if the data inside the esp already rounded at some point. So I added the logging to verify that the esp kept the decimals.

If you want me to I can do it again ?

Its just strange that the other sensors shows up perfectly fine as a float in HA states but the other more like a int…

Just tried to do like:

- lambda: |-
            float result = x * 0.28;
            return result;

Its the same . Still shows up like 0 and not 0.00

Alright so if I just do like this:

    total:
      icon: 'mdi:weather-rainy'
      unit_of_measurement: 'mm/day'
      name: ' Greenhouse Total Rain per day'
      id: greenhouse_total_rain
      accuracy_decimals: 1
      filters:
        - debounce: 0.2s
        - lambda: 'return x * 0.28;'
        - round: 1

Now it seems to be working. I think its the round: 1 that makes the difference.

Well, it’s pulse counter, so I’d say that the initial result is logical - it’s rounded to zero decimals because it just counts pulses (1, 2, 3…) - you can’t have “quarter of a pulse”, pulse is a binary thing, either you get it or you don’t… so, perhaps you’d be better to use a template sensor, using existing pulse counter sensor.

You’re forgetting the filters they applied. One pulse x 0.28 is equal to 0.28.

I was also going to suggets replacing that with this filter:

- multiply: 0.28
1 Like

True, but, as i said, initial result was somewhat expected, unless you change default decimals…

Are you saying that the counter total always outputs an integer no matter what filters are applied (even those that would generate floating point numbers)?

It surely seems that way… it seems that round is fixed at zero in this case.
Although, i agree that it shouldn’t be if any filters are applied… (a bug, perhaps?)

Seems like it has been a bug for some time:

1 Like

Great find.

Yeah ok that is a problem and there’s no guarantee that your found solution will keep working in future as the devs seem to be of the opinion that the output should be an integer. I reckon this would be your best option:

Use an on_value / update_component automation to update your template sensor whenever the counter total changes.