Count switch works time and count switch turn_on per 1h/15 mins etc

Hello friends! I"m traying to count switch works time and count switch turn_on per 1h/15 mins etc. But nothing happen with sensor. How to properly do it?

switch:
  - platform: gpio
    pin:
      number: GPIO4
      allow_other_uses: true 
    id: switch_power_relay
    name: switch_power_relay
    #inverted: false 
    restore_mode: ALWAYS_OFF 
############################################################
sensor:
  - platform: pulse_counter
    pin:
      number: GPIO4
      allow_other_uses: true
      mode:
        input: true
        pulldown: true
    name: "Switch Count Meter1"
    update_interval : 60s

  - platform: pulse_counter
    pin:
      number: GPIO4
      allow_other_uses: true
    name: "Switch Count Meter2"
    id: switch_count_meter
    unit_of_measurement: "times/min"
    update_interval : 60s
    icon: "mdi:water"
    total:
      name: "Switch Count Meter"
      unit_of_measurement: "minutes"

This component might suite better for your case:

1 Like

Thanks Karosm
But one problem. Can not count power_on times per hour, it changes to zero after 1 minutes.

  - platform: switch
    name: "Relay works status sensor"
    id: relay_works_status_sensor
    source_id: switch_power_relay
    device_class: running 

sensor:
  - platform: duty_time
    id: relay_work_duty_time
    name: Relay_work_duty_time
    # Support logical sources (optional): 'binary_sensor'
    sensor: relay_works_status_sensor
    restore: True
    # Sensor for last turn-on time (optional)
    last_time:
      name: Relay_work_duty_time Last Hour Turn-On Time
    on_value:
      then:                             # Публикуем полученные данные в программные датчики
        - sensor.template.publish:      
            id: count_of_pump_starts
            #state: !lambda "return id(count_of_pump_starts).state + 1;"
            state: !lambda "return id(count_of_pump_starts).state + 1.0;"
    update_interval: 60s


  - platform: template
    name: "Сount of pump starts per hour"
    id: count_of_pump_starts
    unit_of_measurement: "times"
    accuracy_decimals: 0 
    lambda: "return 0.0;"

interval:
  - interval: 60min  # 1 hour 
    then:
      - sensor.duty_time.reset: relay_work_duty_time
      - sensor.template.publish:
          id: count_of_pump_starts
          state: "0.0"

What’s the point of this…

1 Like

I changed lambda: “return 0.0;” to “return 0;”
Both variant counting pump starts per hour works properly for 1 minutes.
When I toggle the switch manually the sensor showing 1-2-3-4 etc
But after 1 min it is resetting to 0.
I have no idea what happend…

I don’t completely understand what are you after with that lambda.
Anyway, template sensor has default update interval of 60s, so it returns zero like you asked…
You can set interval to never or one hour …

1 Like

Hmm… I thought the sensor will update the state after interval settings( 60 mins)

You don’t even need that interval template.publish if you set the template update interval to 60min.
Or remove the return zero lambda.
Or set the update interval to : never.

Too many options :sweat_smile:

1 Like

Karosm
Too many thanks, all works like a charm