Played around with the irrigaton system yaml

Signed-off-by: Eduard Iten <eduard@iten.pro>
This commit is contained in:
Eduard Iten 2025-07-14 14:12:48 +02:00
parent 6f304efb57
commit ef966cb078
1 changed files with 119 additions and 64 deletions

View File

@ -1,3 +1,11 @@
# ===================================================================
# ESPHome Configuration - Final Version
#
# This version corrects the C++ function call inside the valve actions
# to use the correct `send` method from the ModbusDevice base class,
# which is compatible with the esp-idf framework.
# ===================================================================
esphome: esphome:
name: irrigation-system name: irrigation-system
friendly_name: Bewässerung friendly_name: Bewässerung
@ -5,7 +13,7 @@ esphome:
esp32: esp32:
board: esp32-c6-devkitm-1 board: esp32-c6-devkitm-1
framework: framework:
type: esp-idf type: esp-idf # Set to esp-idf as required by the ESP32-C6 board
wifi: wifi:
ssid: !secret wifi_ssid ssid: !secret wifi_ssid
@ -24,88 +32,135 @@ logger:
web_server: web_server:
# UART-Bus für Modbus # ===================================================================
# HARDWARE SETUP - COMPLETE
# ===================================================================
# --- UART for RS485 Communication ---
uart: uart:
id: uart_bus id: uart_bus
tx_pin: GPIO1 tx_pin: GPIO1
rx_pin: GPIO2 rx_pin: GPIO2
baud_rate: 9600 baud_rate: 9600
data_bits: 8
stop_bits: 1 stop_bits: 1
parity: NONE parity: NONE
# Modbus-Komponente (der Hub) # --- Base Modbus component for the bus ---
modbus: modbus:
- id: modbus1 - id: modbus_hub
uart_id: uart_bus uart_id: uart_bus
# --- Modbus Controller for the specific valve device ---
modbus_controller: modbus_controller:
- id: valve_device - id: valve_controller
address: 0x01 modbus_id: modbus_hub
modbus_id: modbus1 address: 0 # The Modbus address of your valve. Change if not 0.
# update_interval: 1s
number: # ===================================================================
# SENSORS - COMPLETE
# ===================================================================
sensor:
# This sensor reads the raw 16-bit value from the valve's input register.
- platform: modbus_controller - platform: modbus_controller
modbus_controller_id: valve_device modbus_controller_id: valve_controller
id: valve_controller_command name: "Valve Raw Status"
name: "Valve Control" id: valve_raw_status
address: 0x01 internal: true # Hide from Home Assistant UI
value_type: U_WORD register_type: read # 'read' is the valid type for input registers
# min_value: 0 address: 0x0000 # The address of the register to read
# max_value: 2 value_type: U_WORD # Read the full 16-bit unsigned word
# step: 1 - platform: modbus_controller
modbus_controller_id: valve_controller
name: "VDD"
id: valve_vdd
register_type: read # 'read' is the valid type for input registers
address: 0x00FC # The address of the register to read
value_type: U_WORD # Read the full 16-bit unsigned word
entity_category: diagnostic # Mark as diagnostic
unit_of_measurement: "V"
accuracy_decimals: 2 # Show two decimal places
# Apply filters to convert the raw value to volts and update periodically
filters:
- lambda: |-
// Convert the raw VDD value to volts
return x / 1000.0; // Assuming the value is in millivolts
- heartbeat: 60s # Update every 60 seconds
- delta: 200 # Only update if the value changes by more than 200 mV
globals: # ===================================================================
- id: my_valve_is_open # TEXT SENSORS FOR HUMAN-READABLE STATUS
type: bool # ===================================================================
restore_value: false text_sensor:
initial_value: 'true' # 1. This text sensor extracts the HIGH BYTE for the operation status.
- platform: template
name: "Valve Operation"
id: valve_operation_status
icon: "mdi:state-machine"
lambda: |-
// Extract the high byte from the raw status sensor
// using a bitwise right shift.
int operation_code = (int)id(valve_raw_status).state >> 8;
switch (operation_code) {
case 0: return {"Idle"};
case 1: return {"Opening"};
case 2: return {"Closing"};
case 3: return {"Obstacle Detected"};
case 4: return {"End Position Not Reached"};
default: return {"Unknown Operation"};
}
# 2. This text sensor extracts the LOW BYTE for the current valve state.
- platform: template
name: "Valve Position"
id: valve_position_status
icon: "mdi:valve"
lambda: |-
// Extract the low byte from the raw status sensor
// using a bitwise AND mask.
int state_code = (int)id(valve_raw_status).state & 0xFF;
switch (state_code) {
case 0: return {"Closed"};
case 1: return {"Open"};
default: return {"Unknown"};
}
# ===================================================================
# THE MAIN VALVE COMPONENT
# ===================================================================
valve: valve:
- platform: template - platform: template
name: "Modbus Ventil" name: "Modbus Controlled Valve"
id: my_modbus_valve id: modbus_valve
optimistic: false
# Lambda, um den aktuellen Zustand zu bestimmen # The lambda determines the current state (open or closed) of the valve.
# Liest den Zustand aus der globalen Variable
lambda: |- lambda: |-
return id(my_valve_is_open); int state_code = (int)id(valve_raw_status).state & 0xFF;
if (state_code == 1) {
# Aktion beim Drücken auf "Öffnen" return true; // Open
} else if (state_code == 0) {
return false; // Closed
} else {
return {}; // Unknown
}
# Action to execute when the "OPEN" button is pressed.
open_action: open_action:
- number.set: - lambda: |-
id: valve_controller_command // Use the send() command inherited from ModbusDevice
value: 1 // Function 0x06: Write Single Register
- globals.set: // Payload for value 1 is {0x00, 0x01}
id: my_valve_is_open const uint8_t data[] = {0x00, 0x01};
value: 'true' id(valve_controller).send(0x06, 0x0000, 1, 2, data);
# Action to execute when the "CLOSE" button is pressed.
# Aktion beim Drücken auf "Schliessen"
close_action: close_action:
- number.set: - lambda: |-
id: valve_controller_command // Payload for value 2 is {0x00, 0x02}
value: 2 const uint8_t data[] = {0x00, 0x02};
- globals.set: id(valve_controller).send(0x06, 0x0000, 1, 2, data);
id: my_valve_is_open # Action to execute when the "STOP" button is pressed.
value: 'false'
# (Optional) Aktion beim Drücken auf "Stopp"
stop_action: stop_action:
- number.set: - lambda: |-
id: valve_controller_command // Payload for value 3 is {0x00, 0x03}
value: 0 const uint8_t data[] = {0x00, 0x03};
id(valve_controller).send(0x06, 0x0000, 1, 2, data);
sensor:
- platform: modbus_controller
modbus_controller_id: valve_device
name: "Supply Voltage"
register_type: read
device_class: voltage
entity_category: diagnostic
accuracy_decimals: 2
filters:
- lambda: |-
return x / 1000.0;
address: 0x00F5
unit_of_measurement: "V"
value_type: U_WORD