Help to fix my pool automation template

I have a complex package to manage my pool pump but at the core, a power monitor determines how long the pump has been running.

# Reports 'on' time as a decimal. Used to stop pump after 8.00 hours #
  - platform: history_stats
    name: Pool Pump running today
    entity_id: sensor.pool_pump_status
    state: 'Active'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

When the time on exceeds 8.0 hours, the pump switches off.

  - alias: 'Turn Pool Pump Off after 8hrs'
    initial_state: true   
    trigger:
    - platform: numeric_state
      entity_id: sensor.pool_pump_running_today
      above: 8.00
    condition:
        - condition: state
          entity_id: input_boolean.swimming_season
          state: 'on'
        - condition: state
          entity_id: input_select.pool_pump
          state: Auto
        - condition: state
          entity_id: switch.pool_pump
          state: 'on'         
    action:
      service: homeassistant.turn_off
      entity_id: switch.pool_pump

Yesterday I had my external docker based broker offline for at least 2hrs while I upgraded my NAS so at lot of the milestone messages were not reported to HA. Late last night I heard my pool pump going despite me checking the ‘time-on’ and it being 10:25m.

Obviously the transition from 08:00hrs to 08:01 got missed so the automation to turn off never happened? Is there some code I can add (maybe on HA start) that checks if the run time is over 8hrs and turns the pump off? A sort of safety override if you will.

  - alias: 'Turn Pool Pump Off after 8hrs'
    initial_state: true   
    trigger:
      - platform: homeassistant
        event: start
      - platform: numeric_state
        entity_id: sensor.pool_pump_running_today
        above: 8.00
    condition:
      - condition: state
        entity_id: input_boolean.swimming_season
        state: 'on'
      - condition: state
        entity_id: input_select.pool_pump
        state: Auto
      - condition: state
        entity_id: switch.pool_pump
        state: 'on'
      - condition: numeric_state
        entity_id: sensor.pool_pump_running_today
        above: 8.00 
    action:
      service: homeassistant.turn_off
      entity_id: switch.pool_pump
1 Like

Nice. You’ve added the trigger into the conditions? Had not though of that. Thank you. Oooh, and the homeassistant start trigger too!

1 Like

Yeah, I duplicated your original trigger in the conditions so that when the trigger is the homeassistant start event it still only fires if the time is over 8 hours :+1:

1 Like