How can I reset repeat.index to 0?

Hi All!

I would add some anti-rattle in my automation, but how can I reset anti-rattle timeout?

alias: CarCharge
description: ""
triggers:
  - trigger: state
    entity_id:
      - switch.s_8_avtomobilnoe_zariadnoe_ustroistvo
    from: "off"
    to: "on"
  - trigger: template
    value_template: >-
      {{ ( now().timestamp() -
      as_timestamp(state_attr('automation.carcharge','last_triggered')) ) >
      259200 }}
conditions: []
actions:
  - type: turn_on
    device_id: 2b37ebd4166a01aefa38555777834ce9
    entity_id: 50ec59ebdac6e04bc4196a5ebd99ca74
    domain: switch
  - repeat:
      sequence:
        - delay:
            hours: 0
            minutes: 0
            seconds: 10
            milliseconds: 0
        - if:
            - condition: template
              value_template: >-
                {{ states('sensor.s_8_avtomobilnoe_zariadnoe_ustroistvo_power')
                | float(0) > 0 }}
          then: [HERE I NEED TO RESET repeat.index to 0]
      while:
        - condition: template
          value_template: "{{ repeat.index <= 10 }}"
    enabled: true
  - type: turn_off
    device_id: 2b37ebd4166a01aefa38555777834ce9
    entity_id: 50ec59ebdac6e04bc4196a5ebd99ca74
    domain: switch
mode: restart

AFAIK, you can’t reset the index… but if you did reset it, the delay would just repeat indefinitely until the power sensor was equal to 0… is that the actual goal?

If it is, you would be better off with a Wait for template or Wait for trigger. But even better than that would be to get rid of the waiting altogether. Either add a trigger for turning the switch off and use branched logic (If/Then or Choose actions) or set up a second automation to handle turning the switch off.

1 Like

Yes, if sensor > 0, I stay inside the cycle. If sensor == 0, I do 10 cycles and exit. Each non-zero sensors resets counter to 0.

If I will wait condition of template [sensor == 0], first true pulls me out, but I need several trues to do this.

Maybe I can do this another way…

It looks like this automation might be running for hours at a time, right? That would not be a very good idea. Automations aren’t really intended for that (e.g. you shouldn’t restart HA while this is running). I have a suggestion, but would need confirmation first.

Yes, but this is not a very big problem in this case. I can do this using two different automations, but I would like to know how can I do this very standard algorythm. If I code something with assembler, I always do anti-rattle this style

Do what you would do in assembler then: a global variable that’s your index instead. In HA world, it will be a helper.

1 Like

Your automation turns on a switch, idles in a repeat until sensor.s_8_avtomobilnoe_zariadnoe_ustroistvo_power is zero, then turns off the switch.

Why not simply add another trigger, that detects when the sensor is zero, and use that event to turn off the switch?

alias: CarCharge
description: ""
triggers:
  - id: 'on'
    trigger: state
    entity_id:
      - switch.s_8_avtomobilnoe_zariadnoe_ustroistvo
    from: "off"
    to: "on"
  - id: 'on'
    trigger: template
    value_template: >-
      {{ now() - state_attr('automation.carcharge', 'last_triggered') > timedelta(hours=72) }}
  - id: 'off'
    trigger: template 
    value_template: >
      {{ states('sensor.s_8_avtomobilnoe_zariadnoe_ustroistvo_power') | int(0) == 0 }}
conditions: []
actions:
  - action: "switch.turn_{{ trigger.id }}"
    target:
      entity_id: switch.your_switch 
mode: single

Yes, I will try

Because first zero is not end of charging algorythm. See real power consumption graph

How many times should the sensor report zero before the switch is turned off?

Your example seems to suggest ten times.

while:
  - condition: template
    value_template: "{{ repeat.index <= 10 }}"

EDIT

The alternative is to detect when the sensor’s value remains zero for at least 10 minutes (or whatever length of time is best).

- id: 'off'
  trigger: template
  value_template: >
    {{ states('sensor.s_8_avtomobilnoe_zariadnoe_ustroistvo_power') | int(0) == 0 }}
  for:
    minutes: 10
1 Like

Тhis looks like what i was looking for :+1: I didn’t know that I can put timeout here. Thank you!

1 Like