Lambda if statement help

I’m experimenting with 8x8 matrices (actually I’m more of a tester) so ignore the platform:.

This sketch used to work fine, however, since I changed the lambda to include the pills section it doesn’t seem to work correctly, it should switch displays between the time and leaf unless the input_boolean on, but it’s always switching between time and tablets no matter if pills is on or off.

esphome:
  name: clock
  platform: ESP32
  board: nodemcu-32s

wifi:

  networks: 
    - ssid: !secret wifi
      password: !secret wifi_password
    - ssid: !secret wifi2
      password: !secret wifi_password2

  manual_ip:
    static_ip: 192.168.0.238
    gateway: 192.168.0.1
    subnet: 255.255.255.0
    dns1: 192.168.0.1
    dns2: 8.8.8.8

# Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Clock Fallback Hotspot"
    password: !secret wifi_password

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret api_password

ota:
  password: !secret ota_password

switch:
  - platform: restart
    name: "Clock Restart"

time:
  - platform: homeassistant
    id: esptime

sensor:
  - platform: homeassistant
    id: leaf_battery
    entity_id: sensor.leaf_battery
    internal: true

binary_sensor:
  - platform: homeassistant
    entity_id: input_boolean.pills
    id: pills
    internal: true

globals:
  - id: my_global_bool
    type: bool
    restore_value: no
    initial_value: 'true'

# SPI for max7219digit
spi:
  clk_pin: GPIO18
  mosi_pin: GPIO19

display:
  - platform: max7219digit_v1
    cs_pin: GPIO21
    id: my_matrix
    num_chips: 8
    lambda: |-
      if (id(my_global_bool)) {
        it.strftime("%H:%M %A", id(esptime).now());
      }
      else {
        if (id(pills)) {
          it.printf("TABLETS");
        }
        else {
          it.printf("LEAF %2.0f%%", id(leaf_battery).state);
        }
      }
# For example cycle through pages on a timer
interval:
  - interval: 5s
    then:
      - lambda: |-
          if (id(my_global_bool)) {
            id(my_global_bool) = false;
          }
          else {
            id(my_global_bool) = true;
          }