How to reset energy template sensor to zero after botched first attempt?

Hey, after a successful first attempt at monitoring my gas meter via a TCRT5000 (following the approach in Tracking gas usage with ESPHome, Home Assistant, and TCRT5000 | Savjee.be), I extended the YAML and the hardware setup and added a second TCRT, this time to my power meter.

Unfortunately I initially setup the template sensor as “device_class: power” (instead of energy), which resulted in a rediculously high sensor value/state:

[23:20:25][D][sensor:124]: 'Power usage': Sending state 1570730934272.00000 kWh with 2 decimals of accuracy

I fixed the code (device_class: power => device_class: energy), redeployed, but the problem is still that the sensor of course keeps the value from before.

grafik

How can I get a “fresh start”, aka: reset the sensor state/value to zero? I tried removing the code section completely, redeployed (which made the entity unavailable in HA), added the code section again, but after flashing the value is back. Of course I could add another sensor with a slightly different name and just ignore the botched one, but I’d prefer to use the original and just reset it.

How do I do that?

Here is the YAML in case it helps:

esphome:
  name: power8266

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: ""

ota:
  password: ""

wifi:
  ssid: "XXXXXXXX"
  password: "XXXXXXXXX"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Power8266 Fallback Hotspot"
    password: "XXXXXXXXX"

captive_portal:


globals:
  - id: total_e_pulses
    type: int
    restore_value: false
    initial_value: '0'
  - id: total_g_pulses
    type: int
    restore_value: false
    initial_value: '0'


binary_sensor:
  - platform: gpio
    id: electro_pulse_counter
    pin: GPIO12
    filters:
      - delayed_on: 500ms
    on_press:
      then:
        - lambda: id(total_e_pulses) += 1;
  - platform: gpio
    id: gas_pulse_counter
    pin: GPIO5
    filters:
      - delayed_on: 500ms
    on_press:
      then:
        - lambda: id(total_g_pulses) += 1;
        
sensor:
  - platform: template
    name: "Gas usage"
    device_class: gas
    unit_of_measurement: "m³"
    state_class: "total_increasing"
    icon: "mdi:fire"
    accuracy_decimals: 2
    lambda: |-
        return id(total_g_pulses) * 0.01;
  - platform: template
    name: "Power usage"
    device_class: energy
    unit_of_measurement: "kWh"
    state_class: "total_increasing"
    icon: "mdi:flash"
    accuracy_decimals: 2
    lambda: |-
        return id(total_e_pulses) * 0,0026666666666667;

And this is what the log output currently looks like in “normal” operation:

[07:22:20][D][binary_sensor:036]: 'gas_pulse_counter': Sending state ON
[07:22:21][D][binary_sensor:036]: 'electro_pulse_counter': Sending state ON
[07:22:22][D][binary_sensor:036]: 'electro_pulse_counter': Sending state OFF
[07:22:22][D][sensor:124]: 'Gas usage': Sending state 0.04000 m³ with 2 decimals of accuracy
[07:22:24][D][sensor:124]: 'Power usage': Sending state 1570730934272.00000 kWh with 2 decimals of accuracy
[07:22:39][D][binary_sensor:036]: 'gas_pulse_counter': Sending state OFF
[07:22:43][D][binary_sensor:036]: 'gas_pulse_counter': Sending state ON
[07:23:03][D][binary_sensor:036]: 'electro_pulse_counter': Sending state ON
[07:23:05][D][binary_sensor:036]: 'electro_pulse_counter': Sending state OFF
[07:23:11][D][binary_sensor:036]: 'gas_pulse_counter': Sending state OFF
[07:23:15][D][binary_sensor:036]: 'gas_pulse_counter': Sending state ON
[07:23:22][D][sensor:124]: 'Gas usage': Sending state 0.06000 m³ with 2 decimals of accuracy
[07:23:24][D][sensor:124]: 'Power usage': Sending state 1570730934272.00000 kWh with 2 decimals of accuracy
[07:23:39][D][binary_sensor:036]: 'gas_pulse_counter': Sending state OFF
[07:23:42][D][binary_sensor:036]: 'gas_pulse_counter': Sending state ON
[07:23:47][D][binary_sensor:036]: 'electro_pulse_counter': Sending state ON
[07:23:49][D][binary_sensor:036]: 'electro_pulse_counter': Sending state OFF
[07:24:09][D][binary_sensor:036]: 'gas_pulse_counter': Sending state OFF
[07:24:13][D][binary_sensor:036]: 'gas_pulse_counter': Sending state ON
[07:24:22][D][sensor:124]: 'Gas usage': Sending state 0.08000 m³ with 2 decimals of accuracy
[07:24:24][D][sensor:124]: 'Power usage': Sending state 1570730934272.00000 kWh with 2 decimals of accuracy
[07:24:32][D][binary_sensor:036]: 'electro_pulse_counter': Sending state ON
[07:24:33][D][binary_sensor:036]: 'electro_pulse_counter': Sending state OFF
[07:24:39][D][binary_sensor:036]: 'gas_pulse_counter': Sending state OFF
[07:24:42][D][binary_sensor:036]: 'gas_pulse_counter': Sending state ON
[07:25:15][D][binary_sensor:036]: 'electro_pulse_counter': Sending state ON
[07:25:18][D][binary_sensor:036]: 'electro_pulse_counter': Sending state OFF
[07:25:22][D][sensor:124]: 'Gas usage': Sending state 0.09000 m³ with 2 decimals of accuracy
[07:25:24][D][sensor:124]: 'Power usage': Sending state 1570730934272.00000 kWh with 2 decimals of accuracy

Thanks a lot in advance :slight_smile:

You might try logging the value of total_e_pulses

Should that be 0.0026666666666667 (not comma)

1 Like

Curse the gods that struck us with locale issues…
Thank you so much, that solved the problem of the value being completely off :slight_smile:

image

The only remaining issue:
HA still “remembers” the old values and uses those as the max reference point:
image

Is that a lowlevel editing thing now to dig into the underlying db to remove those values? I would like to get rid of them before I add the sensor to the energy dashboard…

Cheers!

Developer Tools → Statistics. Click on the little ramp icon to the right of your sensor. You should then be able to edit past values.

1 Like

Thanks a lot, I found the entries and actually set all of the ones I found back to zero, but the actual history/graph doesn’t change through that :-/

Example:

image

But when I then go back into the editing mode, there is simply no entry for the time in question that is not zero:

Maybe the history graphs are rendered from a different/cached datasouce?

Yeah sorry, history graphs are from the recorder database states table(s). Not the Long Term Stats (LTS) table(s).

Good news is that this will auto purge after 10 days (unless you have changed the default).

LTS data does not purge, which is why there is a tool to edit it.

Ah, great, makes total sense :slight_smile: Thanks!
Then I just hope that the energy dashboard reads from LTS and not the recorder data…

Yes it does.

You can use sql to delete the data.

1 Like

Thanks for the help guys, the energy dashboard looks good now, and I can wait the 9 days until the graphs in the history are purged

Cheers! :slight_smile:

It creates the LTS data for energy, but it does it in real time as the values come in, not retroactively. So the past data LTS energy that you fixed will not get generated again and the SQL data will be gone in 10 days.

1 Like

Hi there,

i did the following steps:

  • i dropped the whole HA DB…
  • cut all integral sensors out of the yaml…
  • restart HA
  • Recorder says, DB not found (correct)
  • re-integrates the integral sensors to the yaml
  • rebuild the empty database
  • restart HA

and now:

the integral sensors shows values: 2.000 kwh, 300 kwh…

where HA stores this values? Not in the DB… this was completly new…

thanks for help…

regards
Daniel