# =================================================================== # 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: name: irrigation-system friendly_name: Bewässerung esp32: board: esp32-c6-devkitm-1 framework: type: esp-idf # Set to esp-idf as required by the ESP32-C6 board wifi: ssid: !secret wifi_ssid password: !secret wifi_password fast_connect: true api: encryption: key: !secret api_key ota: platform: esphome password: !secret ota_password logger: web_server: # =================================================================== # HARDWARE SETUP - COMPLETE # =================================================================== # --- UART for RS485 Communication --- uart: id: uart_bus tx_pin: GPIO1 rx_pin: GPIO2 baud_rate: 9600 data_bits: 8 stop_bits: 1 parity: NONE # --- Base Modbus component for the bus --- modbus: - id: modbus_hub uart_id: uart_bus # --- Modbus Controller for the specific valve device --- modbus_controller: - id: valve_controller modbus_id: modbus_hub address: 0 # The Modbus address of your valve. Change if not 0. # update_interval: 1s # =================================================================== # SENSORS - COMPLETE # =================================================================== sensor: # This sensor reads the raw 16-bit value from the valve's input register. - platform: modbus_controller modbus_controller_id: valve_controller name: "Valve Raw Status" id: valve_raw_status internal: true # Hide from Home Assistant UI register_type: read # 'read' is the valid type for input registers address: 0x0000 # The address of the register to read value_type: U_WORD # Read the full 16-bit unsigned word - 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 # =================================================================== # TEXT SENSORS FOR HUMAN-READABLE STATUS # =================================================================== text_sensor: # 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: - platform: template name: "Modbus Controlled Valve" id: modbus_valve optimistic: false # The lambda determines the current state (open or closed) of the valve. lambda: |- int state_code = (int)id(valve_raw_status).state & 0xFF; if (state_code == 1) { 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: - lambda: |- // Use the send() command inherited from ModbusDevice // Function 0x06: Write Single Register // Payload for value 1 is {0x00, 0x01} const uint8_t data[] = {0x00, 0x01}; id(valve_controller).send(0x06, 0x0000, 1, 2, data); # Action to execute when the "CLOSE" button is pressed. close_action: - lambda: |- // Payload for value 2 is {0x00, 0x02} const uint8_t data[] = {0x00, 0x02}; id(valve_controller).send(0x06, 0x0000, 1, 2, data); # Action to execute when the "STOP" button is pressed. stop_action: - lambda: |- // Payload for value 3 is {0x00, 0x03} const uint8_t data[] = {0x00, 0x03}; id(valve_controller).send(0x06, 0x0000, 1, 2, data);