Water Tank Level and Water Volume with ESPHome

Ok I try to put 2 cm, but the other parameters are correct? What does that -100 at the end of the formula indicate?

    filters:
      - filter_out: nan
      - lambda: return ((((x*0,25)-2)-(11-2))/(11-2))*-100;

The result is always -100%

This is my code so you can compare:

  - platform: ultrasonic
    trigger_pin: D6
    echo_pin: D5
    name: 'Distancia al agua' # Distance to water
    unit_of_measurement: 'm'
    accuracy_decimals: 1
    update_interval: 10s
    pulse_time: 20us
    timeout: 2.0m
    filters:
      - filter_out: nan

- platform: ultrasonic
    trigger_pin: D6
    echo_pin: D5
    name: "Nivel cisterna"
    unit_of_measurement: "%"
    accuracy_decimals: 0
    update_interval: 10s
    filters:
        ## Tank Depth = all the way from the cover where the sensor is mounted to the bottom = 75
        ## Sensor Value = x this is will stay the same as this is the reading from the sensor
        ## Excluded Distance = EX which is the distance I have on top from the sensor which is mounted to the cover to the highest level the water can reach = 21
        ## return ((((x*100)-EX)-(TD-EX))/(TD-EX))*-100
      - lambda: return ((((x*100)-21)-(75-21))/(75-21))*-100;
      - sliding_window_moving_average:
          window_size: 10 #creates a moving average of the last 10 values
          send_every: 1 #sends the moving average at every measurement (but only if it passes the delta filter below))
          send_first_at: 1 #after startup immediately start sending the result rather than wait for the first 10 measurements
      - delta : 0.05 # only send the result if the level difference with the last sent result is higher than this   
      - filter_out: nan

What values are you getting for X?

1 Like

The tank I use for the test is 11.0 cm high, the maximum water level is 9.0 cm (+ 2.0 cm of space between sensor and max level = 11 cm tot)

With max water level (9 cm), in the esphome log it indicates 0.02 m (2 cm).

With min water level (0 cm), in the esphome log it indicates 0.09 m (9 cm).

This is my sensor code, now the level is at max but on homeassistant it returns -0.9

here my configuration with only one sensor reading.

sensor:
  - platform: ultrasonic
    trigger_pin: D6
    echo_pin: D5
    name: $ha_prefix Meter
    id: ultrasonic_meter
    state_class: measurement
    unit_of_measurement: 'm'
    accuracy_decimals: 2
    update_interval: 30min
    pulse_time: 20us
    timeout: 2.5m
    filters:
      - filter_out: nan
  
  - platform: copy
    name: $ha_prefix Percent
    source_id: ultrasonic_meter
    state_class: measurement
    unit_of_measurement: '%'
    accuracy_decimals: 1
    filters:
      # height from sensor to empty tank MAX: 228cm
      # height from sensor to full tank OFFSET: 38cm
      # sensor reading: x
      # used = x - OFFSET
      # remaining = MAX - used 
      # Full = (MAX - OFFSET ) = 190 cm
      # percent = 1 - (remaining / Full) * 100
      - lambda: return (1-((x-.38)/1.9))*100;

Bonjour peux tu partager ton code yaml sous esp home svp ?

Guys I need your help!
I’m trying to complete a similar project with temperature and ultrasonic senors.

A. Temp sensors (3x) will be attached to the storage tank of hot water for observing

  1. the temp of stored heated water
  2. the temp of water circulating in the diesel boiler system (common with house heating)
  3. the temp of water circulating in the solar panel system (there is an autonomous system with differential switch and a motor attached to it for the circulation).
    No any automation will be connected to this system, it will be exclusively for keeping me informed what is going on.

B. ultrasonic sensor to measure the level of the underground potable water tank, 6,000 ltrs, almost cube in shape, with well known dimensions. Filling of the tank, the submerged pump and Hi/Lo limits are operated from an autonomous system. I want to follow up the level, remaining quantity in it and consequently the overall consumption bu this is not the main object.

for the purpose of this, Im using:
Waterproof Ultrasonic Module JSN-SR04T
DS18B20 Temperature Sensor Module Kit Waterproof
ESP32 Development Board ESP32-WROOM-32U

So far the temp part is working but the level not.

[17:52:00][D][ultrasonic.sensor:036]: 'Water Tank Volume' - Distance measurement timed out!

this is the permanent error and presented on pre-defined interval of configuration
This is how is connected
5v----->5v of ESP32
RX---->GPIO1 of ESP32
TX----->GPIO3 of ESP32
GND—>GND of ESP32

My yaml file is

 - platform: ultrasonic
    trigger_pin: GPIO1
    echo_pin: GPIO3
    name: "Water Tank Level"
    unit_of_measurement: "%"
    accuracy_decimals: 0
    update_interval: 5s
    filters:
      - lambda: return ((((x*100)-20)-(123-20))/(123-20))*-100;
      - filter_out: nan
      
  - platform: ultrasonic
    trigger_pin: GPIO1
    echo_pin: GPIO3
    update_interval: 5s
    name: "Water Tank Volume"
    unit_of_measurement: "l"
    accuracy_decimals: 0
    filters:
      - lambda: return ((((x*100)-20)-(123-20))/(123-20))*-1000;
      - filter_out: nan

**dont bother with lambda, its not adjusted yet for my tank !

Any idea what else to try? other GPIO? what?
It is convenient for me to have only one ESP board in that place for simplicity reasons and the external antenna that I’m using with this board.

Thanks

Only use the same pins one time in your config - that would be for the ultrasonic platform. If you want to calculate other values (Volume etc.) make use of copy or template sensors instead :bulb:

Yes, both GPIO 1 and 3 have limitations and should be avoided. ESP32 got plenty of GPIO’s that are good for Input/Output use, check here for more info:

1 Like

since the day that posted here, I found another thread for my specific sensor suggesting the use of uart setup and the low power serial mode.
AJ_SR04M is the exact model and here is my adventure on this.
At the end of the day I have a sensor that is working until the reboot of the ESP board! Then I have to disconnect either pin Tx/Rx of the sensor before commence working again!
It’s insane for me that I have no any coding background.

That’s because you use pins you shouldn’t use :bulb:


(avoid any GPIO’s with red and yellow boxes and go for the double green :ok: instead)

It’s okay - no coding skills required as esphome has a “no code” approach :wink:

I have tried a lot of other couple of pins, results are the same on rebot.

If you have time and stamina, tell me which board, which gpios and wait for my feedback!
i have a ESP32-WROOM-32U, a ESP32-cam, a D1 ESP32 and few ESP8266

Did you check before that the lot of other pins you choose don’t (also) have limitations? I saw that that the link to the pinout reference also was posted in your other adventure thread 3 days ago (and today again).

Community guys helped a lot, somebody directed me to this guide for choosing GPIO. I have use even the same pins with them but with different results.
Tonight Im going to try tasmota on D1 and the one sensore in original mode.Really I dont know what eelse to try!

I also have the same problem did you get any solution of the fluctuations?

I used the hcsr04 and the jsn sr04t both have the same problem.

If you use the same pins (and yaml) you should be able to reproduce the setup from someone else.

Watch out because the D1 mini is a esp82xx which has other (and less) usable pins that the esp32 (‘classic’) you used before.

Sure? Any logs, yamls, wiring diagram etc.? :man_shrugging:

How to help us help you - or How to ask a good question

image

I did too and they work a treat with esphome :ok_hand:

Another point that I Have noticed for a responsive sensor is the grounding! ESP board and my particular sensor should have common source of power and grounding, not powering the sensor from the board or similar.
I have noticed that in oposited case the sensor is halting or freezing or delaying before updating measurements.
The issue of reset the pins tx/rx before giving measurements after reboot is there.
Now, D1 mini WROOM board, pins 22 and 21

uart:
  id: uart_bus
  tx_pin: GPIO22
  rx_pin: GPIO21
  baud_rate: 9600
  stop_bits: 1

PS confusion is caused for my case, I’m posting as well to this thread which is related to my issues and some persons are posting both sides.

You can use 2 power sources, one for the esp and one for the sensor.
But you MUST wire all GNDs together.

You should send pics of your esp boards and wiring


This is the ESP32 cam that used to various tests, removed from a camera just for tests.


This is the ESP32-WROOM-32U that is intented to be used for my project (due to its external antena + dallas sensors)


This is the board of the sensor where is soldered 47KΩ resistor, activating low power serial mode as per this link


another clear view of the board with hook up cables Yellow=GND, Purple=5V, Green=Tx, Blue=Rx


Version of sensor is AJ-SR04M


This is how powering the boards, Im using this step down board only for the pins, no conversion of voltage. The pair green-brown on the left is 5v and the other pair coming from sensor board. Sensor Green/Tx==>GPIO21, Sensor Blue/Rx==>GPIO22, Ill post all config files on next post.


ESP32 wroom D1 mini in operation


Cables of 5v and sensor to the ESP32 wroom D1 mini


The sensor itself.

Next post my config files

1 Like


Sensor is reporting every sec the measured distance although is not stable, I have seen more accurate, could be because of its position during the photo session.

/config/esphome/AJ_SR04M_Sensor.h

#include "esphome.h"

class AJ_SR04M_Sensor : public PollingComponent, public UARTDevice, public Sensor {
  public:

    AJ_SR04M_Sensor(UARTComponent *parent) : PollingComponent(1000), UARTDevice(parent) {}

    void update() override {

      byte frame[5];
      int pos = 0;
      float value = 0.0;

      //write(0x00); // Try this
      write(0x01); // Try this
      //write(0x01); // Try this
      while (available()) {

        frame[pos] = read();

        pos++;

        if (pos == 4) {

	  if ((frame[0] == 0xFF) && (frame[4] == 0x00) && (((frame[0] + frame[1] + frame[2]) & 0x00ff) == frame[3])){
            value = ((frame[1] << 8) + frame[2]) / 10.0;
            publish_state(value);
          }
          break;


        }
      }
    }
};

The page of the senor in previous post advising the use of

write(0x01); 

write(0x0);
write(0x55);
have been tested with unstable and confusing results

esphome-web-a7f4a8.yaml

esphome:
  name: esphome-web-a7f4a8
  friendly_name: ESPHome Web a7f4a8
  includes:
    - AJ_SR04M_Sensor.h

uart:
  id: uart_bus
  tx_pin: GPIO22
  rx_pin: GPIO21
  baud_rate: 9600
  stop_bits: 1

esp32:
  board: esp32dev
  framework:
    type: arduino

sensor:
- platform: custom
  lambda: |-
    auto my_sensor = new AJ_SR04M_Sensor(id(uart_bus));
    App.register_component(my_sensor);
    return {my_sensor};
  sensors:
     unit_of_measurement: cm
     accuracy_decimals: 1
     name: "Distance"

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

ota:
  password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "xxxxxxxxxxxx"
    password: "xxxxxxxxxxxxxxxxxxx"

captive_portal:
    

So this is my situation, its working but when the ESP board is rebooted I should removed one or both tx/rx pin before resuming reporting mesurements to ESPHome (not available is the message). In the logs is appearing nothing until the remove/re-insert the pins.
Then going back to measuring operation.

How are you powering this module?

Connect the esp32 via a USB cable and power the sensor using the pins on the esp32.
Use some program like Esp-Flaher to flash the firmware, wait until you get some readings, reset the esp32 using the button and post the log.

Measurements are taken in the shape of a cone. Then try to aim for a wall that doesn’t have any nearby obstacles.

My sensor is pretty constant, but for some reason it sends a measurement of approximately 2.xx meters, so I can only use it if the distance is less than 2 meters.

Log with some restart:

Using 'COM6' as serial port.
Connecting....
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting......
Detecting chip type... ESP32
Connecting.........

Chip Info:
 - Chip Family: ESP32
 - Chip Model: ESP32-D0WDQ5 (revision 1)
 - Number of Cores: 2
 - Max CPU Frequency: 240MHz
 - Has Bluetooth: YES
 - Has Embedded Flash: NO
 - Has Factory-Calibrated ADC: YES
 - MAC Address: XX:XX:XX:XX:XX:XX
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
 - Flash Size: 4MB
 - Flash Mode: dio
 - Flash Frequency: 40MHz
Erasing flash (this may take a while)...
Chip erase completed successfully in 9.6s
Flash will be erased from 0x00001000 to 0x00005fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x0000e000 to 0x0000ffff...
Flash will be erased from 0x00010000 to 0x000f3fff...
Compressed 17088 bytes to 11848...
Writing at 0x00001000... (100 %)
Wrote 17088 bytes (11848 compressed) at 0x00001000 in 0.5 seconds (effective 271.6 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 129...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (129 compressed) at 0x00008000 in 0.0 seconds (effective 491.5 kbit/s)...
Hash of data verified.
Compressed 8192 bytes to 47...
Writing at 0x0000e000... (100 %)
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.1 seconds (effective 787.0 kbit/s)...
Hash of data verified.
Compressed 932304 bytes to 618748...
Writing at 0x00010000... (2 %)
Writing at 0x00019bc6... (5 %)
Writing at 0x00023075... (7 %)
Writing at 0x0002766a... (10 %)
Writing at 0x0002e744... (13 %)
Writing at 0x0003ab2a... (15 %)
Writing at 0x00040561... (18 %)
Writing at 0x000463ed... (21 %)
Writing at 0x0004bb5d... (23 %)
Writing at 0x00051555... (26 %)
Writing at 0x00056d34... (28 %)
Writing at 0x0005c4f6... (31 %)
Writing at 0x00061c9b... (34 %)
Writing at 0x0006738f... (36 %)
Writing at 0x0006cc6b... (39 %)
Writing at 0x000720bf... (42 %)
Writing at 0x000771c6... (44 %)
Writing at 0x0007c635... (47 %)
Writing at 0x000818a5... (50 %)
Writing at 0x00086975... (52 %)
Writing at 0x0008bac1... (55 %)
Writing at 0x00090c08... (57 %)
Writing at 0x00097401... (60 %)
Writing at 0x0009cf30... (63 %)
Writing at 0x000a269c... (65 %)
Writing at 0x000a79d4... (68 %)
Writing at 0x000accab... (71 %)
Writing at 0x000b200d... (73 %)
Writing at 0x000b767d... (76 %)
Writing at 0x000bcda4... (78 %)
Writing at 0x000c2715... (81 %)
Writing at 0x000c8370... (84 %)
Writing at 0x000cdcbb... (86 %)
Writing at 0x000d4caf... (89 %)
Writing at 0x000dee21... (92 %)
Writing at 0x000e45db... (94 %)
Writing at 0x000e9da4... (97 %)
Writing at 0x000ef2b6... (100 %)
Wrote 932304 bytes (618748 compressed) at 0x00010000 in 14.8 seconds (effective 502.4 kbit/s)...
Hash of data verified.

Leaving...
Hard Resetting...
Hard resetting via RTS pin...
Done! Flashing is complete!

Showing logs:
[15:46:19][I][logger:262]: Log initialized
[15:46:19][C][ota:469]: There have been 0 suspected unsuccessful boot attempts.
[15:46:19][D][esp32.preferences:114]: Saving 1 preferences to flash...
[15:46:19][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[15:46:19][I][app:029]: Running through setup()...
[15:46:19][C][uart.arduino_esp32:077]: Setting up UART...
[15:46:19][    45][E][HardwareSerial.cpp:530] setRxBufferSize(): RX Buffer must be higher than 128.
[15:46:19]
[15:46:19][C][wifi:038]: Setting up WiFi...
[15:46:19][C][wifi:048]: Starting WiFi...
[15:46:19][C][wifi:049]:   Local MAC: XX:XX:XX:XX:XX:XX
[15:46:19][D][wifi:425]: Starting scan...
[15:46:19][D][sensor:094]: 'Distance': Sending state 32.30000 cm with 2 decimals of accuracy
[15:46:20][D][sensor:094]: 'Distance': Sending state 32.30000 cm with 2 decimals of accuracy
[15:46:20][D][sensor:094]: 'Distance': Sending state 32.30000 cm with 2 decimals of accuracy
[15:46:20][D][sensor:094]: 'Distance': Sending state 32.30000 cm with 2 decimals of accuracy
[15:46:20][D][sensor:094]: 'Distance': Sending state 32.30000 cm with 2 decimals of accuracy
[15:46:20][D][sensor:094]: 'Distance': Sending state 32.30000 cm with 2 decimals of accuracy
[15:46:20][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:21][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:21][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:21][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:21][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:21][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:21][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:21][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:22][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:22][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:22][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:22][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:22][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:22][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:22][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:23][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:23][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:23][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:23][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:23][D][sensor:094]: 'Distance': Sending state 226.20000 cm with 2 decimals of accuracy
[15:46:23][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:24][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:24][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:24][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:24][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:24][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:24][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:24][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:25][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:25][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:25][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:25][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:25][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:25][D][sensor:094]: 'Distance': Sending state 220.80000 cm with 2 decimals of accuracy
[15:46:25][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:26][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:26][D][wifi:440]: Found networks:
[15:46:26][D][wifi:485]:     Channel: 9
[15:46:26][D][wifi:486]:     RSSI: -50 dB
[15:46:26][D][wifi:485]:     Channel: 11
[15:46:26][D][wifi:486]:     RSSI: -74 dB
[15:46:26][D][sensor:094]: 'Distance': Sending state 230.70000 cm with 2 decimals of accuracy
[15:46:26][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:26][D][sensor:094]: 'Distance': Sending state 225.39999 cm with 2 decimals of accuracy
[15:46:26][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:27][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:27][C][wifi:379]:   Local MAC: XX:XX:XX:XX:XX:XX
[15:46:27][C][wifi:380]:   SSID: 'JUNIOR 2.4B'
[15:46:27][C][wifi:381]:   IP Address: 192.168.0.238
[15:46:27][C][wifi:383]:   BSSID: XX:XX:XX:XX:XX:XX
[15:46:27][C][wifi:384]:   Hostname: 'ultrasonic-sr04m'
[15:46:27][C][wifi:386]:   Signal strength: -52 dB ▂▄▆█
[15:46:27][C][wifi:390]:   Channel: 9
[15:46:27][C][wifi:391]:   Subnet: 255.255.255.0
[15:46:27][C][wifi:392]:   Gateway: 192.168.0.1
[15:46:27][C][wifi:393]:   DNS1: 192.168.0.6
[15:46:27][C][wifi:394]:   DNS2: 127.0.0.1
[15:46:27][D][wifi:566]: Disabling AP...
[15:46:27][C][web_server:116]: Setting up web server...
[15:46:27][C][ota:093]: Over-The-Air Updates:
[15:46:27][C][ota:094]:   Address: ultrasonic-sr04m.local:3232
[15:46:27][C][ota:097]:   Using Password.
[15:46:27][C][api:025]: Setting up Home Assistant API server...
[15:46:27][I][app:062]: setup() finished successfully!
[15:46:27][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:27][I][app:102]: ESPHome version 2023.6.3 compiled on Jul  3 2023, 15:37:57
[15:46:27][C][wifi:543]: WiFi:
[15:46:27][C][wifi:379]:   Local MAC: XX:XX:XX:XX:XX:XX
[15:46:27][C][wifi:380]:   SSID: 'JUNIOR 2.4B'
[15:46:27][C][wifi:381]:   IP Address: 192.168.0.238
[15:46:27][C][wifi:383]:   BSSID: XX:XX:XX:XX:XX:XX
[15:46:27][C][wifi:384]:   Hostname: 'ultrasonic-sr04m'
[15:46:27][C][wifi:386]:   Signal strength: -52 dB ▂▄▆█
[15:46:27][C][wifi:390]:   Channel: 9
[15:46:27][C][wifi:391]:   Subnet: 255.255.255.0
[15:46:27][C][wifi:392]:   Gateway: 192.168.0.1
[15:46:27][C][wifi:393]:   DNS1: 192.168.0.6
[15:46:27][C][wifi:394]:   DNS2: 127.0.0.1
[15:46:27][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:27][C][logger:301]: Logger:
[15:46:27][C][logger:302]:   Level: DEBUG
[15:46:27][C][logger:303]:   Log Baud Rate: 115200
[15:46:27][C][logger:305]:   Hardware UART: UART0
[15:46:27][C][uart.arduino_esp32:124]: UART Bus 1:
[15:46:27][C][uart.arduino_esp32:125]:   TX Pin: GPIO22
[15:46:27][C][uart.arduino_esp32:126]:   RX Pin: GPIO21
[15:46:27][C][uart.arduino_esp32:128]:   RX Buffer Size: 4
[15:46:27][C][uart.arduino_esp32:130]:   Baud Rate: 9600 baud
[15:46:27][C][uart.arduino_esp32:131]:   Data Bits: 8
[15:46:27][C][uart.arduino_esp32:132]:   Parity: NONE
[15:46:27][C][uart.arduino_esp32:133]:   Stop bits: 1
[15:46:27][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:27][C][captive_portal:088]: Captive Portal:
[15:46:27][C][web_server:161]: Web Server:
[15:46:27][C][web_server:162]:   Address: ultrasonic-sr04m.local:80
[15:46:27][C][mdns:108]: mDNS:
[15:46:27][C][mdns:109]:   Hostname: ultrasonic-sr04m
[15:46:27][C][ota:093]: Over-The-Air Updates:
[15:46:27][C][ota:094]:   Address: ultrasonic-sr04m.local:3232
[15:46:27][C][ota:097]:   Using Password.
[15:46:27][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:27][C][api:138]: API Server:
[15:46:27][C][api:139]:   Address: ultrasonic-sr04m.local:6053
[15:46:27][C][api:141]:   Using noise encryption: YES
[15:46:27][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:27][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:28][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:28][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:28][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:28][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:28][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:28][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:28][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:29][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:29][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:29][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:29][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:29][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:29][D][sensor:094]: 'Distance': Sending state 253.20000 cm with 2 decimals of accuracy
[15:46:30][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:30][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:30][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:30][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:30][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:30][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:30][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:31][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:31][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:31][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:31][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:31][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:31][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:31][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:32][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:32][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:32][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:32][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:32][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:32][D][sensor:094]: 'Distance': Sending state 238.89999 cm with 2 decimals of accuracy
[15:46:33][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:33][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:33][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:33][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:33][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:33][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:33][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:34][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:34][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:34][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:34][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:34][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:34][D][sensor:094]: 'Distance': Sending state 230.80000 cm with 2 decimals of accuracy
[15:46:34][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:35][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:35][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:35][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:35][D][sensor:094]: 'Distance': Sending state 225.39999 cm with 2 decimals of accuracy
[15:46:35][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:35][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:36][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:36][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:36][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:36][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:36][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:36][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:36][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:37][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:37][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:37][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:37][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:37][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:37][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:37][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:38][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:38][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:38][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:38][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:38][D][sensor:094]: 'Distance': Sending state 31.80000 cm with 2 decimals of accuracy
[15:46:38][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:39][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:39][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:39][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:39][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:39][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:39][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:39][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:40][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:40][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:40][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:40][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:40][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:40][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:40][D][sensor:094]: 'Distance': Sending state 239.00000 cm with 2 decimals of accuracy
[15:46:41][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:41][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:41][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:41][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:41][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:41][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:42][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:42][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:42][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:42][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:42][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:42][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:42][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:43][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:43][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:43][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:43][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:43][D][sensor:094]: 'Distance': Sending state 31.20000 cm with 2 decimals of accuracy
[15:46:43][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:43][D][sensor:094]: 'Distance': Sending state 219.60001 cm with 2 decimals of accuracy
[15:46:44][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:44][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:44][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:44][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:44][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:44][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:45][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:45][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:45][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:45][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:45][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:45][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:45][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:46][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:46][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:46][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:46][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:46][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:46][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:46][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:47][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:47][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:47]ets Jun  8 2016 00:22:57
[15:46:47]
[15:46:47]rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
[15:46:47]configsip: 0, SPIWP:0xee
[15:46:47]clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
[15:46:47]mode:DIO, clock div:2
[15:46:47]load:0x3fff0030,len:1184
[15:46:47]load:0x40078000,len:12784
[15:46:47]load:0x40080400,len:3032
[15:46:47]entry 0x400805e4
[15:46:47][I][logger:262]: Log initialized
[15:46:47][C][ota:469]: There have been 1 suspected unsuccessful boot attempts.
[15:46:47][D][esp32.preferences:114]: Saving 1 preferences to flash...
[15:46:47][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[15:46:47][I][app:029]: Running through setup()...
[15:46:47][C][uart.arduino_esp32:077]: Setting up UART...
[15:46:47][    55][E][HardwareSerial.cpp:530] setRxBufferSize(): RX Buffer must be higher than 128.
[15:46:47]
[15:46:47][C][wifi:038]: Setting up WiFi...
[15:46:47][C][wifi:048]: Starting WiFi...
[15:46:47][C][wifi:049]:   Local MAC: XX:XX:XX:XX:XX:XX
[15:46:48][D][wifi:425]: Starting scan...
[15:46:48][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:48][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:48][D][sensor:094]: 'Distance': Sending state 253.80000 cm with 2 decimals of accuracy
[15:46:48][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:48][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:48][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:49][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:49][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:49][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:49][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:49][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:49][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:50][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:50][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:50][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:50][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:50][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:50][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:50][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:51][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:51][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:51][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:51][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:51][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:51][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:51][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:52][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:52][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:52][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:52][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:52][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:52][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:53][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:53][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:53][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:53][D][sensor:094]: 'Distance': Sending state 31.00000 cm with 2 decimals of accuracy
[15:46:53][D][sensor:094]: 'Distance': Sending state 32.30000 cm with 2 decimals of accuracy
[15:46:53][D][sensor:094]: 'Distance': Sending state 32.30000 cm with 2 decimals of accuracy
[15:46:53][D][sensor:094]: 'Distance': Sending state 32.30000 cm with 2 decimals of accuracy
[15:46:54][D][sensor:094]: 'Distance': Sending state 32.30000 cm with 2 decimals of accuracy
[15:46:54][D][sensor:094]: 'Distance': Sending state 32.30000 cm with 2 decimals of accuracy
[15:46:54][D][sensor:094]: 'Distance': Sending state 32.30000 cm with 2 decimals of accuracy


esphome:
  name: ultrasonic-sr04m
  includes:
    - AJ_SR04M_Sensor.h

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:
  #level: VERBOSE #makes uart stream available in esphome logstream
  # baud_rate: 0 #disable logging over uart
  
# Enable Home Assistant API
api:
  encryption:
    key: "VYTgaWFYVhlT/nvxIEVD5/jBtUSUFoTKlHDX+Ng+6d8="

ota:
  password: "642bd5435ed5fdcbec13e61cf4e1cb00"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Us-Ha Fallback Hotspot"
    password: "mwASg1rbcEGo"

captive_portal:

web_server:
  port: 80
  
globals:
  - id: last_state
    type: float
    restore_value: no
    initial_value: '0.0'

uart:
  id: uart_bus
  tx_pin: GPIO22 # echo/RX do SR04M  (verde)
  rx_pin: GPIO21 #trigger/TX do SR04M (amarelo)
  baud_rate: 9600
  stop_bits: 1
  rx_buffer_size: 4
  debug:


sensor:
  - platform: custom
    lambda: |-
      auto my_sensor = new AJ_SR04M_Sensor(id(uart_bus));
      App.register_component(my_sensor);
      return {my_sensor};
    sensors:
      unit_of_measurement: cm
      accuracy_decimals: 2
      name: "Distance"

Polling with 150 miliseconds:

#include "esphome.h"

class AJ_SR04M_Sensor : public PollingComponent, public UARTDevice, public Sensor {
  public:
    float last_state;
    float old_state;
    AJ_SR04M_Sensor(UARTComponent *parent) : PollingComponent(150), UARTDevice(parent) {}

    void update() override {
      old_state = last_state;
      //ESP_LOGD("Start", "Last: %.2f", old_state);

      byte frame[4];
      int pos = 0;
      float value = 0.0;

      //write(0x00); // Try this
      //write(0x01); // Try this
      write(0x55); // Try this AJ_SR04M
      //write(0xFF); // Try this DYP-A12BNYTW-V1.0
      while (available()) {

        frame[pos] = read();
        //ESP_LOGD("Hex", "Hex: %X", frame[pos]);

        //pos++;

        if (pos == 3) {
          //ESP_LOGD("Validation", "pos == 4");

          //if ((frame[0] == 0xFF) && (frame[4] == 0x00) && (((frame[0] + frame[1] + frame[2]) & 0x00ff) == frame[3])){
          if ((frame[0] == 0xFF) && (((frame[0] + frame[1] + frame[2]) & 0x00FF) == frame[3])) {
            //ESP_LOGD("Validation", "SUM");
            value = ((frame[1] << 8) + frame[2]) / 10.0;
            publish_state(value);
            last_state = value;
            //ESP_LOGD("Start", "Last: %f", last_state);
          }
          break;


        }
        pos++;

      }
    }
};