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’.
Извините, я не знаю Английский.