Time based shut off not working consistently

Hi, I use an automation for my pool pump so that it runs when my solar is producing over 2000w of power. The start up and shut down based on solar output work fine, but I have an automation to shut everything down (incl automations) once the pump has been running for 4.1 hours since midnight. It doesn’t consistently operate when it hits 4.1 hours and sometimes just runs for the entire day because the 4.1 hour trigger didn’t activate.

Configuration.yaml as below:

sensor:
  - platform: history_stats
    name: Pool pump ON today
    entity_id: switch.pool_pump
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now().replace(hour=23, minute=59, second=0) }}"

Automation as follows:

alias: Pump Off 4 Hours
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.pool_pump_on_today
    from: '4.1'
condition: []
action:
  - type: turn_off
    device_id: (removed for post)
    entity_id: switch.pool_pump
    domain: switch
  - service: automation.turn_off
    target:
      entity_id:
        - automation.pool_pump_on
        - automation.pump_off_4_hours
        - automation.pool_pump_no_solar
        - automation.pool_pump_off
        - automation.pool_pump_no_solar_off
    data:
      stop_actions: false
  - service: notify.mobile_app_phone
    data:
      message: 'Pump off after 4 hrs '
mode: single

Is anyone able to see anything that might be causing the inconsistent operation of this automation? It worked for the past two days flawlessly, but today the pump operated for 4.2 hours before I caught it out and manually turned it off. This kind of inconsistency has been happening for the 3-4 months that I’ve had it all in place.

Appreciate any help

Thanks

Change your trigger to this:

trigger:
  - platform: numeric_state
    entity_id: sensor.pool_pump_on_today
    above: 4.09

The problem with your current trigger is that the state may change from 4.09 or below to 4.11 or above, skipping 4.1 entirely.

Thanks heaps. I did a test with this and it worked properly. I’ll keep an eye on it to make sure it’s consistent. I suspected the issue with my existing solution was the state skipping the number but didn’t know about the numeric state trigger or any other way to manage it. Thanks!

All of the available triggers are listed here (with examples):

And conditions too:

It is worthwhile reading.

Hiya,
I’m successfully using an automation for my pool pump based on your sample (thank you very much!). But I’m just having a hard time trying to work out how to timer counts up the hours the pool pump runs. I’ve looked again and again at the configuration.yaml set up and can’t understand how the timer/counter works.

Apologies for being such a noob - there’s probably a simple answer but I just can’t get it. And I’d rather understand how it works than just using a “magic potion”!!!

Ahhhhh. Sensor is part of the History_stats integration which provides a way to watch the activity of another entity. It can track how long another entity has been active. No wonder I couldn’t see how it works, the smarts are all hidden in the integration. Got it.