So I got myself an Ultrasonic Sensor from DFRobot.
In creating the YAML I borrowed some entity configuration from the Athom Tech Garage Door Openers yaml code I installed a while ago.
I have the ultrasonic sensor connected to an ESP8266 board. I’m using the 4 pin from the bottom right of the pin out diagram linked here.
I even tried swapping the Rx & Tx wires thinking I might have mixed them up. All the other entities show up but NOT the distance (which is the whole purpose of the device).
Where did I go wrong in the YAML code?
esphome:
name: esp-zz-os-well-8266
friendly_name: ESP_ZZ_OS_Well_8266
esp8266:
board: esp01_1m
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX="
ota:
- platform: esphome
wifi:
networks:
- ssid: !secret wifi_ssid
password: !secret wifi_password
- ssid: !secret 2_wifi_ssid
password: !secret 2_wifi_password
manual_ip:
static_ip: 192.168.0.232
gateway: 192.168.0.1
subnet: 255.255.255.0
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
web_server:
version: 2
captive_portal:
uart:
tx_pin: GPIO1
rx_pin: GPIO3
baud_rate: 9600
sensor:
- platform: "a01nyub"
id: distance_sensor
entity_category: diagnostic
name: "Distance"
unit_of_measurement: 'cm'
icon: "mdi:water-percent"
device_class: distance
state_class: measurement
accuracy_decimals: 2
filters:
- throttle: 60s
- heartbeat: 60s
- quantile:
window_size: 7
send_every: 4
send_first_at: 3
- platform: uptime
name: "Uptime Sensor"
id: uptime_sensor
entity_category: diagnostic
internal: true
- platform: wifi_signal
name: "WiFi Signal dB"
id: wifi_signal_db
update_interval: 60s
entity_category: "diagnostic"
- platform: copy
source_id: wifi_signal_db
name: "WiFi Signal Percent"
filters:
- lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
unit_of_measurement: "Signal %"
entity_category: "diagnostic"
device_class: ""
button:
- platform: restart
name: "Restart"
entity_category: config
- platform: factory_reset
name: "Factory Reset"
id: Reset
entity_category: config
- platform: safe_mode
name: "Safe Mode"
internal: false
entity_category: config
light:
- platform: status_led
name: "Status LED"
id: wifi_led
disabled_by_default: true
pin:
inverted: true
number: GPIO0
text_sensor:
- platform: wifi_info
ip_address:
name: "IP Address"
entity_category: diagnostic
ssid:
name: "Connected SSID"
entity_category: diagnostic
mac_address:
name: "Mac Address"
entity_category: diagnostic
# Creates a sensor showing when the device was last restarted
- platform: template
name: 'Last Restart'
id: device_last_restart
icon: mdi:clock
entity_category: diagnostic
# Creates a sensor of the uptime of the device, in formatted days, hours, minutes and seconds
- platform: template
name: "Uptime"
entity_category: diagnostic
lambda: |-
int seconds = (id(uptime_sensor).state);
int days = seconds / (24 * 3600);
seconds = seconds % (24 * 3600);
int hours = seconds / 3600;
seconds = seconds % 3600;
int minutes = seconds / 60;
seconds = seconds % 60;
if ( days > 3650 ) {
return { "Starting up" };
} else if ( days ) {
return { (String(days) +"d " + String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() };
} else if ( hours ) {
return { (String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() };
} else if ( minutes ) {
return { (String(minutes) +"m "+ String(seconds) +"s").c_str() };
} else {
return { (String(seconds) +"s").c_str() };
}
icon: mdi:clock-start
time:
- platform: sntp
id: sntp_time
# Define the timezone of the device
timezone: America/Halifax
# Change sync interval from default 5min to 6 hours (or as set in substitutions)
#update_interval: ${sntp_update_interval}
# Set specific sntp servers to use
servers:
- 0.pool.ntp.org
- 1.pool.ntp.org
- 2.pool.ntp.org
# Publish the time the device was last restarted
on_time_sync:
then:
# Update last restart time, but only once.
- if:
condition:
lambda: 'return id(device_last_restart).state == "";'
then:
- text_sensor.template.publish:
id: device_last_restart
state: !lambda 'return id(sntp_time).now().strftime("%a %d %b %Y - %I:%M:%S %p");'