I changed to binary_sensor as well. The pulses where count in HA using history_stats.
From that I use template sensor to calculate gas consumption in m³. This sensor I add to the energy dashboard.
Works good for now
I would like to use this, too. I also use an ESP32 but with the IN-Z65. I would like to know how to install this in Home Assistant? Or do i just have to copy the files and folders to the /config/esphome-directory?
My current code works, but the recorded values and the real value on the meter are more and more different while it records.
Thank you for your answer. i had installed esphome via settings > esphome. it is up and running and i was able to flash my esp32 with the yaml-code i posted.
My Question was how i could install this for my sensor?
I’ve just taken this code (thanks) to implemented my own gas sensor. I had to make some minor changes (text translation to English, adjusting impulse factor etc.) and thought it was working fine. Then after a while I noticed the meter display kept ‘drifting’ from the actual reading. At first I thought the sensor was missing pulses so I watched it for while and it didn’t miss a beat. Then I noticed the following in the ESPHome logs. Gasmeter Display’: Sending state 30500.49805 m³ with 2 decimals of accuracy
The Gasmeter display variable (gas_meter_displayvalue) seems as if its being stored to 5 decimal places. Even more strange is I only set it to 2 decimal places in my code (e.g. 30500.49), only increment by 0.01 (pulse factor) on each pulse so no idea why this is.
When I track the updates to Gasmeter Display in the log file on each update it is incremented by .00997 or 00998 even though I have the pulse factor to 0.01.
This explains why the figure drifts after a while in HA as this only stores/displays to decimal places but I’ve no idea why ESPHOME has it to 5 decimal places and the fractional increments,
The web server settings are a little bit surprising to me. The web page wasn’t loading, so I checked the yaml code.
Web server defined twice
It seems to me like there are two definition of the web server. The first one is used, changed to the webserver.yaml are ignored. I propose to remove the lines in base.yaml
common/base.yaml: line 67ff
common/base_webserver.yaml: line 1ff
CSS and Javascript causes web page to loop forever
The web site wasn’t loading, to I removed the settings for the local CSS (css_include and css_url) and JS (js_include and js_url) files. Then the web page is displayed.
This might be caused by my ESP8266 instead of an ESP32, but I am wondering why the extra style sheets are required.
The double definition does not cause an error, the last one are always taken.
But it comes down to the version of ESPHome, previous versions had problems with the web server.
With the new version you can do the following:
but I am wondering why the extra style sheets are required.
css_include (Optional, local file): Path to local file to be included in web server index page. Contents of this file will be served as /0.css and used as CSS stylesheet by internal webserver. Useful when building device without internet access, where you want to use built-in AP and webserver.
I’m having the exact same problem. I created a minimal working example with only one global total counter which is increased by 0.1 each time.
Interestingly the initial value will already change when performing a firmware update with the exact same configuration. These are the outputs from two different runs:
Sending state 8434.40039 m³ with 2 decimals of accuracy
Sending state 8434.38965 m³ with 2 decimals of accuracy
Here is my config:
# ----------------------------------------------------------------
# Global variables
# ----------------------------------------------------------------
globals:
- id: gas_totalm3
type: float
restore_value: true
initial_value: '8434.39'
binary_sensor:
- platform: gpio
pin:
number: D4
mode: INPUT_PULLUP ## set ESP32 pin to input pull-up mode
inverted: false
name: ${friendly_name} Gasmeter Z-62 Impuls
id: gassensor_state
device_class: window
filters:
- delayed_off: 10ms
## This automation will be triggered when a button press ends,
## or in other words on the falling edge of the signal.
on_release:
then:
- lambda: |-
ESP_LOGI("main", " ------- Z-62 SET NEW VALUES !!!!!");
id(gas_totalm3) = id(gas_totalm3) + 0.1;
# ----------------------------------------------------------------
# ALL SENSORS
# ----------------------------------------------------------------
sensor:
- platform: template
name: ${friendly_name} Gas total (28820238)
id: gas_total_m3
accuracy_decimals: 2
unit_of_measurement: "m³"
device_class: gas
state_class: total_increasing
lambda: |-
return id(gas_totalm3);
I finally found a solution, after finding this thread:
Instead of using a float value to count, I’m now using two individual int values, which I convert to float afterwards. This way, counting will not be affected by float precision.
Here is my MWE:
# ----------------------------------------------------------------
# Global variables
# ----------------------------------------------------------------
globals:
- id: gas_total_m3_integer_part
type: int
restore_value: yes
initial_value: "8437"
- id: gas_total_m3_fraction_part
type: int
restore_value: yes
initial_value: "22"
binary_sensor:
- platform: gpio
pin:
number: D4
mode: INPUT_PULLUP ## set ESP32 pin to input pull-up mode
inverted: false
name: ${friendly_name} Gasmeter Z-62 Impuls
id: gassensor_state
device_class: window
filters:
- delayed_off: 10ms
## This automation will be triggered when a button press ends,
## or in other words on the falling edge of the signal.
on_release:
then:
- lambda: |-
id(gas_total_m3_fraction_part) += 10; // (Set to impuls_factor * 100, e.g. 0.01 = 1; 0.1 = 10, etc...)
if (id(gas_total_m3_fraction_part) >= 100) {
ESP_LOGD("main", " *** CARRY OVER DETECTED ... compensating...");
id(gas_total_m3_integer_part) +=1;
id(gas_total_m3_fraction_part) -= 100;
}
id(gas_total_m3).update();
# ----------------------------------------------------------------
# ALL SENSORS
# ----------------------------------------------------------------
sensor:
- platform: template
name: ${friendly_name} Gas total
id: gas_total_m3
accuracy_decimals: 2
unit_of_measurement: "m³"
device_class: gas
state_class: total_increasing
lambda: |-
return ( id(gas_total_m3_integer_part) + (id(gas_total_m3_fraction_part)*0.01) );