Pool pump dynamic management - TIME trigger issue

I’m trying to dynamically manage the pool pump time based on the water temperature.

The point is that I would switch on the pump at a specific time that is calculated by this template, but it doesn’t start. What am I missing?

  - platform: template
    sensors:
      orario_inizio_calcolato:
        unique_id: orario_inizio_calcolato
        value_template: >
          {% set ore_funzionamento_stimate = states('sensor.ore_funzionamento_stimate') | float %}
          {% set ore_da_sottrarre = ore_funzionamento_stimate / 2 %}
          {% set ora_iniziale = strptime('13:00', '%H:%M') %}
          {% set ora_finale = ora_iniziale - timedelta(hours=ore_da_sottrarre) %}
          {{ ora_finale.strftime('%H:%M:%S') }}

Output is this but seems is not recognize as time trigger automation.

You’re missing the device class.

https://www.home-assistant.io/docs/automation/trigger/#sensors-of-datetime-device-class

  - platform: template
    sensors:
      orario_inizio_calcolato:
        unique_id: orario_inizio_calcolato
        device_class: timestamp  ## <-------------THIS
        value_template: >
          {% set ore_funzionamento_stimate = states('sensor.ore_funzionamento_stimate') | float %}
          {% set ore_da_sottrarre = ore_funzionamento_stimate / 2 %}
          {% set ora_iniziale = strptime('13:00', '%H:%M') %}
          {% set ora_finale = ora_iniziale - timedelta(hours=ore_da_sottrarre) %}
          {{ ora_finale.strftime('%H:%M:%S') }}

You really should be using the new template integration for new template sensors.

configuration.yaml (not sensors.yaml)

template:
  - sensor:
      - name: Orario Inizio Calcolato
        unique_id: orario_inizio_calcolato
        device_class: timestamp
        state: >
          {% set ore_funzionamento_stimate = states('sensor.ore_funzionamento_stimate') | float %}
          {% set ore_da_sottrarre = ore_funzionamento_stimate / 2 %}
          {% set ora_iniziale = strptime('13:00', '%H:%M') %}
          {% set ora_finale = ora_iniziale - timedelta(hours=ore_da_sottrarre) %}
          {{ ora_finale.strftime('%H:%M:%S') }}
1 Like

Thank you, I tried to add device class, use the new template syntax and write everything in a packages yaml.

Anyway even I tried to simplify the template, I’m getting crazy to make it work:

Sensor is shown as UNKNOWN but running the template in developer tools’s template editor it seems working:

template:
  - sensor:
      - name: Ore stimate funzionamento
        unique_id: estimated_running_time
        device_class: timestamp
        state: >
          {{ (((states('sensor.gw2000a_pool_temperature') | float ) * (1 + (states('input_number.delta_ore_funzionamento') | float / 100))) | round(0, 'ceil') * 3600) | timestamp_custom('%H:%M', false) }}

What am I doing wrong?

Did you restart HA as you are using the template integration for the first time?

Yes, I restarted several times

Are there any errors in your log related to this sensor?

Emhmm… yes. I didn’t notice:

https://www.home-assistant.io/integrations/sensor/

Screenshot 2024-04-25 at 20-00-10 Sensor

Has to be this format: https://en.wikipedia.org/wiki/ISO_8601#Times

Ok thank you, it show the values now but in date-time format.

The point is now that removing date from the template it gives “unknown”.

It should:

  • calculate the pump WORKING TIME based on the half of pool water temperature
  • calculate the START time deducting half of working time from 14:00
  • calculate the STOP time adding half of working time from 14:00

The result is this:

using this code:

template:
  - sensor:
      - name: Ore stimate funzionamento
        unique_id: estimated_running_time
        device_class: timestamp
        state: >
          {{ (((states('sensor.gw2000a_pool_temperature') | float / 2) * (1 + (states('input_number.delta_ore_funzionamento') | float / 100))) | round(0, 'ceil') * 3600) | timestamp_custom('%Y-%m-%dT%H:%M:%S%z', false) }}

  - sensor:
      - name: Pump start time (automatic)
        unique_id: Pump_start_time_automatic
        device_class: timestamp
        state: >
          {{ (as_timestamp(strptime('14:00', '%H:%M')) - ((((states('sensor.gw2000a_pool_temperature') | float / 2 / 2) * (1 + (states('input_number.delta_ore_funzionamento') | float / 100))) | round(0, 'ceil') * 3600))) | timestamp_custom('%Y-%m-%dT%H:%M:%S%z', true) }}

  - sensor:
      - name: Pump stop time (automatic)
        unique_id: Pump_stop_time_automatic
        device_class: timestamp
        state: >
          {{ (as_timestamp(strptime('14:00', '%H:%M')) + ((((states('sensor.gw2000a_pool_temperature') | float / 2 / 2) * (1 + (states('input_number.delta_ore_funzionamento') | float / 100))) | round(0, 'ceil') * 3600))) | timestamp_custom('%Y-%m-%dT%H:%M:%S%z', true) }}