Calculate Time of a Switch in a certain state

I need to run Pool filtration not less than 8 hours a day.
I have a Switch that turns it on.
Is there a way to track how many hours a day this Switch has been turned on? I need to make an automation with condition: in case until the end of the day (till 23:59) Switch hasn’t been turned on, it should be turned on so that filtration would run for a necessary period on time (e.g. since 00:00 it was turned off an in 16:00 it needs to be automatically turned on). But in case if filtration was used for a certain time of current day the remaining time for its automatic operability in TurnedOn state should be less (mathematical operation needs to calculate it).

I pulled this out of old file, need to check it, with new HA version for syntax.
I have a counter[Counter - Home Assistant] that increments every minute that the pool pump switch is on, it resets to zero every morning
I use an input_number[Input Number - Home Assistant] to set the minimum run times for the pump, daily
I use this template in an automation trigger to see if the pump needs to turn on. If it is a negative value pump turns on.

 value_template: >-
       {{ (((((as_timestamp(state_attr('sun.sun','next_rising')) | int) -
       ((as_timestamp(now())) | int )) / 60) | int) - (
       (((states('input_number.pool_pump_run_hours')) | int ) * 60) -
       ((states('counter.pool_pump_running_minutes')) | int))) }}

So basically, if I have the daily run hours set for 8, and the pool has run 4 hours today, the pump will start 4 hours before sunrise, and get the 8 hours in for that day. You can modify as you wish to suit your needs.

1 Like

use the History Stats - Home Assistant integration. It will sum the time for the current day. Make an automation to shut off your switch when the result is greater than 8.

sensor:
  - platform: history_stats
    name: Switch ON today
    entity_id: switch.xyz
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
automation:
- alias: Turn off switch when it's been on for 8 hours today.
  trigger:
  - platform: numeric_state
    entity_id: sensor.switch_on_today
    above: 8
  action:
  - service: switch.turn_off
    target:
      entity_id: switch.xyz
7 Likes

True, but just an fyi, I don’t want the pool to shutoff after 8 hours, I want it to start if it has not done 8 hours today, in time to get 8 hrs by the end of the day. So if I turn the pump on manually at 8:00 because we have people coming over, I don’t want it to shut off on its own at 8 hours. But if its run 4 hours today, I want it to start when there are 4 hours remaining in the day. I think the OP wants similar.

Easiest way is create an automation with a schedule to turn it on and off (with a duration of 8 hours).

What are the requirements that prevent you from adopting this simple approach?

Or do you already have it running on a schedule but someone, or something, is turning off the pump before it completes the full duration?

Not to belabor this, and only speaking for my “complex solution” not the OP, but there are other factors requiring the complexity:
The pool pump turns on and off automatically for heating, amount of time is unpredictable.
People may turn on and off the pump when they want to use the pool, clean the pool, skim it extra etc. This is also unknown, and may occur up to 2:00 AM.
It is not desirable to run the pump more than 8 hours (but it may due to reasons above), so setting a time schedule wastes electricity, if the pump has already run enough for the days minimum, from other activities.

So this is not the filtration pump but a separate one dedicated for heating? Or it is just one filtration pump that only runs when heating is needed (which seems unusual to me, unless you live somewhere with cold summers and it runs for many hours a day)? :thinking:

It is the one pump, its Canada for me so, yeah, heating in summer and shoulder seasons. This is not an unusual setup, at least here, most heaters have aux contact to start pump, and flow switch to ensure its working. If you search pool heater schematic online, most all set-ups use the main pump. Mine actually uses a heat ex-changer with low BTU’s, runs a lot. Regardless of the heater, the pump is normally off, but if the pool is getting used it is turned on, this can vary from zero to beyond 8 hrs in a day. I used this set-up for a number of years very successfully, currently I just run a variable speed pump, 24/7 on very low watts. But based on the OPs question he is facing what I did.

Ditto for Canada and pump runs 11 hours daily during peak season. If heating is required, I can flip some valves to direct the flow through the heater, otherwise it’s bypassed.

The concept of operating a single-speed pump only when heating is foreign to me (and I have been running pools in homes I have owned since 1991 … when Mount Pinnatubo erupted and screwed up global weather causing a truly lousy, cold summer for using my newly-installed pool). Perhaps I have misunderstood your explanation but how do you ensure proper filtration and chlorination if the pump is permitted to run as little as never?

All guidance I have seen recommends a minimum number of hours (more than 8) and that’s easily done with a schedule. A variable or two-speed pump can run at low speed non-stop with the only thing scheduled being a switch to high speed for short period (or manually when cleaning or there are swimmers).

Anyway, maybe there’s something special about your arrangement that allows you to maintain water quality while operating the pump for as little as 0 hours a day?

Dears,

@Kdem is right. There are several factors that make the Pool Filtration pump operation time unpredictable.

  1. In my system there is a Heat Exchanger Pump that works in a boiler room and runs whenever a water temp in the Heat Accumulator is higher than Pool water temp (means - pool heating). In this case Pool Filtration pump is getting launched too (no heating possible without water circulation).
  2. People that may come & use the Pool. Sometimes it may be in the morning time, sometimes in the evening.
  3. Nobody may use the pool and it may not be heated as well. Anyhow, Filtration should be running.
    So explicit running of 8 HRs/day by simple Automation is not the best way.

I’ll apply the suggestions above & come back to you with the result. Thanks!

So when no one is using the pool and it’s not being heated, how long do you want the pump to run to maintain a minimum level of filtration?

Min. 8 hours, as it’s generally recommended.

So what would be the drawback to schedule the pump to run for a minimum of 8 hours?

Please read and understand the template trigger. The pool always runs the minimum hrs set by the input number, so the OP wants 8. The pool pump won’t run less than this; however the start time will be decremented based how much the pool pump ran each day for reasons other than this automation(this amount(the counter) is what can be zero), so that it doesn’t exceed 8 because of the automation’s actions. It may exceed 8 from other uses, i.e. if the pump is run manually for 12 hours, then the automation won’t need to run, and won’t trigger. That’s the beauty of it. If you want 11, you can have it too. I can tell you I have an always sparkling pool and never had it set for more than 8, and I have been doing this even longer than you…old guy.

When you wrote this, it was about your pool, was it not?

How does that not contradict your most recent statement:

It’s ‘normally off’ and ‘zero to beyond 8 hrs in a day’ and ‘never had it set for more than 8’ seems like Schrodinger’s Pump. :slightly_smiling_face: A clarification would be appreciated (your situation, not the OP’s).

This is ridiculous: this was about my set-up, I had in place on my pool, up until this spring when I changed to a VSP. They are not contradictory. The pump is normally off, until the heater or someone manually turns the pump on, or at the end of the day the automation turns it on. You are conflating two issues. The “never set below 8” is the setting of the minimum pump run time( the input number). The zero to beyond 8 hours in a day is the amount the pool was run for other reasons, i.e. the heater was on, someone ran the pump manually etc. They are not the same thing at all, and one can be zero, and the other 8 and the pump runs 8 hrs, which I believe is why you fail to understand.

Thanks for the clarification. Minimum 8 hours plus time for other purposes if they occur.

Almost, minimum 8 hrs, and maximum 8 hrs, allowing for other purposes, unless the other purposes themselves total more than 8 of course. The whole idea is to not run the pump more than needed, i.e., if its already run 7 hours for other purposes, then the automation will run for one hour.

1 Like

Given a hot day and no heating occurs and no one uses the pool, the pump doesn’t start its 8 hour run until when? The evening?

BTW, so far the requirements seem to point to the use of a History Sensor to monitor the pump’s daily hours (like in Petro’s post).


EDIT

If the pump is scheduled to run, say, at 19:00, it computes its duration by subtracting the History Sensor’s value from 8 hours. If it already ran for 6 hours then it runs for 2 hours.

Or is there a requirement to avoid running from a specific start time and be a bit more dynamic based on the day’s events?

The input number of hours before sunrise

So if you do this by 21:00, the pool has run 8 hours, now at 22:00 the heater comes on for 3 hours…you didn’t need to run the 2 hours, wasting electricity. If someone uses the pool from 10PM to 2 AM, (I have a teenager) same thing. I realize you could count these as the next days running, but I like it my way so the running happens in the dark, early in the morning, ensuring the chlorine level from SWG is highest just before the sun comes up.
Look, you don’t have to do this, think its great, understand it, or accept it, but it worked for me. I offered a path to a possible solution, for the OP as I understood his question. He is free to accept any of your and other’s solutions if they work better for him.