ESPhome pulse counter example with set pulse total needed

I’ve made an energy meter using the ESP home pulse counter. It appears to work when fed pulses on my bench, but I’d like to initialise the kWh total to the same value as on my electricity meter. I’d also like to be able to edit this value from within HA in case I loose counts for some reason. The pulse resetting fragment example given at Pulse Counter Sensor — ESPHome is a bit unclear to me. Specifically the line ‘id: pulse_counter_id’. I don’t know what that refers to and my yaml does not like it. See below in the services section of the code, the error I get is pasted in as a comment.

Thanks for any guidance, I’ve not used ESP home before, so I’m probably missing something obvious. A working example would help a lot.

J.

PS. Apologies if the code formatting is problematic, I’m getting an unformatted code warniing even though I’ve done the 3 backticks thing…

esphome:
  name: esphome-web-xxxxxx

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
# Set pulse counter total from home assistant using this service call:
  services:
    - service: set_pulse_total
      variables:
        new_pulse_total: int
      then:
        - pulse_counter.set_total_pulses:
            id: pulse_counter_id # gives error - Couldn't find ID 'pulse_counter_id'. Please check you have defined an ID with that name in your configuration.
            value: !lambda 'return new_pulse_total;'

ota:

wifi:
  networks:
    - ssid: !secret wifi_ssid
      password: !secret wifi_password
    - ssid: !secret wifi_ssid_rpt
      password: !secret wifi_password
    - ssid: !secret wifi_ssid_rpt2
      password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-xxxxxx"
    password: "xxxxxx"

captive_portal:

switch:
  - platform: gpio
    name: "Output_5"
    pin: 5

binary_sensor:
  - platform: gpio
    name: "Input_12"
    pin:
      number: 12
      inverted: false
      mode:
        input: true
        pullup: true    

sensor:
  - platform: uptime
    name: Smart Meter Uptime
  - platform: wifi_signal
    name: Smart Meter WiFi signal
  - platform: pulse_counter
    pin: 4
    unit_of_measurement: 'kW'
    name: 'Power Meter'
    filters:
      - multiply: 0.06  # (60s/1000 pulses per kWh)
    total:
      unit_of_measurement: 'kWh'
      accuracy_decimals: 2
      icon: mdi:gauge
      name: 'Energy Meter'
      filters:
        - multiply: 0.001  # (1/1000 pulses per kWh)

Presuming its 1000 pulses per kwh, create a sensor from your esp that just measures used kwh from 0. You can the use the utility meter helper to give you a count that matches your meter as you can ‘calibrate it’ with current reading when you want.

I have done this with a frient energy meter pulse counter and i have very accurate readings.

I’ll give it a try Mark, thanks.

If anyone knows how to use the set_pulse_count thing, I’d love to know.

Thanks,

J

Mark,

I tried the utility meter helper (which I did not know about before you mentioned it) & it looks like it will do the job in this case. Many thanks!

I’d like to setup a text input to do this setup of the initial reading (without using the developer tools - services tab. I guess thats possible?

J

Id say yes using an automation that calls the service when the state of the input number changes and passes the value of it. There maybe other ways but not aware of any.

The HA utility meter suggested works for me too. I use the manual config on utility meter, not the helper. However to answer the question from jmcc:

I think the ESPHome doc needed to make clear what id was being reset. The docs mention a generic “pulse_counter_id”. It ought to be an ID you need to attach to your sensor. In my case below that ID is ‘myelectricity’. Here’s what I did and what works to reset the total.

ELECTRICITY METER
The following code reads the red flashes from the electricity meter. It produces two entities one that measures the electric power in kw, the other gives energy in kWh which is more useful. The code appears to add a service to HA - scroll down for how to test it. I’m scratching in the dark myself so please share any improvements you make.

sensor:
  - platform: pulse_counter
    pin: GPIO12
    unit_of_measurement: 'kW'
    device_class: "power"
    state_class: "measurement"
    update_interval: 20s
    accuracy_decimals: 2
    name: 'elect-power'
    id: myelectricity
    filters:
    - multiply: 0.060
    - sliding_window_moving_average:
        window_size: 5
        send_every: 5
    total:
      accuracy_decimals: 3
      unit_of_measurement: 'kWh'
      device_class: "energy"
      name: 'elect-energy'
      filters:
        - multiply: 0.001  # (1/1000 pulses per kWh)    
api:
  password: xxx
  services:
    - service: set_pulse_total
      variables:
        new_pulse_total: int
      then:
        - pulse_counter.set_total_pulses:
            id: myelectricity
            value: !lambda 'return new_pulse_total;'

TO TEST THE RESET FEATURE
Go to Developer Tools > Services > ESPHome: meter_set_pulse_total > Enter a value for new_pulse_total (eg 0 ).

TO USE THE RESET FEATURE
Make an automation that say, Triggers at midnight and as an Action Calls the service: ESPHome: meter_set_pulse_total and enter the value (eg 0)

Roger F
automation.rogerfrost.com

1 Like

How can i make the “total” decrement by 0.001 on every pulse?

Set Value failed

Home Assissistant
ESPHome: erdgas_set_pulse_total
new_pulse_total 5000

ESPHome

api:
  password: "xxxxxx"
  services:
    - service: set_pulse_total
      variables:
        new_pulse_total: int
      then:
        - pulse_counter.set_total_pulses:
            id: gascount_id
            value: !lambda 'return new_pulse_total;'


sensor:
- platform: pulse_counter
    pin: D5
    unit_of_measurement: "m3"
    name: "Gaszähler"
    id: gascount_id
    filters:
      - multiply: 0.01

setting Value will not work -why?

Hi there, could you please explain how you were able to use the Utility Meter helper?
I created one and called it pulse_counter_id however that still gives the error

  • Couldn’t find ID ‘pulse_counter_id’. Please check you have defined an ID with that name in your configuration.
    Thanks in advance.