Skip to content

Instantly share code, notes, and snippets.

@peyanski
Created May 28, 2025 09:55
Show Gist options
  • Select an option

  • Save peyanski/d64a17c4b8e5c23040e78ff6fcd1b9fb to your computer and use it in GitHub Desktop.

Select an option

Save peyanski/d64a17c4b8e5c23040e78ff6fcd1b9fb to your computer and use it in GitHub Desktop.
# Uncomment the two lines below if you want to have a web server with sensor readings
#web_server:
# port: 80
sensor:
- platform: ultrasonic
id: Distance
trigger_pin: 5
echo_pin: 4
name: "Distance"
update_interval: 1s
pulse_time: 20us
timeout: 6m
# this is a template function to return percentage where 20cm = 100% , 6m = 0%
- platform: template
name: "% Full"
lambda: |-
if ((id(Distance).state) <= 0.200) {
return 100;
}
else if ((id(Distance).state) >= 6.000) {
return 0;
} else {
return (100 - ((id(Distance).state) - 0.200) / 5.80 * 100);
}
update_interval: 1s
@nehasjains
Copy link

20250908_193830
20250908_193842
20250908_221825

Hello,

Please find attached the screenshots, yaml and the log. I am using HASS OS v2025.9.1 with esphome as addon installed v2025.8.3

Using D1 Mini and AJ_SR04M. Did the wiring as per your pdf and also copied the code. But the sensor I only giving nan as output.

Kindly help what am I doing wrong here.

Very much hoping for your reply.

Thanks in advance.

Yaml

esphome:
  name: tank
  friendly_name: Tank


esp8266:
  board: esp01_1m
#  board: d1_mini

# Enable logging
logger:
  level: VERY_VERBOSE

# Enable Home Assistant API
api:
ota:
  - platform: esphome
  - 
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  power_save_mode: none
  fast_connect: True  
  manual_ip:
    static_ip: 192.168.1.77
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    dns1: 192.168.1.15

captive_portal:

# ---- Ultrasonic distance sensor ----
sensor:
  - platform: ultrasonic
    id: Distance 
    trigger_pin: 5
    echo_pin: 4
    name: "Distance"
    update_interval: 1s
    pulse_time: 20us
    timeout: 6m

# this is a template function to return percentage where  20cm = 100% , 6m = 0%
  - platform: template
    name: "% Full"
    lambda: |-
      if ((id(Distance).state) <= 0.200)  {
        return 100;
      }
      else if ((id(Distance).state) >= 6.000) {
        return 0;
      } else {
        return (100 - ((id(Distance).state) - 0.200) / 5.80 * 100);
      }
    update_interval: 1s

LOG

[22:20:01][D][ultrasonic.sensor:035]: 'Distance' - Distance measurement timed out!
[22:20:01][V][sensor:051]: 'Distance': Received new state nan
[22:20:01][D][sensor:103]: 'Distance': Sending state nan m with 2 decimals of accuracy
[22:20:01][V][sensor:051]: '% Full': Received new state -nan
[22:20:01][D][sensor:103]: '% Full': Sending state -nan  with 1 decimals of accuracy
[22:20:01][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:01][VV][api.service:012]:   key: 1240670792
[22:20:01][VV][api.service:012]:   state: nan
[22:20:01][VV][api.service:012]:   missing_state: NO
[22:20:01][VV][api.service:012]:  }
[22:20:01][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:01][VV][api.service:012]:   key: 283078860
[22:20:01][VV][api.service:012]:   state: -nan
[22:20:01][VV][api.service:012]:   missing_state: NO
[22:20:01][VV][api.service:012]:  }
[22:20:02][V][ultrasonic.sensor:032]: Echo took 6µs
[22:20:02][D][ultrasonic.sensor:035]: 'Distance' - Distance measurement timed out!
[22:20:02][V][sensor:051]: 'Distance': Received new state nan
[22:20:02][D][sensor:103]: 'Distance': Sending state nan m with 2 decimals of accuracy
[22:20:02][V][sensor:051]: '% Full': Received new state -nan
[22:20:02][D][sensor:103]: '% Full': Sending state -nan  with 1 decimals of accuracy
[22:20:02][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:02][VV][api.service:012]:   key: 1240670792
[22:20:02][VV][api.service:012]:   state: nan
[22:20:02][VV][api.service:012]:   missing_state: NO
[22:20:02][VV][api.service:012]:  }
[22:20:02][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:02][VV][api.service:012]:   key: 283078860
[22:20:02][VV][api.service:012]:   state: -nan
[22:20:02][VV][api.service:012]:   missing_state: NO
[22:20:02][VV][api.service:012]:  }
[22:20:03][V][ultrasonic.sensor:032]: Echo took 6µs
[22:20:03][D][ultrasonic.sensor:035]: 'Distance' - Distance measurement timed out!
[22:20:03][V][sensor:051]: 'Distance': Received new state nan
[22:20:03][D][sensor:103]: 'Distance': Sending state nan m with 2 decimals of accuracy
[22:20:03][V][sensor:051]: '% Full': Received new state -nan
[22:20:03][D][sensor:103]: '% Full': Sending state -nan  with 1 decimals of accuracy
[22:20:03][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:03][VV][api.service:012]:   key: 1240670792
[22:20:03][VV][api.service:012]:   state: nan
[22:20:03][VV][api.service:012]:   missing_state: NO
[22:20:03][VV][api.service:012]:  }
[22:20:03][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:03][VV][api.service:012]:   key: 283078860
[22:20:03][VV][api.service:012]:   state: -nan
[22:20:03][VV][api.service:012]:   missing_state: NO
[22:20:03][VV][api.service:012]:  }
[22:20:04][V][ultrasonic.sensor:032]: Echo took 6µs
[22:20:04][D][ultrasonic.sensor:035]: 'Distance' - Distance measurement timed out!
[22:20:04][V][sensor:051]: 'Distance': Received new state nan
[22:20:04][D][sensor:103]: 'Distance': Sending state nan m with 2 decimals of accuracy
[22:20:04][V][sensor:051]: '% Full': Received new state -nan
[22:20:04][D][sensor:103]: '% Full': Sending state -nan  with 1 decimals of accuracy
[22:20:04][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:04][VV][api.service:012]:   key: 1240670792
[22:20:04][VV][api.service:012]:   state: nan
[22:20:04][VV][api.service:012]:   missing_state: NO
[22:20:04][VV][api.service:012]:  }
[22:20:04][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:04][VV][api.service:012]:   key: 283078860
[22:20:04][VV][api.service:012]:   state: -nan
[22:20:04][VV][api.service:012]:   missing_state: NO
[22:20:04][VV][api.service:012]:  }
[22:20:05][V][ultrasonic.sensor:032]: Echo took 6µs
[22:20:05][D][ultrasonic.sensor:035]: 'Distance' - Distance measurement timed out!
[22:20:05][V][sensor:051]: 'Distance': Received new state nan
[22:20:05][D][sensor:103]: 'Distance': Sending state nan m with 2 decimals of accuracy
[22:20:05][V][sensor:051]: '% Full': Received new state -nan
[22:20:05][D][sensor:103]: '% Full': Sending state -nan  with 1 decimals of accuracy
[22:20:05][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:05][VV][api.service:012]:   key: 1240670792
[22:20:05][VV][api.service:012]:   state: nan
[22:20:05][VV][api.service:012]:   missing_state: NO
[22:20:05][VV][api.service:012]:  }
[22:20:05][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:05][VV][api.service:012]:   key: 283078860
[22:20:05][VV][api.service:012]:   state: -nan
[22:20:05][VV][api.service:012]:   missing_state: NO
[22:20:05][VV][api.service:012]:  }
[22:20:06][V][ultrasonic.sensor:032]: Echo took 1µs
[22:20:06][D][ultrasonic.sensor:035]: 'Distance' - Distance measurement timed out!
[22:20:06][V][sensor:051]: 'Distance': Received new state nan
[22:20:06][D][sensor:103]: 'Distance': Sending state nan m with 2 decimals of accuracy
[22:20:06][V][sensor:051]: '% Full': Received new state -nan
[22:20:06][D][sensor:103]: '% Full': Sending state -nan  with 1 decimals of accuracy
[22:20:06][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:06][VV][api.service:012]:   key: 1240670792
[22:20:06][VV][api.service:012]:   state: nan
[22:20:06][VV][api.service:012]:   missing_state: NO
[22:20:06][VV][api.service:012]:  }
[22:20:06][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:06][VV][api.service:012]:   key: 283078860
[22:20:06][VV][api.service:012]:   state: -nan
[22:20:06][VV][api.service:012]:   missing_state: NO
[22:20:06][VV][api.service:012]:  }
[22:20:07][V][ultrasonic.sensor:032]: Echo took 6µs
[22:20:07][D][ultrasonic.sensor:035]: 'Distance' - Distance measurement timed out!
[22:20:07][V][sensor:051]: 'Distance': Received new state nan
[22:20:07][D][sensor:103]: 'Distance': Sending state nan m with 2 decimals of accuracy
[22:20:07][V][sensor:051]: '% Full': Received new state -nan
[22:20:07][D][sensor:103]: '% Full': Sending state -nan  with 1 decimals of accuracy
[22:20:07][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:07][VV][api.service:012]:   key: 1240670792
[22:20:07][VV][api.service:012]:   state: nan
[22:20:07][VV][api.service:012]:   missing_state: NO
[22:20:07][VV][api.service:012]:  }
[22:20:07][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:07][VV][api.service:012]:   key: 283078860
[22:20:07][VV][api.service:012]:   state: -nan
[22:20:07][VV][api.service:012]:   missing_state: NO
[22:20:07][VV][api.service:012]:  }
[22:20:08][V][ultrasonic.sensor:032]: Echo took 6µs
[22:20:08][D][ultrasonic.sensor:035]: 'Distance' - Distance measurement timed out!
[22:20:08][V][sensor:051]: 'Distance': Received new state nan
[22:20:08][D][sensor:103]: 'Distance': Sending state nan m with 2 decimals of accuracy
[22:20:08][V][sensor:051]: '% Full': Received new state -nan
[22:20:08][D][sensor:103]: '% Full': Sending state -nan  with 1 decimals of accuracy
[22:20:08][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:08][VV][api.service:012]:   key: 1240670792
[22:20:08][VV][api.service:012]:   state: nan
[22:20:08][VV][api.service:012]:   missing_state: NO
[22:20:08][VV][api.service:012]:  }
[22:20:08][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:08][VV][api.service:012]:   key: 283078860
[22:20:08][VV][api.service:012]:   state: -nan
[22:20:08][VV][api.service:012]:   missing_state: NO
[22:20:08][VV][api.service:012]:  }
[22:20:09][V][ultrasonic.sensor:032]: Echo took 6µs
[22:20:09][D][ultrasonic.sensor:035]: 'Distance' - Distance measurement timed out!
[22:20:09][V][sensor:051]: 'Distance': Received new state nan
[22:20:09][D][sensor:103]: 'Distance': Sending state nan m with 2 decimals of accuracy
[22:20:09][V][sensor:051]: '% Full': Received new state -nan
[22:20:09][D][sensor:103]: '% Full': Sending state -nan  with 1 decimals of accuracy
[22:20:09][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:09][VV][api.service:012]:   key: 1240670792
[22:20:09][VV][api.service:012]:   state: nan
[22:20:09][VV][api.service:012]:   missing_state: NO
[22:20:09][VV][api.service:012]:  }
[22:20:09][VV][api.service:012]: send_message sensor_state_response: SensorStateResponse {
[22:20:09][VV][api.service:012]:   key: 283078860
[22:20:09][VV][api.service:012]:   state: -nan
[22:20:09][VV][api.service:012]:   missing_state: NO
[22:20:09][VV][api.service:012]:  }
[22:20:10][V][ultrasonic.sensor:032]: Echo took 6µs
[22:20:10][D][ultrasonic.sensor:035]: 'Distance' - Distance measurement timed out!
[22:20:10][V][sensor:051]: 'Distance': Received new state nan
[22:20:10][D][sensor:103]: 'Distance': Sending state nan m with 2 decimals of accuracy
[22:20:10][V][sensor:051]: '% Full': Received new state -nan
[22:20:10][D][sensor:103]: '% Full': Sending state -nan  with 1 decimals of accuracy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment