Problem with using binary sensor boolean as trigger in "on_boot":

No matter what I try, I cannot get on_boot code to execute. This HAS to be something simple…

esphome:
  name: ${device_name}
  comment: ${device_description}
  platform: ESP8266
  board: esp01_1m
  on_boot:
    #priority: -100.0
    then:
      - delay: 5s
      - logger.log: "Im in the on_boot section.  Now write to the log and show me I am."

if I don’t see a log entry, theres no way my code executes, which it doesnt. what am I doing wrong?

Try setting priority as -100 and increase the delay to 10 or 15s and try. Maybe the logger is not intialized at priority 600 which is default.

that totally worked. thanks!

Now how do I use a binary sensor to trigger my while loop of “on_boot”? I’ve tried referencing the ID as well as using a lambda and setting globals, all to no avail. I just cant recognize its on/true state. It works outside of on_boot. I check the same sensor in a binary_sensor section using the same

condition:
  - binary_sensor.is_on: p5z

and it works fine. but not here:

  on_boot:
    priority: -100.0
    then:
      - delay: 20s
      - logger.log: 
          format: "In on_boot: p5z =%1f | auger_off_timez = %.1f | auger_on_timez = %.1f "
          args: [ 'id(p5z).state', 'id(auger_off_timez).state', 'id(auger_on_timez).state' ]
      - while:
          condition:
#            lambda: |-
#             if (id(p5z)) {
#                return true;
#              } else {
#                return false;
#              }
            - binary_sensor.is_on: p5z
          then:
            - logger.log: "P5 active.in WHILE LOOP " 

its always reporting a value of 0.0 in my logger statement, even when it is TRUE:

          format: "In on_boot: p5z =%1f | auger_off_timez = %.1f | auger_on_timez = %.1f "
          args: [ 'id(p5z).state', 'id(auger_off_timez).state', 'id(auger_on_timez).state' ]

[10:35:31][D][main:318]: In on_boot: p5z =0.000000 | auger_off_timez = 10.5 | auger_on_timez = 1.0

The full sections:

on_boot:
    priority: -100.0
    then:
      - delay: 20s
      - logger.log: 
          format: "In on_boot: p5z =%1f | auger_off_timez = %.1f | auger_on_timez = %.1f "
          args: [ 'id(p5z).state', 'id(auger_off_timez).state', 'id(auger_on_timez).state' ]
      - while:
          condition:
#            lambda: |-
#             if (id(p5z)) {
#                return true;
#              } else {
#                return false;
#              }
            - binary_sensor.is_on: p5z. <<<----THIS DOES NOT WORK
          then:
            - logger.log: "P5 active.in WHILE LOOP " 
            - switch.turn_on: auger_output_to_optoisolator    
            - logger.log: 
                format: "on_boot in WHILE auger_ON_timez Delay Value: %.1f .1f"
                args: [ 'id(auger_on_timez).state']
            - delay: !lambda "return id(auger_on_timez).state * 1000;"

globals:
  - id: g_p5
    type: bool
    initial_value: 'false'

binary_sensor:
  - platform: homeassistant
    id: p5z
    entity_id: input_boolean.p5
    on_state:     # WHEN STATE IS PUBLISHED
      then:
        - globals.set:
            id: g_p5
            value: !lambda 'return bool(x);'   
#
  - platform: gpio
    pin: GPIO4
    id: esph_house_auger_sensor_from_mcu
    name: "esph House Auger Sensor from MCU" #pinAuger signal from MCU, goes low when motor is running
    filters:
      - invert:
# This is the code that will run when the binary sensor detects that the Auger motor needs to be turned off
    on_state:      
      then:
        - logger.log:
            format: "GPIO4 on_state: changed! p5z =%1f | auger_off_timez = %.1f | auger_on_timez = %.1f "
            args: [ 'id(p5z).state', 'id(auger_off_timez).state', 'id(auger_on_timez).state' ]

     # Turn on the Auger motor
        - if:
            condition:
              and:
                - binary_sensor.is_off: p5z. <<<<----THIS WORKS FINE.
                - binary_sensor.is_off: esph_house_auger_sensor_from_mcu
            then:
              - switch.turn_off: auger_output_to_optoisolator

EDIT: Nothing I tried worked so I moved it out of on_boot and into the binary sensor on_state trigger which works perfectly. VERY STRANGE. I battled this for almost a day.

binary_sensor:
  - platform: homeassistant
    id: p5z
    entity_id: input_boolean.p5
    on_state:     # WHEN STATE IS PUBLISHED
      then:
#        - logger.log: "P5 state changed" 
        - while:
            condition:
                - binary_sensor.is_on: p5z
            then:
              - logger.log: "P5 active.in WHILE LOOP " 
              - switch.turn_on: auger_output_to_optoisolator    
              - logger.log: 
                  format: "on_boot in WHILE auger_ON_timez Delay Value: %.1f .1f"
                  args: [ 'id(auger_on_timez).state']
              - delay: !lambda "return id(auger_on_timez).state * 1000;"
              #
              - switch.turn_off: auger_output_to_optoisolator  
              - logger.log:
                  format: "on_boot in WHILE auger_OFF_timez Delay Value: %.1f .1f"
                  args: [ 'id(auger_off_timez).state']            
              - delay: !lambda "return id(auger_off_timez).state * 1000;"
              - logger.log: "P5 active Auger OFF after auger_off_timez"   

See this post for on_boot: delay to make sure api has connected and has provided an update to the binary sensor.

whats strange that even if I put a 20 second delay in, I still couldnt get it to recognize the binary sensor.

Moving the whole thing to sensor on_state works fine, so I’ll just leave it there. After reviewing some of my old esphome config files, I see I’ve had similar problems w/ on_boot in the past. Its not very well documented.