Combine the pulse meter sensor with the Analog Threshold Binary Sensor

Hey guys,
I‘m currently reading my gas meter with a QMC5883L magnetometer. So far so good.
For that scenario I was thinking about combining the pulse meter sensor with the Analog Threshold Binary Sensor. So the threshold of the magnetometer could come up with a boolean value each time the magnetometer exceeds a certain threshold. Now how could I geht that pulse to the pulse meter sensor without bridging pins?

A pulse meter is binary; it already creates two states. There is no “threshold” to set.

If you wanted to do something for each pulse then a Template Binary Sensor would allow you to create an additional output in what every from you wish.

To answer your question The Analog Threshold Binary Sensor requires you to set the sensor_id (the pulse meter id) that you have already created.

  • sensor_id (Required, ID): The ID of the source sensor.

Hello, I think that you do not need the pulse sensor to achieve what you want.
I solved this as follows:

  # Project Name, upper/lowercase characters, digits and underscores.
  project_name: "laundry-sensor"
  project_id: 'sensor_laundry'
  # Set calibration_off to 'false' in order to send magnetometer raw data to HA. This makes the calibration easy.
  # Once the calibration is ok (no pulses lost) then it can be set to 'true'.
  calibration_off: 'false'
  threshold_upper: '400'
  threshold_lower: '200'
  # which sensor to use for pulse detection
  magnetometer: "${project_id}_hmc5883lz"

  # Lower the default flash write interval in order to avoid flash wear out as global variables get written to flash every ${flash_write_interval}
  flash_write_interval: 30min

  # By calling this service from HA you can set the value of the counter according to the reading of your meter.
  # HA - Developer Tools - Services - ESPHome: ${project_id}_set_gas_counter_total - set value with decimal point
    - service: set_gas_counter_total
        counter_actual: float
        - globals.set:
            id: counter_total
            value: !lambda 'return counter_actual;'
        - logger.log:
            format: "Service set_gas_counter_total called from HA: counter_actual (esp:counter_total) has been set to: %.2f"
            args: [id(counter_total)] 

   - id: counter_total
     type: double
     restore_value: yes

# Magnetometer
  - platform: hmc5883l
    address: 0x1e
# axis X  
      name: "${friendly_name} - Magnetic field x"
      id: ${project_id}_hmc5883lx
      accuracy_decimals: 0
      internal: ${calibration_off}
# axis Y    
      name: "${friendly_name} - Magnetic field y"
      id: ${project_id}_hmc5883ly
      accuracy_decimals: 0
      internal: ${calibration_off}
# axis Z    
      name: "${friendly_name} - Magnetic field z"
      id: ${project_id}_hmc5883lz
      accuracy_decimals: 0
      internal: ${calibration_off}
    oversampling: 8
    range: 810uT
    update_interval: 1s
# Gas total
  - platform: template
    name: "${friendly_name} - Gas total m3"
    id: "${project_id}_gas_total"
    lambda: |-
      return id(counter_total);
    update_interval: 15s
    unit_of_measurement: "m³"
    accuracy_decimals: 2
    icon: 'mdi:fire'
    device_class: gas
    state_class: total_increasing

# Generate pulses representing the rotation of the magnet
# This sensor converts the analog value of the given sensor value to a binary signal using hysteresis. The binary sensor will:
#  - turn to 'off' if the value drops below threshold/low 
#  - turn to 'on' if the value raises above threshold/high
# Every off -> on transition will trigger on_press, which increments the value of 'counter_total'
# Set the threshold below
  - platform: analog_threshold
    name: "${friendly_name} - Gas magnet"
    sensor_id: ${magnetometer}
      upper: ${threshold_upper}
      lower: ${threshold_lower}
        - lambda: |-
            id(counter_total) += 0.01;

hey @ZsZs73 ,
thank you for sharing you solution! The sensor I’m using isn’t binary so I had to wiggle around with templates as @JulianDH mentioned.

I also do not use a binary sensor (like a reed) to detect the rotation of the magnet, but the very same QMC5883L magnetometer like you.
The following component

  - platform: analog_threshold

in my code above is the one which converts the analog value read from the magnetometer (${project_id}_hmc5883lz) to binary values with some histeresys and icreases the value of the previously defined counter_total global variable via the on_press: trigger.

I actually went over to using AI-on-the-edge-device because it is way more solid. I’ve lost intervals over the time due to restarts or other reasons.