How to measure pulse counter when switch is ON

Hello, how to write code to measure pulses on a geiger counter using esphome.
the power supply of the Geiger counter is connected to the ON/OFF switch.
This means that when I turn on the geiger for 1 minute, I want to know the number of cpm after one minute.

And then the power goes off again for 10 minutes.
Can you help me how to write the code for pulse_counter?
well thank you

There is counter helper already provided in HA, so no need to write some code, but I think that reading will not be accurate in this way. Geiger counter need some time to ‘warm up’ after powering on and this time will affect the result. Maybe is better to leave counter povered and write automation which resets counter helper to zero, waits minute, and ‘publish’ value after the minute.

Yes, I know he needs to warm up. That’s how I set it up.

  • Power on
  • wait 1 min.
  • measure the start
  • wait 1 min.
  • publish value
  • power off

But here is the problem: the pulse counter runs constantly. This means that it resets every 1 minute, but we don’t know when.

- platform: pulse_counter
    pin: GPIO13
    id: geigercounter
    name: "${friendly_name} Radiation Count"
    unit_of_measurement: 'CPM'
    accuracy_decimals: 0
    count_mode:
      rising_edge: DISABLE
      falling_edge: INCREMENT
    icon: mdi:radioactive
    *update_interval: 60s* <------

And I would need to reset the pushle counter right at the time of starting the measurement.
Something along the lines of resetting the total counter

    on_press:
      then:
        - pulse_counter.set_total_pulses:
            id: geigercounter
            value: "0"

How to write this?

I think it will help me to set the pulse counter update interval to 1 second
And reset the total count every time you start measuring
and publish the value from the total count after one minute of measurement

FINAL:

sensor:
 # CPM
  - platform: pulse_counter
    pin: "$geiger_pin"
    id: geigercounter
    internal: true
    #name: "${friendly_name} Radiation Count"
    #unit_of_measurement: 'CPM'
    #accuracy_decimals: 0
    count_mode:
      rising_edge: DISABLE
      falling_edge: INCREMENT
    icon: mdi:radioactive
    update_interval: 1s
    filters:
      - lambda: return x * 1 / 60; 
    # total one minute cpm
    total:
      unit_of_measurement: 'CPM'
      id: geigercounter_total_cpm
      internal: True
            
  # CPM - template 
  # only send measure time
  - platform: template
    name: "${friendly_name} Radiation Count"
    id: radiation_cpm
    icon: mdi:radioactive
    unit_of_measurement: 'CPM'
    accuracy_decimals: 0
    update_interval: never
    lambda: "return (id (geigercounter_total_cpm).state);"
    filters:
      filter_out: nan
    on_value:
      then:
        - sensor.template.publish:
            id: radiation_power
            state: !lambda "return (id(radiation_cpm).state * 0.005966);"

  # µSv/h
  - platform: template
    name: "${friendly_name} Radiation Power"
    id: radiation_power
    icon: mdi:radioactive
    unit_of_measurement: 'µSv/h'
    accuracy_decimals: 3
    filters:
      filter_out: nan

switch:
  - platform: gpio
    name: "${friendly_name} Measure"
    icon: mdi:air-filter
    restore_mode: ALWAYS_OFF
    pin:
      number: "$reducer_consumption_pin"
    id: switch_reducer_consumption
    on_turn_on:
      - then:
          - delay: 60s
          - pulse_counter.set_total_pulses:
              id: geigercounter
              value: "0"
          - delay: 60s
          - component.update: radiation_cpm
          - delay: 1s
          - pulse_counter.set_total_pulses:
              id: geigercounter
              value: "0"
          - delay: 500ms
          - switch.turn_off: switch_reducer_consumption
          - logger.log: 
              format: "MEASURE COMPLETE"
              level: warn
    entity_category: config