Run only within specified timewindow

ESPHOME

My challenge is to run a heating floor pump only during certain time windows. Say between 06:00 and 20:00 when the inlet temperature and outlet temperature are at a defined level. When the temperature changes within this time window the pump needs to be turned on and off respectively. Turning it on at 06:00 and off at 20:00 is not an option. I’m missing something to get this working. I therefore went into lambda code, but there must be a way to get this working without the lambda code. By the way the lambda code also doesn’t work, but I listed it anyhow to get the idea what I want to achieve.
My first challenge is to get the condition to execute the code only during the time window 06:00 till 20:00.

Something liek this:

on_...:
          then:
            - if:
                condition:
                  - ...... 6 < hour < 20
                then:

My not working attempt with lambda code:

time:
  - platform: homeassistant
    id: homeassistant_time

    on_time:
      - seconds: 0  # needs to be set, otherwise every second this is triggered!
        minutes: '*'  # Trigger every minute
        then:
          lambda: !lambda |-
            auto time = id(homeassistant_time).now();
            if (id(CV_Floor_Pump_Temp_In).has_state() >= 44) 
              {
              id(Relay01).turn_off();
              id(CV_Floor_Pump_TemperatureTooHigh).turn_on();
              }
            else 
              {
              if ((id(homeassistant_time).now().hour >= 06) and (id(homeassistant_time).now().hour <= 20)) //within operational hours
                {
                if (id(CV_Floor_Pump_Temp_In).has_state() >= 40) 
                  {
                  id(Relay01).turn_off();
                  } 
                else
                  { 
                  if ((id(CV_Floor_Pump_Temp_In).has_state() >= 27) and ((id(CV_Floor_Pump_Temp_In).has_state()) > (id(CV_Floor_Pump_Temp_Out).has_state())))
                    {
                    id(Relay01).turn_on();
                    }
                  if ((id(CV_Floor_Pump_Temp_In).has_state() <= 22) or ((id(CV_Floor_Pump_Temp_In).has_state()) < (id(CV_Floor_Pump_Temp_Out).has_state())))
                    {
                    id(Relay01).turn_off();
                    }
                  }  
                }
                
               if ((id(homeassistant_time).now().hour == 19) and (id(homeassistant_time).now().minute == 39)) //at 20:05 run for 10 minutes
                {
                if (id(CV_Floor_Pump_Temp_In).has_state() >= 40) 
                  {
                  id(Relay01).turn_off();
                  } 
                else
                  { 
                  id(Relay01).turn_on();
                  }
                }  
              if ((id(homeassistant_time).now().hour == 19) and (id(homeassistant_time).now().minute == 40)) //at 20:15
                {
                id(Relay01).turn_off();
                }                
                
              }

This example might help:

- lambda: |-
    int t_now = parse_number<int>(id(homeassistant_time).now().strftime("%H%M")).value();
    if ( ( (t_now >= 600) && (t_now <= 2000) ) ) {
      id(Relay01).turn_on();
    } else {
      id(Relay01).turn_off();
    }

Hi Ed,

Thanks, just tested and works like a charm.
My question still remains, can this be done without using lambda code? So more in line like this:

on_...:
          then:
            - if:
                condition:
                  - ...... 6 < hour < 20
                then:

You can use a crontab as a time trigger.

Hi Mikefila,
That’s also an option, but still not what I’m looking for. My intention is to limited the amount of programning languages as much as possible. Actually make it as simple as possible. By using ESPHome there are many advantages, but need to use the YAML syntax and C++ when going into lambda code blocks. If I could skip the lambda code block or side steps like crontab. Then I could do everything in YAML and it would be perfect for me.

Set your actions up in a template switch. Turn the switch on/off with a time trigger.

Thank you all for your help. I made the decision for now to continue with both languages. Below is my code which is working fine.

#CV_Floor_Pump
esphome:
  name: esp06
  platform: ESP8266
  board: nodemcuv2
  on_boot:
    priority: -100 #lowest priority so start last
    then:
      - lambda: id(Relay01).turn_off();
      - lambda: id(CV_Floor_Pump_TemperatureTooHigh).turn_off();

# Enable logging
logger:
  level: DEBUG
  baud_rate: 115200
  id: logToLog

# Enable Home Assistant API
api:

ota:
  password: "16c2972e32843010a0ebe73ea5698eca"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  reboot_timeout: 20hours # Reboots for the case that there is no wifi for more than 20 hours
  
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp06 Fallback Hotspot"
    password: "gSHMN961CTuf"

captive_portal:


time:
  - platform: homeassistant
    id: homeassistant_time

    on_time:
      - seconds: /30  # needs to be set, otherwise every second this is triggered!
        minutes: '*'  # Trigger every minute
        then:
          lambda: !lambda |-
            auto time = id(homeassistant_time).now();
            int t_now = parse_number<int>(id(homeassistant_time).now().strftime("%H%M")).value();
            float temp_in = static_cast<int>(id(CV_Floor_Pump_Temp_In).state);
            float temp_out = static_cast<int>(id(CV_Floor_Pump_Temp_Out).state);
            if ((temp_in) >= 44)
              {
              id(Relay01).turn_off();
              id(CV_Floor_Pump_TemperatureTooHigh).turn_on();
              }
            if (((temp_in) >= 40) || ((temp_out) >= 40))
              {
              id(Relay01).turn_off();
              }
            else
              {
              if ((t_now >= 600) && (t_now <= 2000))
                {
              	if (((temp_in) >= 27) && ((temp_in) > (temp_out)))
            		  {
            			id(Relay01).turn_on();
            			}
            		if (((temp_in) <= 25) || ((temp_in) < (temp_out)))
            		  {
            			id(Relay01).turn_off();
            			id(CV_Floor_Pump_TemperatureTooHigh).turn_off();
            			}
            		 }
              if ((t_now >= 2030) && (t_now <= 2040))
                {
                id(Relay01).turn_on();
              	}
              if ((t_now >= 2042) && (t_now <= 2100))
                {
                id(Relay01).turn_off();
                }
              }

sensor:
# Define the temperature sensor. In this case the humidity sensor is not used
# CV_Floor_Pump_Temp_In
  - platform: dht
    pin: D5
    temperature:
      name: "CV_Floor_Pump_Temp_In"
      id: CV_Floor_Pump_Temp_In
    update_interval: 30s

# CV_Floor_Pump_Temp_Out
  - platform: dht
    pin: D6
    temperature:
      name: "CV_Floor_Pump_Temp_Out"
      id: CV_Floor_Pump_Temp_Out
    update_interval: 30s

#CV_Floor_Pump_Relay
switch:
  - platform: gpio
    pin:
      number: D7
      inverted: false
    id: Relay01
    name: CV_Floor_Pump_Relay #by providing a name the Relay01 will become visible in Home Assistant under the name as described by name

#CV_Floor_Pump_TemperatureTooHigh
  - platform: gpio
    pin:
      number: D2 #dummy as there is nothing connected, but needed to automatically create a switch in Home Assistant
    id: CV_Floor_Pump_TemperatureTooHigh
    name: CV_Floor_Pump_TemperatureTooHigh 

# TM1637
display:
    platform: tm1637
    id: tm1637_display
    clk_pin: D4
    dio_pin: D3
    intensity: 0 # Ranging from 0 - 7
    #Make sure that any comment in the lambda code block is started with // as all
    #  code in the block is C++.
    lambda: |-
      if (id(CV_Floor_Pump_Temp_In).has_state()) {
        it.printf(0, "%.0f", id(CV_Floor_Pump_Temp_In).state);
      }
      if (id(CV_Floor_Pump_Temp_Out).has_state()) {
        it.printf(2, "%.0f", id(CV_Floor_Pump_Temp_Out).state);
      }

#end code


```