Hi Rik,
I tried to copied what you had done in this topic. I'm running HA on raspberry pi 4b and using the Liudr usb adaptor to read Acclima [TDR315H](https://acclima.com/wp-content/uploads/Acclima-TDR-Sensor-User-Manual-REV-9-30-21.pdf) soil water content sensor. I set the address of the sensor to 1 and changed the serial port ID as you mentioned. I got records written in csv file, but unfortunatelly the records were all "unknow". I checked the entity state, it shows that this entity ("sensor.vwc_s1") does not have a unique ID. Trying adding the same unique id(s1) to each template sensor, it didn't work. It worked in the PC ternimal test
Can you please help me with advancing this?
Hi there, looks like these sensors should indeed work as well. The output of those Acclima TDR looks a bit different from the Teros ones. I guess you’d first want to change the the way your sensor values are created.
In config.yaml you can see how I am splitting the whole SDI-12 output string into seperate sensor outputs. Since your SDI-12 output is different than mine, you might need to check how this SDI-12 output is split into seperate sensor’s (or entities). To learn more about splitting sensor values (or entity states), this post can help: Multiple ‘splits’ in value_template in sensor - #10 by HarmlessSaucer
In any case, the sensors should read ‘normal’ values in the entity’s state. After that, the Excel file will simply be a copy of those states.
I’m on a holiday for this week, so I can’t easily reproduce a working config for you. Let me know if you’re still unable to solve this next week, we can look into it.
Hi Rik, Thanks for your help. The output(1+0.0+21.0+1.1+0+0) of the Acclima TDR sensor is indeed very similar to the Teros sensor. I checked the link you directed and recheck the template sensor in the template editor of the HA. I checked the "sensor.serial_sensor" and "vwc_s1" sensor, both working well. However, when I run the automation, the output in the csv file only gave me the serial sensor not the vwc_s1 sensor which was shown "unknown". And another strange thing was sometimes when I restarted the HA server, the states output of the serial sensor is something like "100015" which I supposed the output of command "1M!" . I attached some output and the config & automation yaml below. looking forward to your further perspectives.
configuration.yaml
###################################################################
# SDI-12 Measure commands
###################################################################
switch:
- platform: command_line
switches:
mm_sensor_1:
command_on: 'echo "1M!" > /dev/serial/by-id/usb-FTDI_FT231X_USB_UART_D30A6XB3-if00-port0' # Sends a measurement request to sensor
command_off: 'echo "1D0!" > /dev/serial/by-id/usb-FTDI_FT231X_USB_UART_D30A6XB3-if00-port0' # Sends a measurement request to sensor
###################################################################
# Logging sensor outputs to a .CSV file and make Telegram messaging possible
###################################################################
notify:
- platform: file
name: filenotify
filename: /config/soilwater.csv #Places measurements inside media/soilsensors folder
timestamp: true
###################################################################
# Untemplated SDI-12 string
###################################################################
sensor:
- platform: serial
serial_port: /dev/serial/by-id/usb-FTDI_FT231X_USB_UART_D30A6XB3-if00-port0
baudrate: 9600
- platform: time_date
display_options:
- "date_time"
###################################################################
# SENSOR ADRESS
###################################################################
- platform: template
sensors:
vwc_s1:
friendly_name: Volumetric Water Content Sensor 1
value_template: >-
{% if "1" in states('sensor.serial_sensor')[0:1] %}
{{ states('sensor.serial_sensor').split('+')[1] }}
{% else %}
{{ states('vwc_s1') }}
{% endif %}
automations.yaml
###################################################################
# Sending measurement commands and retrieve data commands to/from the sensor
###################################################################
- id: Log all sensors
alias: Log all sensors
trigger:
platform: time_pattern
seconds: '/10' #Sends measurement requests every 60 seconds
# minutes: '/15' #Sends measurement requests every 15 minutes
action:
# MAKE MEAUREMENTS FOR SENSOR 1
- service: switch.turn_on
entity_id: switch.mm_sensor_1
- delay: 00:00:00.2 #Delay is in to enable the sensor to initiate the measurements and respond with information about itself and how many measurements it made
- service: switch.turn_off
entity_id: switch.mm_sensor_1
- delay: 00:00:00.8 #Delay is in for the sensor time to respond with the actual sensor data
- service: notify.filenotify #Log sensors data directly after measurement was retrieved
data_template:
message: "{{ states('sensor.serial_sensor') }}
{{ states('vwc_s1') }};"
Nice job on getting the sensors showing data, first thing I spotted is some missing info on both your configuration.yaml and the last line in your automation.yaml
Please redefine sensors from
{{ states(‘vwc_s1’) }}"
To
{{ states(‘sensor.vwc_s1’) }}"
When looking twice, I see you have pasted the vwc sensor into the serial sensor template. Please take a closer look at the example I gave in my starting post, you will actually need two seperate sensor templates: One for the serial sensor (that is outputting the unique sensor ID, and vwc or any other value you whish to extract from the SDI-12 string.
For the the serial sensor to output something like “100015” is a known issue I found a workaround for, I’ll share it with you once I have access to my current config (after holidays)
Many thanks. I finally got all sensors’ readings correct. The “10015” issue is still on, I supposed that the previous “unknown” issue was also derived from the wrong serial sensor output (“10015” ). Hope to ses your solution to this issue afterwards. Enjoy your holidays.
Here’s the workaround for ignoring those faulty values. This example works specifically for the Teros12 though, it is also a bit more complicated than just measuring a value. I’ll describe it shortly,
-
vwc_s1: The raw volumetric water content output, derrived from the SDI-12 string
-
c_vwc_s1: The actual calculated volumetric water content, based on equation that came with the Teros (you probably could give this a go). This sensor converts the raw output to a number between 0 and 1.
-
filtered_vwc_s1: The volumetric water content but filtered for negative values. The equation in ‘c_vwc_s1’ should always output a value between 0 and 1, everything else will result in ‘nan’ as output. The ‘nan’ value is conveniently ignored by graphs in home assistant.
vwc_s1:
friendly_name: Volumetric Water Content Sensor 1
unit_of_measurement: "θ"
value_template: >
{% if '1' in states('sensor.serial_sensor') [0:1] -%}
{{ states('sensor.serial_sensor').split('+')[1] | default}}
{% else -%}
{{ states.sensor.vwc_s1.state }}
{% endif -%}
c_vwc_s1:
friendly_name: Calculated Volumetric Water Content Sensor 1
unit_of_measurement: "θ"
value_template: >
{{ ((6.771e-10) * (states('sensor.vwc_s1')|float ** 3) -
(5.105e-6) * (states('sensor.vwc_s1')|float ** 2) +
(1.302e-2) * (states('sensor.vwc_s1')|float) -10.848) }}
filtered_vwc_s1:
friendly_name: Filtered Volumetric Water Content Sensor 1
unit_of_measurement: "θ"
value_template: >
{%- if states.sensor.c_vwc_s1.state|float >= 0 and states.sensor.c_vwc_s1.state|float <= 1 -%}
{{(states.sensor.c_vwc_s1.state) | float | round (3) }}
{%- else -%}
nan
{%- endif -%}
Thanks, Rik. It has been long since I had put this project aside. I just checked your solution to the unknown issue, it works well. But another issue arises: the reading of the serial sensor never changed whatever I change the actual soil water content. The time_pattern I set up is every 60 seconds. See picture below. What do you think?
It looks like your sensor fails to make new measurements, but sends a cached measurement instead. Could you check the documentation of your sensor to see if the ‘make new measurement’ command is correct?
In other words, does command “1M!” make a new measurement and does command “1D0!” retrieve it?
-edit @jiangduo2013 I’m messaging you directly not to spoof the topic, I think we can figure out the solution and post it back here once resolved.
Would it be difficult to make this work with the Teros 32 Tensiometer sensor? I’m working with an agronomist who recommends using both types of moisture sensors for my greenhouse operation.
The first issue I’m seeing is that barometric pressure is needed to calibrate one of the measurements reported by the sensor.
“TEROS 32 measures the sum of matric potential and atmospheric pressure potential (Ψp + Ψm ). To extract the matric potential, the barometric pressure should also be registered with a reference sensor. METER ZL6 and EM60 data loggers include a barometric pressure sensor and convert the signal into soil water potential If using a non-METER data logger, a barometric sensor is needed at the measuring site. A barometric sensor is available from METER by contacting Customer Support.”
I’m guessing I could use a temp/humidity sensor like the RuuviTag to get a pressure reading but no idea actually how to do that. I have very little coding experience and have mostly used node-red to create automations, while having HA handle all my BT sensors and smart plugs integrations. I have HA running 2 grow tents rn without touching my config or automation folder lol
If you know/have a working function for calibrating the TEROS32 against barometric pressure, the answer is -edit no: this should not be very difficult.
With a barometer sensor that is precise/reliable/fast (probably you want a dedicated SDI-12 barometer instead of something wireless) you can use that sensor reading in a template like i did in post 15. No hurt in testing this with a cheap barometer you have lying around first. If you have the calibration function, I can help you template the sensors.
hello everyone new member here so please be gentle!.
i have an acclima tdr water sensor one of liudrs sdi12 to usb adapters and a rp4 running homeassistant, @Potterstraat project and code is more or less exactly what i am wanting to acomplish with the only difference being i want to send and recieve the data wirelessly through an esp32 back to my rp4.
i have the physical connections all sorted using pins 16 + 17 (tx/rx) uart2 on the esp32 to the serial connector on dr lius sdi12 to usb board.
i am just unsure what lines of code need changing in the config.yaml to tell my esp32 to send and look for the data on the uart2 port and not the /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AC00Z2T4-if00-port0 used for the usb connection.
can anyone help?, im a complete novice at this but willing and hopefully capable of learning.
many thanks.
So if I understand you right, you hooked the SDI-12 sensor up to an ESP32 and you want the ESP to talk to home assistant?
I think you can make this work with ESPHome: Custom UART Device — ESPHome
I guess you should be able to get the untemplated SDI-12 string into home assistant that way.
thanks for the speedy reply,
i have the sensor connected to the sdi12 to usb adapter and the adapter connected to the esp32 via uart.
and yes i would like the esp32 to send the read and retrieve data comands and to relay it back to homeassistant.
do you mean i should replace the original untemplated SDI-12 string code in your original config.yaml with the yaml code from the custom UART device link?.
like i said im a complete novice at this so please bare with me.
and what would need changing in the SDI-12 measure comands so it uses the uart for the on “1M!” and off “1D0!” and not the usb?.
also just to check is all this code going into the esp32’s config.yaml or does some go in the homeassistant config.yaml in file editor?.
The first thing to do after you connected the ESP to the SDI-12 modbus (Liudr board) is to get a sensor reading on the ESP. I’m not very familiar with ESPs connected via UART, but you might be lucky on other forums for setting up such a connection. Googled a bit, you are not the first one thinking in this direction: ESP32 as Converter between modbus and SDI - Using Arduino / Project Guidance - Arduino Forum
Out of curiosity, what is your usecase? What is the added value in setting up the Raspberry Pi separated from the SDI-12 modbus?
-edit: Looks like ESPHome has a modbus controller component you could try: Modbus Controller — ESPHome
hi Rik, i have the esp32 connected to the serial conections sdi-12 lidur board, i am pretty sure the connections are correct.
why do you think i might need aditional hardware?, i was under the impression that the liudr board would interface with the acclima sensor and then connect with the esp32 via tx rx pins then relay the readings wirelessly to my rp4.
its very similar to your setup, you have your teros sensors connected to the sdi-12 liudr board, i have my acclima sensor connected the same.
your data is traveling via the usb serial port on the liudr board direct to your rp4, my data is traveling via the serial connections on the liudr board into the uart connections on the esp32 then transmitted wirelessly to my rp4.
i thought that our projects were similar enough that your original code could be adapted to account for the differences in how we send and recieve the data, yours serial usb, mine uart, without to much work.
the only reason the rp4 is seperate from the sdi-12 adapter is because the rp4 is in use in an existing set up and the sensor will be a few rooms away.
i thought it would be straight forward using the esp32 to transmit the data to homeassistant wirelessly.
if we cant get your original code to work with some modifications could this approach work? - https://community.home-assistant.io/t/reading-sending-to-esp32-serial-uart-vs-mqtt-for-ha-workflows/280111/7
i have used his work and came up with this,
esphome:
name: acclimatdr-310w
platform: ESP32
board: esp-wrover-kit
includes:
- uart_read_line_sensor.h
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Acclima Tdr-310W"
password: "ytxcMUCZwHJt"
captive_portal:
# Enable logging
logger:
level: VERBOSE #makes uart stream available in esphome logstream
baud_rate: 0 #disable logging over uart
# Enable Home Assistant API
api:
ota:
uart:
id: tdr_wc_ec
tx_pin: 16
rx_pin: 17
baud_rate: 9600
text_sensor:
- platform: custom
text_sensors:
id: "tdr_wc_ec_uart"
lambda: |-
auto tdr_wc_ec_sensor = new UartReadLineSensor(id(tdr_wc_ec));
App.register_component(tdr_wc_ec_sensor);
return {tdr_wc_ec_sensor};
- platform: homeassistant
name: "ACCLIMA COMMANDS"
id: acclima_command
entity_id: input_text.acclima_command
switch:
- platform: template
name: "Send Command"
id: send_cmd
icon: "mdi:send"
turn_on_action:
- uart.write:
id: tdr_wc_ec
data: !lambda |-
std::string str = id(acclima_command).state;
std::vector<uint8_t> vec(str.begin(), str.end());
vec.push_back('\r');
return vec;
- delay: 2000ms
- switch.turn_off: send_cmd
it dosent quite work… my buttons on the dashboard are not correct (not the same as the example in the link) so i cant input any comands for the sensor so i dont get any readings, but im trying and i feel i have made some progress.
honestly im green as grass when it comes to this stuff so any help much appreciated.
I don’t think you need additional hardware, you are already putting in an ‘in between ESP’
I think you are on the right track templating the ESP via ESPhome following that thread. Although these lambda templates are hard to troubleshoot, especially since they are C++ and do not 1 to 1 match with Home Assistant templates. I’m afraid I’m also not much of a help on those templates.
Setting up some working switches an text inputs in ESPHome to see whether it connects to Home Assistant shouldn’t be to difficult though, maybe try adding a simple switch first to see if that works, then you are down to troubleshooting these lambda templates to get a reading.
Also I don’t know if you already found the ESP log, inside Home Assistant you can open the ESPHome tab and click your ESP there. From that screen you can open a live log, it might help reading this log while trying to input text.
-edit: Adding a few lines in your ESPHome config for debugging UART communication might also be a good idea UART Bus — ESPHome
Keep us posted on your findings, I’m interested on the outcome
ok thanks its good to know that maybe im on the right track.
yes i have found the logs section in esphome thanks.
i tried adding a couple of simple switches, not sure if they are correct?. it still doesnt work but there is activity in the logs.
esphome:
name: acclimatdr-310w
platform: ESP32
board: esp-wrover-kit
includes:
- uart_read_line_sensor.h
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Acclima Tdr-310W"
password: "ytxcMUCZwHJt"
captive_portal:
# Enable logging
logger:
level: VERBOSE #makes uart stream available in esphome logstream
baud_rate: 0 #disable logging over uart
# Enable Home Assistant API
api:
ota:
uart:
id: tdr_wc_ec
tx_pin: 16
rx_pin: 17
baud_rate: 9600
text_sensor:
- platform: custom
text_sensors:
id: "tdr_wc_ec_uart"
lambda: |-
auto tdr_wc_ec_sensor = new UartReadLineSensor(id(tdr_wc_ec));
App.register_component(tdr_wc_ec_sensor);
return {tdr_wc_ec_sensor};
switch:
- platform: uart
name: "read sensor"
data: '1M!'
- platform: uart
name: "get measurement"
data: '1D0!'
INFO Reading configuration /config/esphome/acclima TDR-310W.yaml...
INFO Starting log output from acclimatdr-310w.local using esphome API
INFO Successfully connected to acclimatdr-310w.local
[10:43:12][I][app:102]: ESPHome version 2022.2.6 compiled on Mar 11 2022, 10:32:46
[10:43:12][C][wifi:491]: WiFi:
[10:43:12][C][wifi:353]: Local MAC: 94:B9:7E:7B:94:D0
[10:43:12][C][wifi:354]: SSID: [redacted]
[10:43:12][C][wifi:355]: IP Address: 192.168.1.239
[10:43:12][C][wifi:357]: BSSID: [redacted]
[10:43:12][C][wifi:358]: Hostname: 'acclimatdr-310w'
[10:43:12][C][wifi:360]: Signal strength: -51 dB ▂▄▆█
[10:43:12][V][wifi:362]: Priority: 0.0
[10:43:12][C][wifi:366]: Gateway: 192.168.1.1
[10:43:12][C][wifi:367]: DNS1: 192.168.1.1
[10:43:12][C][wifi:368]: DNS2: 0.0.0.0
[10:43:12][C][logger:233]: Logger:
[10:43:12][C][logger:234]: Level: VERBOSE
[10:43:12][C][logger:235]: Log Baud Rate: 0
[10:43:12][C][logger:236]: Hardware UART: UART0
[10:43:12][C][uart.arduino_esp32:107]: UART Bus:
[10:43:12][C][uart.arduino_esp32:108]: TX Pin: GPIO16
[10:43:12][C][uart.arduino_esp32:109]: RX Pin: GPIO17
[10:43:12][C][uart.arduino_esp32:111]: RX Buffer Size: 256
[10:43:12][C][uart.arduino_esp32:113]: Baud Rate: 9600 baud
[10:43:12][C][uart.arduino_esp32:114]: Data Bits: 8
[10:43:12][C][uart.arduino_esp32:115]: Parity: NONE
[10:43:12][C][uart.arduino_esp32:116]: Stop bits: 1
[10:43:12][C][uart.switch:040]: UART Switch 'read sensor'
[10:43:12][C][uart.switch:040]: UART Switch 'get measurement'
[10:43:12][C][captive_portal:144]: Captive Portal:
[10:43:12][C][mdns:084]: mDNS:
[10:43:12][C][mdns:085]: Hostname: acclimatdr-310w
[10:43:12][V][mdns:086]: Services:
[10:43:12][V][mdns:088]: - _esphomelib, _tcp, 6053
[10:43:12][V][mdns:090]: TXT: version = 2022.2.6
[10:43:12][V][mdns:090]: TXT: mac = 94b97e7b94d0
[10:43:12][C][ota:085]: Over-The-Air Updates:
[10:43:12][C][ota:086]: Address: acclimatdr-310w.local:3232
[10:43:12][C][api:138]: API Server:
[10:43:12][C][api:139]: Address: acclimatdr-310w.local:6053
[10:43:12][C][api:143]: Using noise encryption: NO
[10:43:20][D][switch:013]: 'read sensor' Turning ON.
[10:43:20][D][switch:037]: 'read sensor': Sending state ON
[10:43:20][D][uart.switch:020]: 'read sensor': Sending data...
[10:43:20][D][switch:037]: 'read sensor': Sending state OFF
[10:43:30][D][switch:013]: 'get measurement' Turning ON.
[10:43:30][D][switch:037]: 'get measurement': Sending state ON
[10:43:30][D][uart.switch:020]: 'get measurement': Sending data...
[10:43:30][D][switch:037]: 'get measurement': Sending state OFF