Increment number decimal error

Hi, i have a problem in a simplex automation that increment 2 my numeric helper.
Sometimes increment of 0,00000000001 up of the correct value.
The 2 helper increment 0,0225 for each times.

alias: Conta metri cubi gas
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.shelly1_e09xxxxx
    from: "off"
    to: "on"
condition: []
action:
  - repeat:
      until:
        - condition: state
          entity_id: switch.shelly1_
          state: "off"
      sequence:
        - service: input_number.increment
          data: {}
          target:
            entity_id: input_number.totale_consumo_gas
        - service: input_number.increment
          data: {}
          target:
            entity_id: input_number.consumo_giornaliero
        - delay:
            hours: 0
            minutes: 1
            seconds: 0
            milliseconds: 0
mode: single

I have some difficulty understanding what you’ve written, but it seems like you’re tracking gas consumption.

What are the definitions for those helpers? Do they have a step size?

Is your consumption assumed to be constant? If so, just make a template sensor for each that has a fixed value when your switch is on and zero when off. Then use a utility meter tot track the consumption. This will avoid many issues with your current solution.

Yes, i track gas consumption.
Helper are input_number with increase step of 0,0225.
This automatation Simply increment 2 helper After 1 minut of climate Is on.
Maybe your solution it’s Better, but i use automatation because i’m no very able with template

What does this mean? Are you saying it’s incrementing at that rate instead of the configured step size, or that the total is out by this much?

Have you tried?

template:
  - sensor:
      - name: "Gas Consumption"
        device_class: gas
        unit_of_measurement: m³
        state: >-
          {{ 0.0225 if is_state('switch.shelly1_', 'on') else 0 }}

utility_meter:
  daily_gas_consumption:
    source: sensor.gas_consumption
    cycle: daily

And if you give the sensor a state_class you can add it to your energy dashboard and it will do everything for you.

Thanks for the reply, i want to make a template and i’m my Little free time i study for this solution.
My automation increase 1 times a minute the helper of Total consumption of gas.
The helper as 0,0225 increment step, but sometimes (i don’t understand why) the my automation increase the helper of 0,00000001 instead 0,0225.
The template you post i think is incorret.
The sensor has ti increment One time a minut, only when shelly relè Is on

Sorry for reviving this thread, but I am currently experiencing the same issue. My setup:

  • A door-sensor closes whenever my gas meter changes the 2nd decimal digit on its display.

  • I added a helper (input_number.gasmeter_reading) which should reflect the value on the gas meter display. Since the door sensor closes for every change of the 2nd decimal digit (i.e. one pulse equals an increase by 0.01m3), the step size of input_number.gasmeter_reading is set to 0.01 as well.

  • An automation increases the input_number.gasmeter_reading by one step whenever the sensor on the actual gasmeter re-opens:

  alias: Gaszähler Impulse verarbeiten (Process gasmeter pulses)
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.gasmeter_pulse
    to: 'on'
  condition: []
  action:
  - service: counter.increment
    target:
      entity_id: counter.gasmeter_pulsecounter
    data: {}
  - service: input_number.increment
    target:
      entity_id: input_number.gasmeter_reading
    data: {}
  mode: single

Note: A second helper counter.gasmeter_pulsecounter is also getting incremented by this automation.

The problem: Sporadically (every 20-40 pulses) the input_number.gasmeter_reading gets incremented by 0.01000000001 instead of the desired increase by 0.01.

I have not yet discovered why this is happening and how I prevent it. Does the mode of the automation maybe have anything to with this issue? Any ideas would be appreciated.

For anyone trying to resolve this issue: I found that the decimal error when incrementing a value is a problem with how python handles floats (also see this post).

I “fixed” the issue by rounding the value of the input_number each time it gets incremented by adding an action to my automation:

service: input_number.set_value
metadata: {}
data:
  value: "{{ states.input_number.gasmeter_reading.state|round(2) }}"
target:
  entity_id: input_number.gasmeter_reading
1 Like