How to persist EspHome "Template Binary Sensor" state on reboot?

Hi Guys,

I’d like to persist my Template binary Sensor state on a reboot. I created this config with global variables which are soupposed to survive a reboot but for some reason they always default to ‘on’ after a reboot.

Could soneome please have a look at it? Thank you! :slight_smile:

#Device Specific Config
esp8266:
  board: esp01_1m
  restore_from_flash: true

esphome:
  name: "${device_name}"
  build_path: "./.build/${device_name}/"
  on_boot:
    priority: 800
    then:
      - lambda: |-
          id(template_sensor_1).publish_state(template_sensor_1_global);
          id(template_sensor_2).publish_state(template_sensor_2_global);
          id(template_sensor_3).publish_state(template_sensor_3_global);
          id(template_sensor_4).publish_state(template_sensor_4_global);
          id(template_sensor_5).publish_state(template_sensor_5_global);

globals:
  - id: template_sensor_1_global
    type:  bool
    restore_value: true
  - id: template_sensor_2_global
    type:  bool
    restore_value: true
  - id: template_sensor_3_global
    type:  bool
    restore_value: true
  - id: template_sensor_4_global
    type:  bool
    restore_value: true
  - id: template_sensor_5_global
    type:  bool
    restore_value: true
    
switch:
  - platform: gpio
    pin: GPIO4
    name: "${friendly_name}"
    restore_mode: ALWAYS_OFF
    internal: true

  - platform: restart
    name: "${friendly_name} Restart"
    
binary_sensor:     

  - platform: gpio                     
    pin: GPIO5                              
    id: real_sensor_temp
    internal: true
    on_multi_click:
      - timing:
        - ON for at most 0.5s
        - OFF for at least 0.5s
        then:
          - logger.log: "Single Clicked"
          - lambda: |-
              if (id(template_sensor_1).state) {
              id(template_sensor_1).publish_state(false);
              id(template_sensor_1_global) = false;
              } else { 
              id(template_sensor_1).publish_state(true);
              id(template_sensor_1_global) = true;
              }

              ESP_LOGD("TAG", "Global value is: %d", id(template_sensor_1_global));

      - timing:
          - ON for at most 0.5s
          - OFF for at most 0.5s
          - ON for at most 0.5s
          - OFF for at least 0.5s
        then:
          - logger.log: "Double Clicked"
          - lambda: |-
              if (id(template_sensor_2).state) {
              id(template_sensor_2).publish_state(false);
              id(template_sensor_1_global) = false;
              } else {
              id(template_sensor_2).publish_state(true);
              id(template_sensor_1_global) = true;
              }

              ESP_LOGD("TAG", "Global value is: %d", id(template_sensor_1_global));

      - timing:
          - ON for at most 0.5s
          - OFF for at most 0.5s
          - ON for at most 0.5s
          - OFF for at least 0.5s
          - ON for at most 0.5s
          - OFF for at least 0.5s
        then:
          - logger.log: "Tripple Clicked"
          - lambda: |-
              if (id(template_sensor_4).state) {
              id(template_sensor_4).publish_state(false);
              id(template_sensor_1_global) = false;
              } else {
              id(template_sensor_4).publish_state(true);
              id(template_sensor_1_global) = true;
              }

              ESP_LOGD("TAG", "Global value is: %d", id(template_sensor_1_global));
      
      - timing:
          - ON for at most 0.5s
          - OFF for at most 0.5s
          - ON for at most 0.5s
          - OFF for at least 0.5s
          - ON for at most 0.5s
          - OFF for at most 0.5s
          - ON for at most 0.5s
          - OFF for at least 0.5s
        then:
          - logger.log: "Quadruple Clicked"
          - lambda: |-
              if (id(template_sensor_5).state) {
              id(template_sensor_5).publish_state(false);
              id(template_sensor_1_global) = false;
              } else {
              id(template_sensor_5).publish_state(true);
              id(template_sensor_1_global) = true;
              }

              ESP_LOGD("TAG", "Global value is: %d", id(template_sensor_1_global));
              
      - timing:
          - ON for at least 0.8s 
          #- OFF for at least 0.5s
        then:
          - logger.log: "Long Clicked"
          - lambda: |-
              if (id(template_sensor_3).state) {
              id(template_sensor_3).publish_state(false);
              id(template_sensor_1_global) = false;
              } else {
              id(template_sensor_3).publish_state(true);
              id(template_sensor_1_global) = true;
              }

              ESP_LOGD("TAG", "Global value is: %d", id(template_sensor_1_global));
              
  - platform: template
    name: "${friendly_name} 1"
    id: template_sensor_1
  - platform: template
    name: "${friendly_name} 2"
    id: template_sensor_2
  - platform: template
    name: "${friendly_name} 3"
    id: template_sensor_3
  - platform: template
    name: "${friendly_name} 4"
    id: template_sensor_4
  - platform: template
    name: "${friendly_name} 5"
    id: template_sensor_5