Utility meter and Esphome

Hello, i am trying to get my water and gas meter readings in home assistant.
I am using a Wemos D1 flashed with Esphome and with currently 1 hall switch connected to get pulses from a magnet on the last dial of the gas meter. The Esphome is connecting to HA with the native api.

Current state : i get the pulses from the dial in pulses/min on the esp8266.

But my problem is that the workings of the “pulse counter” in Esphome and Utility meter in HA are not clear to me.

  1. The pulse counter is giving me a value in pulses/min, but how is this value being calculated ? If i set an update interval of 120s the value in pulses/min that i get will be the average number of pulses/min on the last 120 seconds and will then be reset, and restarted for 120s ?

  2. If correct, to get the consumed m3 of gas i will have to multiply the average p/min i get every 120s by 2 to get the number of pulses in that interval, then multiply the result by the value in m³ per pulse (0,01), is this correct ? At the end it gives : p/min x 0.02 = m³ (used in the last 120s)

  3. If correct, this can be achieved with a sensor filter in the configuration :

sensor:

  • platform: pulse_counter
    pin: D1
    unit_of_measurement: ‘m³’
    name: ‘Gas Consumption’
    update_interval : 120s
    filters:
    • multiply: 0.02
  1. At the end, if everything is correct :slight_smile: , in HA i get a value every 120s from the “Gas Consumption” Sensor of the cosumed m³ for the last 120s. But if i feed this directly into the Utility meter it seems not to sum every value to get a total daily gas usage. Do i need to use a temporary “counter” in HA ?

Thank you very much for your help.

Does this help?

Thank you, I already saw this, but I can’t add the power: option to the pulse counter sensor.

So i don’t seem to be able to use this component. Maybe I don’t understand this right ?

And I already have a reading in a fraction of m3 of gas this is the unit I need at the end.

I tried with a lambda in a template sensor to get a total of pulses but I can’t get it to work.

Perhaps show your yaml file and someone can help.

mmmm. tasty :drooling_face:

1 Like

Bloody auto correct.

Here it is :

sensor:

  - platform: pulse_counter
    pin: D1
    name: "Cubic meters used"
    update_interval : 60s
    filters:
      - multiply: 0.01
    unit_of_measurement: "m³"
    id: gaz
    accuracy_decimals: 3
    icon: 'mdi:fire'
  
  - platform: template
    name: "Total m³"
    lambda: |-
      static float total_value = 0.0;
      total_value += id(gaz).state;
      return total_value;
    unit_of_measurement: "m³"
    icon: 'mdi:fire'
    update_interval: 60s
    accuracy_decimals: 3

The multiplier is because my meter is giving a pulse for every 0,01 m3 of gas.

1 Like

I am not sure, but won’t that initialise total_value to 0.0 on every pass?

I Don’t understant it like that if i refer to the Following example from the Esphome Wiki :

" Tip

To store local variables inside lambdas that retain their value across executions, you can create static variables like so. In this example the variable num_executions is incremented by one each time the lambda is executed and the current value is logged. "

lambda: |-
  static int num_executions = 0;
  ESP_LOGD("main", "I am at execution number %d", num_executions);
  num_executions += 1;

Source : https://esphome.io/guides/automations.html

But this sensor reports NAN in home assistant.

Thank you taking time to help me.

Thanks for that, it was just a guess on my part.

Perhaps add some logging, as per the example !

Finally found a solution by myself.

Added the lambda to the filter section of the pulse counter, to have only one sensor in HA wich is the total m³ consumed, since it is that value that is needed by the utility meter sensor.

So final config if it can help others :

  - platform: pulse_counter
    pin: D1
    name: "Cubic meters used"
    update_interval : 60s
    filters:
      - lambda: |-
        static float total_value = 0.0;
        total_value += x * 0.01;
        return total_value;
    unit_of_measurement: "m³"
    accuracy_decimals: 3
    icon: 'mdi:fire'

I still do not understand completely the working of the pulse_counter in Esphome (when it is reset for example) but it is working now for what i am doing. I think it would be simpler if we could just have a number of pulses and not pulses/min as output.

But if my problems can help others… thank you nickrout for the time you took to help me.

1 Like

Since my last answer, everything is working great for Gas meter.
I am now starting the Electricity meter project. I have an Electricity Meter with a rotating wheel with a black line on it. So with a photo resistor i can count the turns.

i would like to extract two values from these pulses :

  • The Kwh used (Easy as i can use the same method as for the Gas meter, with different factor)
  • The current Watts used (This could be done counting the time between two pulses)

My question is still related to the fact that i don’t fully understand how the pulse counter works in esphome.

  • Can i use two sensor that are using the same gpio as input ? Pulse Width and pulse counter ?
  • If i use pulse width how do i manage the refresh rate ? because if i have the default update time of 60s and the weel takes 70s to make a full turn, what will i get from the sensor at the first update ? and at second update ?
  • Is the sensor data resetting itself when i ask for an update ?
  • Or does the sensor stores the time in seconds between pulses (gpio HIGH) and keeps it until a new pulse is trigered (gpio LOW) ? If it is working like that, if i set an update interval of 10s i should get the same value in seconds from the sensor at every update until a new full rotation has been registered at another speed.

Are you able to share a bit more information on the hardware you used?

I’m interested in adding analog meters to HA as well, but don’t have a lot of experience with the hardware part of building the solution.

Of course, i used a Wemos D1 with a A3214LUA-T Hall Effect Switch, this one is very sensitive and able to sense the magnet on the las wheel of my meter dial.

The sensor has 3 pins, The first is the supply voltage (i uses 3,3v) the midle one is the Ground pin, and on the right one is the signal pin.

I connected the signal pin to one of the GPIO ports of the wemos, and soldered a pull-ip resistance of 10k between +3,3v and the signal pin, also placed 2 capacitors like recommended on the Data sheet of the sensor.

Every time the magnet is in front of the sensor, the signal pin is connected to ground and the GPIO is at 0V (LOW), we have our reading.

1 Like

Do you have any more information on this project? Where exactly do the resistor and capacitors go? And which ones did you use? Apologies for the many questions, I’m learning about this stuff and working on this project currently and this would be massively useful. Thanks

Have you looked at the datasheet of the sensor?

Yes but I’ll be honest and say I don’t fully understand it. I understand I need a capacitor between supply and ground, and a resistor between supply and signal but I can’t see a mention of a second capacitor on the datasheet (unless it’s on the technical drawings which I don’t know how to interpret) or their specifications.

My only experience with capacitors is on guitar wiring so any help would be appreciated.

I see what you mean from the datasheet I found. @nbdriver, can you elaborate?

I am in the process of doing something very similar using a spare esp8266. Could the author of the post @nbdriver or anyone else that already tried it recommend any proper hall switch that works?

I have been looking for information about them (hall sensors and reed switches) but I can’t seem to find any definitive answer for one that “just works” using pulse_counter and esphome and is sensitive enough.

Regards,

This is exactly what I need. I’m using pulses from a rain gauge but I can’t get it to validate.

I have:

  - platform: pulse_counter
    pin: D5
    name: "Rainfall mm"
    update_interval : 10s
    filters:
      - lambda: |-
        static float total_value = 0.0;
        total_value += x * 0.01;
        return total_value;
    unit_of_measurement: "mm"
    accuracy_decimals: 1
    icon: 'mdi:weather-rainy'

It generates this error when validating:

while scanning a simple key
  in "/config/esphome/weather_station_d1.yaml", line 60, column 9:
            static float total_value = 0.0;
            ^
could not find expected ':'
  in "/config/esphome/weather_station_d1.yaml", line 61, column 9:
            total_value += x * 0.01;