Time between pulses from adc sensor

I have a Hall sensor mounted on my gasmeter. Due to the week signal, it doesn’t work as a switch. The analog signal though is strong enough to consistantly detect the pulses.
It works just fine for the pulse count, but I also would like to calculate the heating power. Pulse meter only works on a digital pin, so I tried to determine the interval between on-on and on-off.
I came up with the code below, but I fail to get the states from the timers to publish.
Can anyone put me on the right track?

Thanks in advance,

esphome:
  name: d1minilolin-01
  friendly_name: D1MiniLOLIN-01

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "MeItImNVFSKIxMucF3h2hCw2eUJxR7gwXWCX0z4f5Ow="

ota:
  password: "f3605d46be86c79d06ca0ff3cf767bde"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "D1Minilolin-01 Fallback Hotspot"
    password: "KoLWfxrgkzkK"

captive_portal:

  - platform: adc
    pin: A0
    id: gasmeter_hall_measurement
    internal: true 
    update_interval: 100ms
    raw: true 
    name: "Gasmeter Hall measurement"
    filters:
    - lambda: return 1024-x; 
    - delta: 2.0

  - platform: uptime
    name: "Time Since Last On"
    id: time_since_last_on
    internal: True

  - platform: template
    name: "gasmeterpulsetijd on-on"
    id: gasmeterpulsetijd_on_on
    device_class: duration
    unit_of_measurement: "s"
    state_class: "measurement"
    icon: "mdi:clock-fast"
    accuracy_decimals: 2
    
  - platform: template
    name: "gasmeterpulsetijd on-off"
    id: gasmeterpulsetijd_on_off
    device_class: duration
    unit_of_measurement: "s"
    state_class: "measurement"
    icon: "mdi:clock-fast"
    accuracy_decimals: 3

binary_sensor:
  - platform: analog_threshold
    name: "Gasmeterteller 10liter"
    sensor_id: gasmeter_hall_measurement
    threshold:
      upper: 476 #was 480
      lower: 467 #was 470
    on_press:
      then:
        - lambda: |-
            if (id(starttijdpulse) > 0) {
              id(pulsduur_onon) = id(time_since_last_on).state - id(starttijdpulse);
              id(starttijdpulse) = id(time_since_last_on).state;
              id(gasmeterpulsetijd_on_on).publish_state(id(pulsduur_onon)/1000);
            }
    on_release:
      then:
        - lambda: |-
            if (id(starttijdpulse) > 0) {
              id(pulsduur_onoff) = id(time_since_last_on).state - id(starttijdpulse);
              id(gasmeterpulsetijd_on_off).publish_state(id(pulsduur_onoff)/1000);
            }
    

globals:
  - id: total_pulses
    type: int
    initial_value: '920' 

  - id: starttijdpulse
    type: long
    initial_value: '0'

  - id: pulsduur_onon
    type: long
    initial_value: '0'

  - id: pulsduur_onoff
    type: long
    initial_value: '0'

Is this ever greater than zero??

You are right!
I removed the if statement, but it doesn’t change anything.

Well for starters this won’t work. id(pulsduur_onon) refers to the sensor object, not the value of it.

You would need:

id(gasmeterpulsetijd_on_on).publish_state(id(pulsduur_onon).state/1000);

You may have other errors, this is just the one I noticed.

I changed the code to:

    on_press:
      then:
        - lambda: |-
            id(pulsduur_onon) = id(time_since_last_on).state - id(starttijdpulse);
            id(starttijdpulse) = id(time_since_last_on).state;
            id(gasmeterpulsetijd_on_on).publish_state(id(pulsduur_onon).state);
    on_release:
      then:
        - lambda: |-
            id(pulsduur_onoff) = id(time_since_last_on).state - id(starttijdpulse);
            id(gasmeterpulsetijd_on_off).publish_state(id(pulsduur_onoff).state);

I get the following error when compiling:

INFO ESPHome 2023.9.3
INFO Reading configuration /config/esphome/d1minilolin-01.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing d1minilolin-01 (board: esp01_1m; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 1MB Flash
Dependency Graph
|-- ESPAsyncTCP-esphome @ 2.0.0
|-- ESPAsyncWebServer-esphome @ 3.1.0
|-- DNSServer @ 1.1.1
|-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|-- noise-c @ 0.1.4
Compiling .pioenvs/d1minilolin-01/src/main.cpp.o
/config/esphome/d1minilolin-01.yaml: In lambda function:
/config/esphome/d1minilolin-01.yaml:107:69: error: request for member 'state' in 'pulsduur_onon->esphome::globals::GlobalsComponent<int>::value()', which is of non-class type 'int'
  107 |             id(gasmeterpulsetijd_on_on).publish_state(id(pulsduur_onon).state);
      |                                                                     ^~~~~
/config/esphome/d1minilolin-01.yaml: In lambda function:
/config/esphome/d1minilolin-01.yaml:112:71: error: request for member 'state' in 'pulsduur_onoff->esphome::globals::GlobalsComponent<int>::value()', which is of non-class type 'int'
  112 |             id(gasmeterpulsetijd_on_off).publish_state(id(pulsduur_onoff).state);
      |                                                                       ^~~~~
*** [.pioenvs/d1minilolin-01/src/main.cpp.o] Error 1
========================== [FAILED] Took 7.58 seconds ==========================

What am I doing wrong?
THX for any help.

Apologies - I missed that was a global variable - so it should have work as-is. :flushed:

Sorry - can’t spot anything wrong.

The problem seems to be related to data types. I tried to change the globals to float and integer, resulting in variations on the above error codes.

Solution: