Custom occupancy Sensor for Car Detection

Hi all,

i’ve a little ESP8266 (with ESPHome) on my Car parking where i’m using two SR04T sensors. The current configuration is this one:

- platform: template
  sensors:
    my_car:
      friendly_name: "My Car"
      device_class: occupancy
      icon_template: >
        {% if is_state("sensor.sensore_auto_mia", "unknown") %}
        mdi:car-off
        {% else %}
        mdi:car
        {% endif %}
      value_template: >-
        {{ states('sensor.sensore_auto_mia')|float < 1 }}
    wife_car:
      friendly_name: "Wife Car"
      device_class: occupancy
      icon_template: >
        {% if states('sensor.sensore_auto_moglie')|float > 1 %}
        mdi:car-off
        {% else %}
        mdi:car
        {% endif %}
      value_template: >-
        {{ states('sensor.sensore_auto_moglie')|float < 1 }}

The problem is with the sensor sensor.sensore_auto_mia, because without the car under the sensor ESPHome report the value to Uknown instead of a real measure.

Most of the time, the reading of this sensor is false (i’ve to check the real item sensor instead of template sensor to check if car is on place or not).

Any hints?

Thanks!

Please format your post correctly.

is it ok now?

thanks

Yep. Much better.

What sort of sensor?

Can we see the esphome config?

here is the ESPHome configuration:

esphome:
  name: esp8266_ext_garage
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "REMOVED"
  password: "REMOVED"
  reboot_timeout: 15s

binary_sensor:
  - platform: status
    name: "Stato ESP02"
  - platform: gpio
    pin:
      number: D6
      inverted: True
      mode: INPUT_PULLUP
    name: "Pulsante Garage"
    on_press:
      then:
        - switch.turn_on: relay_1
    on_release:
      then:
        - switch.turn_off: relay_1

switch:
  - platform: gpio
    pin: D5
    name: "Luce Garage"
    id: relay_1
  - platform: restart
    name: "Riavvia ESP02"

sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D0
    update_interval: 5s
    name: "Sensore Auto Moglie"
  - platform: ultrasonic
    trigger_pin: D3
    echo_pin: D2
    update_interval: 5s
    name: "Sensore Auto Mia"
  - platform: uptime
    name: "Uptime ESP02"
  - platform: wifi_signal
    name: "Segnale WiFi ESP02"
    update_interval: 60s

text_sensor:
  - platform: version
    name: "ESPHome Version"

logger: