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.
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