Counter doesn't save his value when restart HA

Hi, I need help with counter component. I don’t put inicial value and I put it in the recorder like an entity counter.name to save me the last value. But when I restart HA the value is again 0. Is it an error? It doesn’t work like an input number? Please anybody knows?

Which version of HA are you on?
Have you looked at this?

I see nothing in the docs that says that counter supports persistence.

Maybe put in an automation that when the counter is changed it updates an input number (with initial_state: off), and another at startup that restores the counter from the saved value, and then enables the other automation?

Your best option would be to use MQTT and post the counter in there. It would persist as long as the topic exists.

I’m in version 0.65.5. It looks a bit complicated with how simple it would be to do it like the inputs? Have you tried it? Could it be proposed to be introduced in a next version of HA?

I understand what you tell me, is there an example in the forum that does this? Or do you have some similar code? Anyway, I’ll try and tell you.

Do you have any examples of what you tell me? I still do not have MQTT installed and I do not know exactly how it works.

Thank you all for the interest!

There are some in the docs, and it’d be something like:

alias: 'Update number'
initial_state: 'off'
trigger:
  platform: state
  entity_id: counter.YOUR_COUNTER
action:
  service: input_number.set_value
  data_template:
    entity_id input_number.YOUR_NUMBER
    value: '{{ trigger.to_state }}'

You’d then have to write a startup automation with a template that looped until the counter was incremented enough.

1 Like

I think @Tinkerer’s solution would be easier to implement if you aren’t familiar with mqtt.

Ok, I am going to try this and I’ll say you if it is possible and the final code. Thanks for your answers!!

Well, as I mentioned in my post, you’re going to need another automation triggered at startup, like the examples in here, something like this:

script:
  - alias: 'counter_inc_pause'
    - service: counter.increment
      data:
        entity_id: counter.YOUR_COUNTER
      - service_template: >
          {% if states('counter.YOUR_COUNTER')|default(0)|int < states('input_number.YOUR_NUMBER')|int %}
            script.turn_on
          {% else %}
            script.turn_off
          {% endif %}
        data:
          entity_id: script.counter_inc_pause

  - alias: 'counter_inc_pause' 
    - sequence:
      - delay:
          milliseconds: 1
      - service: script.turn_on
        data:
          entity_id: script.counter_inc

Your automation would call the first script, which would then result in the two automations looping until your counter matches the input number. You’ll also need to define your input_number :wink:

Finally, something needs to turn on that first automation a minute or so after startup - after these scripts have reset your counter.

1 Like

I’m trying to do it but I’m still missing something. One question, why do I have to put the automation in initial_state: off? How and when I have to call the scripts? I lack the two automations that call the scripts, I’ll see tomorrow and I take it out. Thanks again!

You want it off, because otherwise when the scripts start running, it’ll result in your input_number being updated with the value 1 as the first loop happens…

You’d have an automation that runs at startup:

trigger:
  - platform: homeassistant
    event: start

That would then call the first script:

action:
  - service: script.turn_on
    data:
      entity_id: script.counter_inc

Which obviously would then loop with the second until the counter matches the input number.

You’d need a second automation that would also have a trigger of HA starting, have a delay of 5 minutes then turn on the Update number automation

action:
  - delay: '00:05:00'
  - service: homeassistant.turn_on
    entity_id automation.update_number
1 Like

Thank you very much for your contributions @Tinkerer!! Finally, I have done it but in a slightly different way. Then I put the codes in case someone is interested, everything is checked! When the counter is at 0, 1, 2, etc.

# configuration.yaml

counter:
  ameba:
    step: 1
    icon: mdi:counter

input_number:
  contador:
    min: 0
    max: 10
    step: 1

recorder:
  purge_keep_days: 5
  include:
    entities:
      - input_number.contador

# automations.yaml

- alias: 'inicio home assistant'
  trigger:
    platform: homeassistant
    event: start
  condition:
    condition: numeric_state
    entity_id: input_number.contador
    above: 0
  action:
    service: script.turn_on
    entity_id: script.counter_inc


- alias: Contador de usos
  trigger:
    - platform: state
      entity_id: vacuum.ameba
      to: 'on'
  action:
    - service: counter.increment
      entity_id: counter.ameba
    - service: input_number.set_value
      data_template:
        entity_id: input_number.contador
        value: "{{ states.counter.ameba.state }}"

# scripts.yaml

counter_inc:
  sequence:
    - service: counter.increment
      entity_id: counter.ameba
    - service_template: >
        {% if states('counter.ameba')|default(0)|int < states('input_number.contador')|int %}
          script.turn_on
        {% else %}
          script.turn_off
        {% endif %}

      entity_id: script.counter_inc_loop

counter_inc_loop:
  sequence:
    - delay:
        milliseconds: 1
    - service: script.turn_on
      entity_id: script.counter_inc

imagen

In the automation that is doing the count (uses counter), I increase both the counter and the input_number adding to this the value of the counter. In the first automation when starting home assistant, which is the one that calls the scripts, I have incorporated a condition so that it always comes in when the counter is greater than zero. This is so that the counter does not always increase in 1 when the first scripts are executed and it gave problems when its value is 0, as you well said.

Finally, the scripts to perform the loop assign the value of the input_number to the counter. There, I eliminated the words ‘data’ because it works equally and the code is clearer. It is important to add the input_number in the recorder so that it does not lose its value when restarting and also not to give it an initial value.

Thank you again for your help, keep reading and learning! :grinning::hugs:

1 Like