Having trouble with globals and int/bool types

I’m trying to use a global variable as a flag in some routines within esphome.
Unfortunately when I try to expose them in the log, the whole chip crashes and reboots. Thankfully it’s up long enough to redownload a new build with the logger lines commented out. I’ve tried both type int and bool. They both cause crashes. It must be in the printf type, %d or %s. Not sure what a Boolean type should be?

globals:
  - id: p5flag
    type: int
    initial_value: '0'
    restore_value: yes
  
binary_sensor:
  - platform: gpio
    pin: GPIO4
    id: esph_house_auger_sensor_from_mcu
    name: "esph House Auger Sensor from MCU"
    filters:
      - invert
    on_state:      
      then:
     - logger.log:
            format: "P5FLAG = %i"
             args: [ 'id(p5flag)' ]
#
# Ive also tried this 
#
globals:
  - id: p5flag
    type: bool
    initial_value: 'true'
    restore_value: yes
    
binary_sensor:
  - platform: gpio
    pin: GPIO4
    id: esph_house_auger_sensor_from_mcu
    name: "esph House Auger Sensor from MCU"
    filters:
      - invert
    on_state:      
      then:
         - logger.log:
             format: "P5FLAG = %s"
             args: [ 'id(p5flag)' ]

Any tips on the proper format for a bool global appreciated. Also how do you test it in a condition? Is lambda the only way?

%i or %d should work for booleans. Looks to me like lambda is the only way to test value in a condition.

Your snippet of the configuration has some indention issues. Does the gpio4 toggle quickly that the log would be flooded with messages to cause the reboot?

I ended up with this finally working:

        - logger.log: 
            format: "script.END STARTING AGAIN | p5flag = %d"
            args: [ 'id(p5flag)' ]         

#and the condition test:

            condition:
              and:
                - binary_sensor.is_off: esph_house_auger_sensor_from_mcu
                - lambda: return !id(p5flag);

This stuff can certainly be tricky when it comes to formatting!

1 Like