Hi Guys,
I do hope this is the correct part of the forum to post this. Mods, do feel free to move it if it is incorrect.
I’ve got to a point in this little project where I am struggling to make further progress, mainly due to my lack of knowledge of YAML, and probably being a bit thick too!
Background
I have quite an old gas meter (made in 1991 according to the date on it) installed in my house. I have tried to get an upgrade to a ‘smart’ one, but the installation people have been four times, looked at it, and have done a very quick about-turn as it is in an awkward location, in my basement and I live in a rural area that has poor mobile reception, so it is unlikely that a smart meter would work properly (I have been told)
The meter is measuring in Ft3 with the dial to the right being 1 ft3 of gas consumed per revolution.
Sensor Setup
I have got an ESP32 reading the passage of the needle via a TCRT5000 IR sensor. This sensor provides both analog and digital outputs. The digital output is not used as the adjustment of it isn’t sensitive enough to record the needle traveling underneath, however, the analog output from the sensor does log a rise in voltage as the needle sweeps below it.
My Gas Appliance
I’m running a Rayburn range off this gas meter. It is a boiler for central heating and hot water, and also a gas cooker. It uses two separate burners to achieve this. The Range cooker was installed when I built the house 30 years ago, and it uses a pilot light system instead of the more modern instantaneous piezo ignition. The pilot lights’ on it consume about 1 ft3 of gas per hour, with the needle on the meter moving at a very much slower rate than it does when the burners are lit. This can be seen here…
This can be contrasted with the sharp spikes when one of the burners is lit in the example below. On this the water was programmed to come on between 6 am and 7 am.
This is a similar type of appliance to the one I have in my kitchen…
Home Assistant & ESP32 Setup
Whilst researching how to achieve this, I came across a fantastic, informative, and very helpful blog by Xavier Decuyper @Savjee on how he achieved his own analog gas meter monitoring, although his gas meter is a different style, and he ultimately used a digital output from the sensor as he adapted his code to use the digital reading.
By using some of Xavier’s YAML files from his blog I have the ESP32 working, and having tweaked the various figures, I have the IR sensor reading as can see from the images posted above. It is quite consistent too. The Energy part of HA is also working, and showing gas consumption, but unfortunately, only logs gas consumption when the ESP32 is restarted after an OTA YAML file update.
For reference, this is my YAML file…
globals:
- id: total_pulses
type: int
restore_value: false
initial_value: '0'
- id: last_pulse_value
type: int
restore_value: false
initial_value: '0'
# The following sensor puts the 'total_pulses' variable into Home Assistant Energy
# Monitor and tells HA it is to ft³
sensor:
- platform: template
name: "Gas used"
device_class: gas
unit_of_measurement: "ft³"
state_class: "total_increasing"
icon: "mdi:fire"
accuracy_decimals: 0
lambda: |-
return id(total_pulses);
# The following is to sense when a new voltage measurement is received,
# then calculate the difference with the previous voltage. If it's larger than
# a certain threshold, increase the pulse counter. This is to log
# when the needle has peaked when it has swept past the sensor, as the
# diff value will then be negative when the voltage falls as the needle moves
# away. The threshold is set at the 0.0035 value which is the last value I tried.
# This figure has been higher and lower and nothing seems to work
on_value:
then:
- lambda: |-
float last_reading = id(last_pulse_value);
float current_reading = id(adc_value).state;
float diff = last_reading - current_reading;
if(diff > 0.0035){
id(total_pulses) += 1;
}
id(last_pulse_value) = current_reading;
# This is the reading of the voltage on the analog input pin. It takes a
# measurement every 333ms, but only reports the average value every two seconds.
# This is to smooth the signal going to the ADC on the ESP32. The value is
# shown in HA so it is possible to confirm that the sensor is 'seeing' the rotating needle
- platform: adc
id: adc_value
pin: GPIO32
accuracy_decimals: 4
name: "Gas Meter Sensor Analog Value"
update_interval: 333ms
attenuation: 11dB
filters:
- throttle_average: 2sec
# Uptime sensor
- platform: uptime
name: Gas Meter Uptime
# WiFi Signal sensor
- platform: wifi_signal
name: Gas Meter WiFi Signal
update_interval: 60s
# Switch to restart
switch:
- platform: restart
name: gas meter monitor restart
I have tried all sorts of different values for the averaging of the signal, and for the threshold to trigger an increase in the ‘total_pulses’ value, and none of it seems to be sufficient to increase the value of the ‘total_pulses’ figure, which is reported back to HA
From checking the logs of the ESP32 device via ESPHome, the peak value for the voltage is consistent at about 0.183 > 0.185 V and as the needle passes, this reduces down to the ‘steady’ state, or background reading, which is then consistent between 0.171 > 0.175… this will then rise to the peak voltage value again as the needle passes under the sensor head… and so on. The voltage value difference is greater when the burner is lit as the needle is moving faster in relation to the sampling time.
As I stated at the beginning my knowledge of yaml is poor.
- Is this method sufficient for triggering an increase in the total_pulses value??
- Is there another method that can be used to register the higher sensor values, and thus trigger the total pulses counter??
Any advice or guidance would be gratefully appreciated.
Thanking you in advance for any responses received.
Mike.