Setting initial state for binary_sensor

I have a xiaomi aqara leak sensor
I use it to detect when my dog’s water bowl is empty (wet=full/dry=empty)
The problem is the initial state of the sensor which is ‘dry’ so I have to manually change it
I looked up how to set ‘initial_state’ for binary sensor but didn’t find any documentation for this
Any ideas?

Do you mean when you restart home assistant, it always resets to ‘dry’?

I think what I’d do in this case is have an input_boolean to hold the real value and just keep the xiaomi sensor in the background, Then have an automation triggered by the xiaomi sensor state to update the state of the input_boolean to match the sensor. The input_boolean will hold its value across a restart.

If you really wanted a binary_sensor in the end, you could create a template-based one that takes its value from the input_boolean.

Could you use an automation to update the sensor state when HA starts?

    platform: homeassistant
    event: start
    - service: homeassistant.update_entity
      entity_id: binary_sensor.xiaomi_aqara_leak_sensor

I’m not familiar with that type of sensor so I don’t know how its state is reported to HA.

Thank you guys.
The second suggestion looks a bit cleaner, I’ve come across this solution so I should be able to override the sensor state. I’ll report back!

Update: it worked. I’ve used the python script to set the sensor on HA startup.
Thank you all

1 Like

Great to hear! :slightly_smiling_face:

I’d be interested in seeing the script if you wouldn’t mind sharing it. I’ve got some binary_sensors (camera motion) that I’m currently resetting with CURL commands but your solution seems more elegant. :nerd_face:

I’ve taken it from this repo


  - alias: 'Init water bowl sensor to wet'
    initial_state: 'on'
    hide_entity: True
      platform: homeassistant
      event: start
      - service: python_script.set_state
          entity_id: 'binary_sensor.bla'
          state: 'on'
1 Like

Oh OK, I wasn’t aware of Very cool!

Did you put it in your HA config folder?

EDIT: Nevermind, I spotted it…

Thanks again.

1 Like

I have binary sensor template below and I try to put initial value switch.home_security=off when home-assistant start, but it doesn’t work
Anybody can tell me why?


- platform: command_line
      command_on: "x10cm15a rfsec 0xA8 arm_away_min"
      command_off: "x10cm15a rfsec 0xA8 disarm"


- platform: template
      friendly_name: "Status Home Presence"
      device_class: presence
      value_template: "{{ is_state('switch.home_security', 'off') }}"


  alias: Switch Initial State Configuration
  initial_state: true
  hide_entity: true
    - platform: homeassistant
      event: start
    - service: switch.turn_off
      entity_id: switch.home_security
    - service: python_script.set_state
        entity_id: binary_sensor.status_home_presence
        state: 'on'