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?
tom_l
May 30, 2024, 3:23am
2
Click the đ icon next to the sensor. Then click the gear icon at the top of the pop-up card.
Adjust the Display Precision
option to what you need.
Hi tom ,
I have tried that . It just goes from
To
But the esp still says 0.28 right now âŚ
tom_l
May 30, 2024, 3:28am
4
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.
tom_l
May 30, 2024, 6:10am
9
Youâre forgetting the filters they applied. One pulse x 0.28 is equal to 0.28.
tom_l
May 30, 2024, 6:12am
10
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âŚ
tom_l
May 30, 2024, 12:03pm
12
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:
opened 02:52PM - 16 Sep 21 UTC
closed 03:12AM - 18 Jan 22 UTC
stale
integration: pulse_meter
### The problem
The pulse_meter default accuracy is 2 decimals, but the totals ⌠is reported as 0 decimals, I have to manually override the totals accuracy to 2 decimals.
The totals section could inherit the accuracy, icon and device_class from the parent counter, as is it has to be specified twice.
### Which version of ESPHome has the issue?
2021.9.0
### What type of installation are you using?
Docker
### Which version of Home Assistant has the issue?
2021.9
### What platform are you using?
ESP32
### Board
devkitc
### Component causing the issue
pulse_meter
### Example YAML snippet
```yaml
# https://esphome.io/components/sensor/index.html
sensor:
# https://esphome.io/components/sensor/pulse_meter.html
# https://esphome.io/components/sensor/pulse_counter.html
- platform: pulse_meter
pin:
number: GPIO32
mode: INPUT_PULLUP
unit_of_measurement: 'gal/min'
name: ${device_name}_water_meter
icon: 'mdi:water'
# There is no water class
# device_class: 'water'
filters:
- multiply: 10
total:
name: ${device_name}_water_meter_total
# Default device class and icon is not inherited
icon: 'mdi:water'
# There is no water class
# device_class: 'water'
id: water_total
unit_of_measurement: 'gal'
# Manually set the accuracy to match the main sensor
accuracy_decimals: 2
filters:
- multiply: 10
- platform: pulse_meter
pin:
number: GPIO33
mode: INPUT_PULLUP
unit_of_measurement: 'ftÂł/min'
name: ${device_name}_gas_meter
icon: 'mdi:fire'
device_class: 'gas'
filters:
- multiply: 10
total:
name: ${device_name}_gas_meter_total
# Default device class and icon is not inherited
icon: 'mdi:fire'
device_class: 'gas'
id: gas_total
unit_of_measurement: 'ftÂł'
# Manually set the accuracy to match the main sensor
accuracy_decimals: 2
filters:
- multiply: 10
# https://esphome.io/components/sensor/template.html
- platform: template
name: ${device_name}_water_meter_total_l
# There is no water class
# device_class: 'water'
state_class: 'total_increasing'
accuracy_decimals: 2
unit_of_measurement: 'l'
lambda: |-
return id(water_total).state / 3.785;
- platform: template
name: ${device_name}_gas_meter_total_m3
device_class: 'gas'
state_class: 'total_increasing'
accuracy_decimals: 2
unit_of_measurement: 'mÂł'
lambda: |-
return id(gas_total).state / 35.315;
```
### Anything in the logs that might be useful for us?
```txt
- platform: pulse_meter
pin:
number: 33
mode: INPUT_PULLUP
inverted: false
unit_of_measurement: ftÂł/min
name: utility-pulse-counter_gas_meter
icon: mdi:fire
device_class: gas
filters:
- multiply: 10.0
total:
name: utility-pulse-counter_gas_meter_total
icon: mdi:fire
device_class: gas
id: gas_total
unit_of_measurement: ftÂł
filters:
- multiply: 10.0
disabled_by_default: false
force_update: false
accuracy_decimals: 0
state_class: total_increasing
disabled_by_default: false
force_update: false
accuracy_decimals: 2
state_class: measurement
internal_filter: 13us
timeout: 5min
```
### Additional information
I added the template sensors to convert the imperial units to metric, hoping HA would like my gas meter, but still no :(
1 Like
tom_l
May 30, 2024, 9:45pm
15
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.