JSN-SR04T sensor not quite adhering to the update interval

Hello,
I’ve set up a JSN-SR04T water level sensor in Mode 2 (querying using a 0x55 command), but it’s not working quite as I expected it to. It seems to query once, then wait the update_interval and then query again (thus the actual interval for new data is 2*update_interval) and actually get the data back. It’s difficult to describe but from the logs its apparent.

[12:41:41][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[12:41:44][V][jsn_sr04t.sensor:014]: Request read out from sensor
[12:41:44][V][jsn_sr04t.sensor:022]: Read byte from sensor: ff
[12:41:44][V][jsn_sr04t.sensor:022]: Read byte from sensor: 1
[12:41:44][V][jsn_sr04t.sensor:022]: Read byte from sensor: 86
[12:41:44][V][jsn_sr04t.sensor:022]: Read byte from sensor: 86
[12:41:44][V][jsn_sr04t.sensor:048]: Distance from sensor: 390mm, 0.390m
[12:41:44][V][sensor:043]: 'Distance': Received new state 0.390000
[12:41:44][D][sensor:094]: 'Distance': Sending state 0.39000 m with 3 decimals of accuracy
[12:41:49][V][jsn_sr04t.sensor:014]: Request read out from sensor
[12:41:54][V][jsn_sr04t.sensor:014]: Request read out from sensor
[12:41:54][V][jsn_sr04t.sensor:022]: Read byte from sensor: ff
[12:41:54][V][jsn_sr04t.sensor:022]: Read byte from sensor: 1
[12:41:54][V][jsn_sr04t.sensor:022]: Read byte from sensor: 4e
[12:41:54][V][jsn_sr04t.sensor:022]: Read byte from sensor: 4e
[12:41:54][V][jsn_sr04t.sensor:048]: Distance from sensor: 334mm, 0.334m
[12:41:54][V][sensor:043]: 'Distance': Received new state 0.334000
[12:41:54][D][sensor:094]: 'Distance': Sending state 0.33400 m with 3 decimals of accuracy
[12:41:59][V][jsn_sr04t.sensor:014]: Request read out from sensor
[12:42:04][V][jsn_sr04t.sensor:014]: Request read out from sensor
[12:42:04][V][jsn_sr04t.sensor:022]: Read byte from sensor: ff
[12:42:04][V][jsn_sr04t.sensor:022]: Read byte from sensor: 1
[12:42:04][V][jsn_sr04t.sensor:022]: Read byte from sensor: 4d
[12:42:04][V][jsn_sr04t.sensor:022]: Read byte from sensor: 4d
[12:42:04][V][jsn_sr04t.sensor:048]: Distance from sensor: 333mm, 0.333m
[12:42:04][V][sensor:043]: 'Distance': Received new state 0.333000
[12:42:04][D][sensor:094]: 'Distance': Sending state 0.33300 m with 3 decimals of accuracy
[12:42:09][V][jsn_sr04t.sensor:014]: Request read out from sensor
[12:42:14][V][jsn_sr04t.sensor:014]: Request read out from sensor
[12:42:14][V][jsn_sr04t.sensor:022]: Read byte from sensor: ff
[12:42:14][V][jsn_sr04t.sensor:022]: Read byte from sensor: 1
[12:42:14][V][jsn_sr04t.sensor:022]: Read byte from sensor: 44
[12:42:14][V][jsn_sr04t.sensor:022]: Read byte from sensor: 44
[12:42:14][V][jsn_sr04t.sensor:048]: Distance from sensor: 324mm, 0.324m
[12:42:14][V][sensor:043]: 'Distance': Received new state 0.324000
[12:42:14][D][sensor:094]: 'Distance': Sending state 0.32400 m with 3 decimals of accuracy
[12:42:19][V][jsn_sr04t.sensor:014]: Request read out from sensor
[12:42:24][V][jsn_sr04t.sensor:014]: Request read out from sensor
[12:42:24][V][jsn_sr04t.sensor:022]: Read byte from sensor: ff
[12:42:24][V][jsn_sr04t.sensor:022]: Read byte from sensor: 1
[12:42:24][V][jsn_sr04t.sensor:022]: Read byte from sensor: 50
[12:42:24][V][jsn_sr04t.sensor:022]: Read byte from sensor: 50
[12:42:24][V][jsn_sr04t.sensor:048]: Distance from sensor: 336mm, 0.336m
[12:42:24][V][sensor:043]: 'Distance': Received new state 0.336000
[12:42:24][D][sensor:094]: 'Distance': Sending state 0.33600 m with 3 decimals of accuracy
[12:42:29][V][jsn_sr04t.sensor:014]: Request read out from sensor
[12:42:34][V][jsn_sr04t.sensor:014]: Request read out from sensor
[12:42:34][V][jsn_sr04t.sensor:022]: Read byte from sensor: ff
[12:42:34][V][jsn_sr04t.sensor:022]: Read byte from sensor: 1
[12:42:34][V][jsn_sr04t.sensor:022]: Read byte from sensor: 49
[12:42:34][V][jsn_sr04t.sensor:022]: Read byte from sensor: 49
[12:42:34][V][jsn_sr04t.sensor:048]: Distance from sensor: 329mm, 0.329m
[12:42:34][V][sensor:043]: 'Distance': Received new state 0.329000
[12:42:34][D][sensor:094]: 'Distance': Sending state 0.32900 m with 3 decimals of accuracy

My configuration

---
substitutions:
  device_name: "rainwater-buffer-tank"
  friendly_name: Rainwater Buffer Tank

<<: !include .base.yaml

logger:
  level: VERBOSE

esp32:
  board: esp32dev
  framework:
    type: arduino

uart:
  - baud_rate: 9600
    tx_pin: 17
    rx_pin: 18

binary_sensor:
  - platform: gpio
    id: switch_state
    pin:
      number: 4
      inverted: True
      mode: 
        input: True
        pullup: True
    on_state: 
      then:
        - switch.toggle: relay

sensor:
  - platform: "jsn_sr04t"
    name: "Distance"
    update_interval: 5s

switch:
  - platform: gpio
    pin: 16
    id: relay
    name: Relay
    restore_mode: ALWAYS_OFF

Thank you in advance for any replies.

try debug: on uart component to see if there is traffic on that “unsuccessful” request.

Apparently yes.

[14:56:53][V][jsn_sr04t.sensor:014]: Request read out from sensor
[14:56:53][D][uart_debug:114]: >>> 55
[14:56:58][V][jsn_sr04t.sensor:014]: Request read out from sensor
[14:56:58][D][uart_debug:114]: >>> 55
[14:56:58][V][jsn_sr04t.sensor:022]: Read byte from sensor: ff
[14:56:58][V][jsn_sr04t.sensor:022]: Read byte from sensor: 1
[14:56:58][V][jsn_sr04t.sensor:022]: Read byte from sensor: 51
[14:56:58][V][jsn_sr04t.sensor:022]: Read byte from sensor: 51
[14:56:58][V][jsn_sr04t.sensor:048]: Distance from sensor: 337mm, 0.337m
[14:56:58][V][sensor:043]: 'Distance': Received new state 0.337000
[14:56:58][D][sensor:094]: 'Distance': Sending state 0.33700 m with 3 decimals of accuracy
[14:56:58][W][component:237]: Component jsn_sr04t.sensor took a long time for an operation (53 ms).
[14:56:58][W][component:238]: Components should block for at most 30 ms.
[14:56:58][D][uart_debug:114]: <<< FF:01:51:51
[14:57:03][V][jsn_sr04t.sensor:014]: Request read out from sensor
[14:57:03][D][uart_debug:114]: >>> 55
[14:57:08][V][jsn_sr04t.sensor:014]: Request read out from sensor
[14:57:08][D][uart_debug:114]: >>> 55
[14:57:08][V][jsn_sr04t.sensor:022]: Read byte from sensor: ff
[14:57:08][V][jsn_sr04t.sensor:022]: Read byte from sensor: 1
[14:57:08][V][jsn_sr04t.sensor:022]: Read byte from sensor: 58
[14:57:08][V][jsn_sr04t.sensor:022]: Read byte from sensor: 58
[14:57:08][V][jsn_sr04t.sensor:048]: Distance from sensor: 344mm, 0.344m
[14:57:08][V][sensor:043]: 'Distance': Received new state 0.344000
[14:57:08][D][sensor:094]: 'Distance': Sending state 0.34400 m with 3 decimals of accuracy
[14:57:08][W][component:237]: Component jsn_sr04t.sensor took a long time for an operation (54 ms).
[14:57:08][W][component:238]: Components should block for at most 30 ms.
[14:57:08][D][uart_debug:114]: <<< FF:01:58:58

Just tx, no response at all. Did you try how it’s behaving with other intervals like 2s and 15s?
Did you set it to mode2 with solder bridge or resistor?

Yeah, I had it at 15s which would result in 30s intervals between getting new data. Important to note that there are instances where it gets an instant response so it’s kind of random.

Esphome sends out the requests correctly it seems, it’s just that the device itself sommetimes answers sometimes doesn’t.

I bridged it with a pice of copper wire (soldered)

[15:36:34][V][jsn_sr04t.sensor:014]: Request read out from sensor
[15:36:34][D][uart_debug:114]: >>> 55
[15:36:34][V][jsn_sr04t.sensor:022]: Read byte from sensor: ff
[15:36:34][V][jsn_sr04t.sensor:022]: Read byte from sensor: 0
[15:36:34][V][jsn_sr04t.sensor:022]: Read byte from sensor: ed
[15:36:34][V][jsn_sr04t.sensor:022]: Read byte from sensor: ec
[15:36:34][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.ED.EC
[15:36:34][D][uart_debug:114]: <<< FF:00:ED:EC
[15:36:35][V][jsn_sr04t.sensor:014]: Request read out from sensor
[15:36:35][D][uart_debug:114]: >>> 55
[15:36:35][V][jsn_sr04t.sensor:022]: Read byte from sensor: ff
[15:36:35][V][jsn_sr04t.sensor:022]: Read byte from sensor: 0
[15:36:35][V][jsn_sr04t.sensor:022]: Read byte from sensor: f5
[15:36:35][V][jsn_sr04t.sensor:022]: Read byte from sensor: f4
[15:36:35][W][jsn_sr04t.sensor:051]: Invalid data read from sensor: FF.00.F5.F4
[15:36:35][D][uart_debug:114]: <<< FF:00:F5:F4

If random, then hardware wise, could be bad soldering on mode pad, bad wiring rx/tx or just a fact that those sensors don’t work reliably at 3.3V

I’m using 5V to power the board fyi.

With level shifters on rx/tx?

No TX to RX on ESP and RX to TX on ESP directly. There shouldn’t be a need for anything else no?

Different voltages are not compatible between each other.
Now you tx at 3.3V voltage and sensor might not consider high signal as high because it’s below threshold.
Rx instead is arriving 5V from the sensor and that might hurt your (3.3V) Esp .

Did you ever try that powering at 3.3V?

I haven’t tried powering the sensor using 3.3V because it says 5V on the PCB

Give it a try at 3.3V
Datasheet says 3.0-5.5V