Change default state of entity: Unknown to 0

Hello all!

I would like to change status 'Unknown" of battery entity (Filament Stan baterii) to 0 since it makes problem with other automations. I tried filters and lambdas but I can’t get it to work. My code:

esphome:
  name: bramka-bluetooth-pietro
  platform: ESP32
  board: esp32dev

wifi:
  ssid: "xxx"
  password: "xxx"

# Enable logging
logger:

# Enable Home Assistant API
api:
#  password: "xxx"

ota:
  password: "xxx"
  
esp32_ble_tracker:
#  scan_parameters:
#    duration: 10s
#  on_ble_service_data_advertise:
#    - mac_address: 
#      service_uuid: FE95
#      then:
#        - lambda: |-
#            id(kettle_temp).publish_state(float(x[16]));
#            if(x[15] == 01) {
#              id(kettle_mode).publish_state(true);
#            } else {
#              id(kettle_mode).publish_state(false);
#            }

binary_sensor:
  - platform: ble_presence
    mac_address: 
    name: "Marcin"
  - platform: ble_presence
    mac_address: 
    name: "Gosia"    
#  - platform: template
#    id: kettle_mode
#    name: "Tryb Czajnik"
sensor:
  - platform: xiaomi_hhccjcy01
    mac_address: 
    temperature:
      name: "Temperatura"
    moisture:
      name: "Wilgotność"
    illuminance:
      name: "Nasłonecznienie"
    conductivity:
      name: "Żyzność gleby"
  #  battery_level:
  #    name: "Stan baterii"
  - platform: xiaomi_lywsdcgq
    mac_address: 
    temperature:
      name: "Temperatura Terrarium"
    humidity:
      name: "Wilgotność Terrarium"
    battery_level:
      name: "Stan baterii Terrarium"
  - platform: xiaomi_lywsd02
    mac_address: '
    temperature:
      name: "Temperatura Zegar"
    humidity:
      name: "Wilgotność Zegar"
  - platform: xiaomi_lywsd03mmc
    mac_address:
    bindkey: 
    temperature:
      name: "Filament Temperatura"
    humidity:
      name: "Filament Wilgotność"
    battery_level:
      id: battery_level
      name: "Filament Stan baterii"
#      filters:
#        - lambda: |-
#                    if ((id(battery_level).state == 'Unknown')) {
#                    id(battery_level).publish_state(0);
#                    return {};}      
  - platform: xiaomi_mhoc401
    mac_address: 
    bindkey: 
    temperature:
      name: "Strych Temperatura"
    humidity:
      name: "Strych Wilgotność"
    battery_level:
      name: "Strych Stan baterii"
#  - platform: template
#    id: kettle_temp
#    name: "Temperatura Czajnik"
#    unit_of_measurement: '°C'
#    accuracy_decimals: 0
#    icon: "mdi:thermometer"

Thank you in advance for any info and help!

Hi, this is an old post but I found it while having the same issue.

For me, the solution was to use isNaN() function. I check the value of x (current value of the input sensor) so it’s evaluated before the sensor state is published.

binary_sensor:
  - platform: homeassistant
    entity_id: switch.heating_2_heat_request
    publish_initial_state: true
    filters:
      - lambda: |-
               if (isnan(x)) {
                 return false;}   
               else {
                 return x;} 
    filters:
      - lambda: if (isnan(x)) {  return 0; } return x;