Gate opening counter meter -SOLVED

Good morning everyone, I’m trying to make a counter of the openings of the house gate. I’ll try to describe below what I realised to know where I’m wrong in your opinion.

I connected the contact of the button of my intercom to a relay driven by a NODEMCU (on which ESPEasy runs). via MQTT I operate the contact from HOMEASSISTANT. The gate is programmed that when the contact closes it opens and closes after 40 seconds alone. If I close the contact of the relay with the gate at the opening, the gate continues to open, while if the gate is closing it reopens. Now I would like to add a metre that indicates the number of gate openings.

This is the switch code to open the gate

- platform: mqtt
  name: "ESP-CITOFONO-CARRAIO RESIDENCE"
  qos: 1 
  state_topic: "/ESP-CITOFONO/Switch4"
  command_topic: "/ESP-CITOFONO/Switch4/gpio/15/cmd"
  payload_on: "Pulse,15,0,500"
  payload_off: "Pulse,15,0,500"
  #optimistic: false
type or paste code here

While this is the sensor that should count the openings

- platform: history_stats
  name: CARRAIO number
  entity_id: switch.esp_citofono_carraio_residence
  state: "on"
  type: count
  start: "{{ 0 }}"
  end: "{{ now() }}"

Of course, the metre on the web interface does not increase, I guess it has to create some link between the switch and the sensor but I don’t sincerely know how.

As always, I trust some advice from the community.

Thank you

Ah, now I see you have created an MQTT switch, the entity_id should be right.

What states does switch.esp_citofono_carraio_residence show? I made a history_stats sensor like yours based on an input boolean that works:

  - platform: history_stats
    name: Testertje Counter
    entity_id: input_boolean.testertje
    state: "on"
    type: count
    start: "{{ 0 }}"
    end: "{{ now() }}"

Are you storing history for the switch using recorder? History_stats requires history(from the recorder integration) to be stored for that device. By default, home assistant stores a history for everything unless you told it not to. I also noticed you’re using a start of 0. One thing you should keep in mind, this will only count what’s in your history. By default, history only keeps a rolling 10 days of information. Therefore, the count will only include 10 days of information.

Given those ifs, maybe check out this thread: Count for number of times Automations triggered - #4 by 123

It provides a solution to count the times an automation has run. If you track the automation that fires when your gate opens you have a more robust solution.

Hi, I checked, I have this setting:

recorder:
  #purge_interval: 1
  purge_keep_days: 15
  commit_interval: 60
  exclude:
    domains:
    #- sensor
    - light
    - automation
  include:
    entities:
    - sensor.carraio_number
    - sensor.testertje_counter

But I still don’t see the data of the last 15 days but 0 as if the code to increase it doesn’t work

it doesn’t magically add 15 days worth of data. It purges after it receives 15 days of data. So if you just turned this on for sensors, you have to wait until the data is collected.

Ok, however, I also activated the automation indicated in the post you recommended to me https://community.home-assistant.io/t/count-for-number-of-times-automations-triggered/308240
Let’s see which of the two gives better results

I didn’t recommend the automation. I was simply pointing out why you weren’t getting history data for your sensor.