Trouble with ESPhome yaml config and binary template sensors

So i recently installed ESPhome and trying to configure the sensors. The sensor is working but it alternates between Unavailable/Open/Closed. Not sure how to get rid of the Unavailable part.

Any idea would be most welcome. I used to use Tasmota and although i liked MQTT, tasmota itself was so unreliable i had to reboot it every day. So far so good with ESPhome.

For the sensor, only the bottom part will be relevant, but i paste the full code just in case anyone is interested.

esphome:
  name: sonoff433

esp8266:
  board: esp01_1m

ota:
  password: "0e7ec723f6c5f0fde6ec6e1ac76a8efe"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Sonoff433 Fallback Hotspot"
    password: "farN3nRtzhZp"
    
  #fast_connect: true

  # Optional manual IP
  manual_ip:
    static_ip: 192.168.50.63
    gateway: 192.168.50.1
    subnet: 255.255.255.0

captive_portal:

api:
  services:
    - service: send_rf_code
      variables:
        sync: int
        low: int
        high: int
        code: int
      then:
        - rf_bridge.send_code:
            sync: !lambda 'return sync;'
            low: !lambda 'return low;'
            high: !lambda 'return high;'
            code: !lambda 'return code;'
    - service: learn
      then:
        - rf_bridge.learn

uart:
  tx_pin: 1
  rx_pin: 3
  baud_rate: 19200

logger:
  baud_rate: 0

sensor:
  - platform: wifi_signal
    name: Sonoff RF Bridge Wifi Signal
    update_interval: 10s

  - platform: uptime
    name: Sonoff RF Bridge Uptime

binary_sensor:
  - platform: status
    name: Sonoff RF Bridge Status

# define the sensors
  - platform: template
    # ON -  Received RFBridge Code: sync=0x0000 low=0x0001 high=0x0010 code=0x000011
    # OFF - Received RFBridge Code:sync=0x0010 low=0x0011 high=0x0110 code=0x000111
    name: "Kitchen Window Sensor N"
    id: window_kitchen_n
    device_class: window

rf_bridge:
   on_code_received:
      then:
      - homeassistant.event:
          event: esphome.rf_code_received
          data:
            sync: !lambda 'char buffer [10];return itoa(data.sync,buffer,16);'
            low: !lambda 'char buffer [10];return itoa(data.low,buffer,16);'
            high: !lambda 'char buffer [10];return itoa(data.high,buffer,16);'
            code: !lambda 'char buffer [10];return itoa(data.code,buffer,16);'
            
      - if:
          condition:
            lambda: |-
              return data.code == 0xE5242C;
          then:
            - binary_sensor.template.publish:
                id: window_kitchen_n
                state: ON
            - delay: 5s
            - binary_sensor.template.publish:
                id: window_kitchen_n
                state: OFF

You have two binary sensors, which one is the problem?

Can you please also post your logs, showing the error.

It’s the template one, window_kitchen_n.

Not much in either HA logs or esphome main logs. Should i see something on the device log when the sensor triggers?

Your template sensor does not have a lambda

Cheers Nick. I’m still a noob on yaml - could you tell me what i need to add ?

Start here Template Binary Sensor — ESPHome

Why does it need a lambda though, i’m setting the state outside of the sensor ? The logs on the esphome output just show the status properly ON/OFF etc, there’s no unavailable there.