Automation not stopping when it should

I have an automation that I have configured that turns on my immersion heather (switch) at 6.40am if my battery is above 40% and it simultaneously switches my hive hot water off.
It should… then stay on for 45mins unless the battery drop below 15%… at which point it should switch off and flick the boiler back on to schedule (eco) mode… but it doesn’t do this and just hammers the battery.

The timer aspect works, its just the turning off when th battery drops low that doesn’t seems to work.

alias: Auto Hot Water - Mon to Fri
description: ""
trigger:
  - platform: time
    at: "06:40:00"
condition:
  - condition: numeric_state
    entity_id: sensor.solarsystem_battery_capacity
    above: 40
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    after: "00:00:00"
action:
  - parallel:
      - type: turn_on
        device_id: idididid
        entity_id: light.immersion_switch
        domain: light
      - device_id: idididiid
        domain: water_heater
        entity_id: water_heater.downstairs
        type: turn_off
  - if:
      - condition: or
        conditions:
          - condition: numeric_state
            entity_id: sensor.solarsystem_battery_capacity
            below: 15
          - condition: numeric_state
            entity_id: sensor.home_instant_electricity
            above: 1000
    then:
      - service: water_heater.set_operation_mode
        data:
          operation_mode: eco
        target:
          entity_id: water_heater.downstairs
      - type: turn_off
        device_id: idididid
        entity_id: light.immersion_switch
        domain: light
    else:
      - delay:
          hours: 0
          minutes: 45
          seconds: 0
          milliseconds: 0
      - service: water_heater.set_operation_mode
        data:
          operation_mode: eco
        target:
          entity_id: water_heater.downstairs
      - type: turn_off
        device_id: idididid
        entity_id: light.immersion_switch
        domain: light
mode: single

Ithe automation triggers at 6:45.
That is the only time the automation will run and the associated actions may be triggered.

You should likely make the battery % part of trigger. When battery is below 15% trigger. the automation will run and should give desired results

Your current automation triggers at a set time, it then check the battery is above 40% and if so it then switches immersion and water heater. A few thousandths of a second later you then have a if statement that says IF battery below 15 - it won’t ever be as the automation has literally just checked it’s above 40.

Assuming you are happy with having the automation run for 45 mins with all the possible pitfalls of doing this (restarting home assistant, reloading automations etc) that can cause the automation to cease running.

Then I would remove the IF statement, I would then replace the 45 min delay with a wait for trigger set to 45 mins and set to continue on time out.

Set the trigger to be what was the two IF conditions.

That way the automation runs at the set time, confirms the battery is above 40, sets the heating system and then waits for up to 45 mins before switching the system back unless the wait for triggers cause it to happen before the 45 mins has elapsed.

Hope that makes sense, if not shout and I will post up an example when in front of a computer next.

Best practice is not to have any sort of waiting or timing within your automation (apart from for in state triggers). Trigger, then take immediate action and finish. Something like the below, where I’ve tried to make a trigger from all the “when” events you mention in your text or your code. I’ve used a choose rather than if despite only being two options as I prefer the syntax and it’s easier to expand at a later date.

I’ve removed all the device noise: you’ll need to check if the water_heater reacts to a homeassistant.turn_off service call, otherwise put your device call in its place.

alias: Auto Hot Water - Mon to Fri
description: >
    Triggers at 06:40 to turn on the heater if the battery is above 40%.
    Triggers again to turn the heater off:
    after 45 mins of being on;
    if the battery drops below 15%;
    or if the instant electricity is above 1000.
trigger:
  - platform: time
    at: "06:40:00"
  - platform: state
    entity_id: light.immersion_switch
    to: 'on'
    for: "00:45:00"
  - platform: numeric_state
    entity_id: sensor.solarsystem_battery_capacity
    below: 15
  - platform: numeric_state
    entity_id: sensor.home_instant_electricity
    above: 1000

condition:
  - or:
      - "{{ trigger.platform == 'time' and states('sensor.solarsystem_battery_capacity')|float(0) > 40 }}"
      - "{{ trigger.platform != 'time' and is_state('light.immersion_switch', 'on') }}"
  - "{{ now().weekday() < 5 }}"

action:
  - choose:
      - conditions: "{{ trigger.platform == 'time' }}"
        sequence:
          - service: homeassistant.turn_on
            entity_id: light.immersion_switch
          - service: homeassistant.turn_off
            entity_id: water_heater.downstairs
    default:
      - service: homeassistant.turn_off
        entity_id: light.immersion_switch
      - service: water_heater.set_operation_mode
        data:
          operation_mode: eco
        target:
          entity_id: water_heater.downstairs

1 Like

Thank you @tmjpugh, @rossk and @Troon for the responses, apologies I must have missed them in my notifications.

I ended up going a different route to achieve what I was aiming for.

Read on if interested!

I have an existing automation that automatically turns the immersion on when my export hits a certain threshold during the day (as well as one to auto flick it off if it suddenly starts to pull from the grid)

I therefore decided to create a sensor to count how often this is ‘on’ for and then have an automation that automatically triggers a hot water boost via my hive for 45 minutes at 6AM if it is below a threshold (0.7).

That way, if the immersion doesn’t come on, it auto flicks the gas hot water on (saving the wife shouting at me for no hot water!) but it won’t do it when we have had enough sun to have heated the water during the day.

If curious, these are my automations etc.

- platform: history_stats
  name: Immersion Status
  entity_id: light.immersion_switch
  state: "on"
  type: time
  end: "{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}"
  duration:
    hours: 24
alias: Auto Hot Water Boost
description: ""
trigger:
  - platform: time
    at: "06:00:00"
condition:
  - condition: numeric_state
    entity_id: sensor.immersion_status
    below: 0.7
action:
  - service: hive.boost_hot_water
    data:
      time_period: "00:45:00"
      on_off: "on"
      entity_id: water_heater.downstairs
mode: single