On_boot time condition check

Hallo!
I want to check time on boot and if time after 7 and before 22 switch relays on or off

 on_boot:
    priority: -100.0
    then:
      - if:
          condition:
            and:
              - lambda: |-
                  return id(sntp_time).now().hour > 7;
              - lambda: |-
                  return id(sntp_time).now().hour < 22;
          then:
            - switch.turn_on: relay1
            - switch.turn_on: relay2
          else:
            - switch.turn_off: relay1
            - switch.turn_off: relay2

if I reboot esp at example 07:20 relays are off

I have 2 questions:

  1. How can i define time interval in hours and minutes on boot?
  2. If i restart ESP by switch from HA - on_boot automation works good
    but if restart ESP by turning power off and on - on_boot automation does not work

any help?
peace!

Could you use on_time: instead?

I use on_time: to switch relays on and off at 7 and 22.

  - platform: sntp
    id: lamps_on
    on_time:
      - seconds: 0
        minutes: 0
        hours: 7
        days_of_week: SUN-SAT
        then:
          - switch.turn_on: relay1
          - switch.turn_on: relay2

I need to check time interval then esp boots after power outage… and switch relays according to this interval on or off

If I understand correctly what you are trying to achieve, you want the realys to go to the state they were before the reboot.

If so, have you tried using restore_mode in your switch config?

Or use this: Restore from flash.

imagine the situation:

My relays turns on at 7.00 and turns off at 22.00.
Let’s say that now time is 21.00 (relays are on) and the power suddenly turned off(power outages).
And then turned on at 23.00(relays should already must be off)
So I need to verify what time is now after esp boot up
and if time between 7.00 and 22.00 - turn relays on. If no - turn off

I think explained clearly
sorry for my English)

On boot, the device won’t know what the time is straight away. I guess you’ve tried to work around that with your priority: -100.0, but the docs say:

Before the ESP has connected to the internet and can get the current time the date will be January 1st 1970. So make sure to check if .is_valid() evaluates to true before triggering any action.

Maybe that’s the issue? When you restart via the reset button, it may remember the time, but not through a power cycle. Is there anything in the logs that might help?

1 Like

solved 2-nd problem with

on_boot:
    priority: -100.0
    then:
      - delay: 4s

but 1-st problem is actual

1-st problem solved

respect to John

Continuing the discussion from time condition check:

#####################################
  time:
    - platform: homeassistant
      id: homeassistant_time
    - platform: sntp
      id: sntp_time

  binary_sensor:
  - platform: template
    device_class: light
    name: "(bs) Day"
    id: bs_sun

  - platform: gpio
    pin:
      number: D5
      mode: INPUT_PULLUP
      inverted: yes
    name: "PIR Sensor"
    device_class: motion
    id: pir_sensor
    on_press:
      then:
        - script.execute: scrl_day_night
        - if:
            condition:
              binary_sensor.is_on: bs_sun
            then:
              - logger.log: "Day, Day, Day !!!!!!!!!!!!!!!!!!!!!!!!"
              # - script.execute: scrl_day
            else:
              - logger.log: "Night, Night, Night, !!!!!!!!!!!!!!!!!!!!!!!!"
              # - script.execute: scrl_night

  script:
    - id: scrl_day_night
      then:
        - if:
            condition:
              and:
                - lambda: |-
                    return id(sntp_time).now().hour > 7;
                - lambda: |-
                    return id(sntp_time).now().hour < 23;
            then:
              # - logger.log: "YESSSSS !!!!!"
              - lambda: |-
                    id(bs_sun).publish_state(true);
            else:
              # - logger.log: "NOOOO !!!!!"
              - lambda: |-
                    id(bs_sun).publish_state(false);
#####################################

Проверка через ‘script’.
Извините, я не знаю Английский.

Google brought me here but there was no clear solution just hints. I eventually got it working with this code

esphome:
  name: front-porch
  friendly_name: Front Porch

  # Ensure correct switch state on boot based on current time
  # Useful for handling power cycling
  on_boot:
    priority: -100
    then:
      - delay: 4s
      - if:
          condition:
            lambda: |-
              auto current_time = id(homeassistant_time).now();
              int hour = current_time.hour;
              return hour > 19;
          then:
            - switch.turn_on: porch_cat_door
      - if:
          condition:
            lambda: |-
              auto current_time = id(homeassistant_time).now();
              int hour = current_time.hour;
              return hour < 11;
          then:
            - switch.turn_on: porch_cat_door

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "***redacted***"

ota:
  - platform: esphome
    password: "***redacted***"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Front-Porch Fallback Hotspot"
    password: "***redacted***"

captive_portal:

web_server:
  port: 80
  
switch:
    
  - platform: gpio
    id: porch_cat_door
    name: "Cat Door"
    pin: GPIO5
    inverted: False
    restore_mode: RESTORE_DEFAULT_OFF
    
  - platform: gpio
    id: garden_tap
    name: "Garden Watering"
    pin: GPIO4
    inverted: False
    restore_mode: RESTORE_DEFAULT_OFF

    # When the switch turns on, start the 60-minute timer script
    on_turn_on:
      - script.execute: tap_timer_script

    # When the switch turns off, stop the timer script
    on_turn_off:
      - script.stop: tap_timer_script 

# Script to handle the timer with reset functionality
script:
  - id: tap_timer_script
    then:
      - delay: 30s  # 60 minutes
      - logger.log: "60-minute timer expired. Turning off the switch."
      - switch.turn_off: garden_tap
      
time:
  - platform: homeassistant  # Use Home Assistant time, or use 'sntp' for internet time
    id: homeassistant_time

    # Schedule to turn on the switch at 7 PM every day
    on_time:
      - seconds: 0
        minutes: 0
        hours: 19  # 7 PM in 24-hour format
        then:
          - logger.log: "Turning on the switch at 7 PM"
          - switch.turn_on: porch_cat_door      

    # Schedule to turn off the switch at 6 AM every day
      - seconds: 0
        minutes: 0
        hours: 11  # 6 AM in 24-hour format
        then:
          - logger.log: "Turning on the switch at 6 AM"
          - switch.turn_off: porch_cat_door      





1 Like