Manual Utility Meter - How to Prevent Changes When Reloading Template Entities?

I have an input_number sensor that gets a number manually added. This sensor feeds a Template Sensor and this Template Sensor then feeds a Utility Meter so it can calculate the running total. Each time the Template Entities is reloaded (e.g. I’m testing a new template sensor), it adds whatever value was last in the input_number sensor (and therefore the Template Sensor) to the Utility Meter, even though the value has not changed. Is it possible to prevent this from happening with the Template Sensor or the Utility Meter?

My current workaround is to change the input_number to log the change and then immediately set it back to 0. This appears to work, but it seems like there should be a smarter way to accomplish this. Any suggestions?

Please share your template sensor config. You probably need an availability template.

Also are you using a running total in your input number or are you inputting delta values?

Finally, why are you using a template sensor?

I’m pretty sure the utility meter can work with input_numbers as a source sensor.

Here’s what I have and you’ll notice the availability hashed out since it didn’t work for me in my tests.

- sensor:
  - name: "DTE Running Bill Cost"
    unique_id: "dte_running_bill Cost"
    state_class: "total"
    unit_of_measurement: "USD"
    device_class: "monetary"
    state: >-
      {% set cost = states('input_number.dte_monthly_bill') | float %}
      {{ (cost) | round(2) }}
#    availability: >-
#      {{
#      [ states('input_number.dte_monthly_bill'),
#      ] | map('is_number') | min
#      }}

I’m inputting the delta values. It’s my monthly bill. I’m using this data point to see what I’m paying out of pocket versus what my costs would have been without solar.

via the Helper UI, the dropdown only allows selecting “sensor.” entities. I haven’t tried to create this utility meter in YAML to see if it’ll take.

While it’s not ideal, inputting the monthly bill total and then immediately changing the input number to zero appears to work. It’d be great if it didn’t require this extra step…and even better if my energy company had an API to pull in this balance automatically…

Using a yaml defined utility meter like this:

    name: DTE Running Bill Cost
    source: input_number.dte_monthly_bill
    delta_values: true

Would probably just move the issue. It would happen when you reloaded your input numbers.

If you used a triggered template sensor you might be able to avoid the entry on reload (untested):

  - trigger:
      - platform: state
        entity_id: input_number.dte_monthly_bill
        not_from: 'unknown'
      - name: "DTE Running Bill Cost"
        unique_id: "dte_running_bill Cost"
        state_class: "total"
        unit_of_measurement: "USD"
        device_class: "monetary"
        state: "{{ states('input_number.dte_monthly_bill') | float(0) | round(2) }}"

This will trigger whenever the input number changes and will set the sensor to the input number value. It shouldn’t trigger when templates are reloaded. But as I said, it is untested.

Thanks for this. I did just try the triggered template sensor, and at first it made both the sensor and utility meter report unavailable values until I changed the input number to 1. I then tested reloading template entities and it continued to increase the utility meter by 1 with each reload of Template Entities.

What about if you use:

        - "unknown"
        - "unavailable"

same result. even with those values, the input number set to 1 still increases the utility meter with each reloading of Template Entities.