Why does this automation sometimes fail?

- id: vedlikeholdslading
  alias: Automatisk vedlikeholdslading
  trigger:
    - platform: numeric_state
      entity_id: sensor.ladeniva
      below: 78
    - platform: numeric_state
      entity_id: sensor.ladeniva
      above: 80
  action:
    service_template: >-
      {% if states("sensor.ladeniva") | float < 79 %}
        switch.turn_on
      {% elif states("sensor.ladeniva") | float > 79 %}
        switch.turn_off
      {% endif %}
    entity_id: switch.osram_plug_01_000d1a23_3

The reason I want this in one automation is that I want to be able to turn ut off sometimes. But that aside, the rule works fine. When “ladeniva” (chargelevel) falls below 78 the charger is turned on (switch.osram). And when it is above 80 it stops/turns off.

Most of the time this works but sometimes it doesnt. Why it fails I am not sure. In the logs there are no clues, but maybe I could log something special? Not sure if the automation triggers but the switch fails or similar. What is baffling is that even if the ladenivĂĄ keeps dropping the automation seems to never fire again. I would expect it to fire another time when it goes lower (like 77 or 76) but that never happens. One time I saw it pass the 80 aswell when charging and not stop. Again same story, just continued to charge until 100 without ever trigging again. Could I force it to check every time the ladeniva sensor gets a new value?

Any tips? Right now I am trying to add more triggers on 77, 76 and 75. Just to double safeguard, but that is more like a workaround…

Each trigger will only fire once for the specified condition. So if it hits 77, it’ll fire. Then at 76, it doesn’t fire again. That particular trigger won’t fire again until it becomes not true, and then true again… i.e… it hits 77, it fires, then 76, 75, 74… then 79, which makes the trigger condition no longer true… so when it hits 77 again, it fires again.

Because you have a service_template, you could use this instead so that it would trigger every time the state changed:

  alias: Automatisk vedlikeholdslading
  trigger:
    - platform: state
      entity_id: sensor.ladeniva
  action:
    - condition: template
      value_template: "{{ states('sensor.ladeniva') | float != 79 }}"
    - service_template: >-
        {% if states("sensor.ladeniva") | float < 79 %}
          switch.turn_on
        {% elif states("sensor.ladeniva") | float > 79 %}
          switch.turn_off
        {% endif %}
    entity_id: switch.osram_plug_01_000d1a23_3
2 Likes

That is a grand idea actually. Not sure why it does not fire again like you state, is there a way to reset the trigger? I sometimes see for example that a sunset trigger fails, but I would like HA to see that the sun is set, my porch lights are off and then get them on :slight_smile: Same applies there, if the trigger fails, or if I rebooted HA at that exact moment the trigger fails and grinds to a halt.

Thank you! I will try the suggested asap!

Other than a restart (which can bite you on the ass when using “for” in a state trigger), I don’t know of any other way to reset triggers.