Automation SPA with Solar Panel in EspHome

Hello everyone!
I need your help for my project which consists of automating the heating of my spa thanks to my solar thermal panel.

I made the code in Arduino IDE for my tests which works well.
The principle of the installation is a spa, a solar thermal panel. Each has a temperature probe, and depending on the temperatures collected, turn on or off the pump which circulates the water and therefore heats it or not.

This system is good, but I have no visual feedback of the data. So I would like to transform this .ino code into yaml to integrate it into EspHome of Home Assistant, first to see the temperatures and if the pump is running or not. Maybe after add a button to turn the pump on or off manually (2nd step that).

Here is my .ino code that works well:

#include <OneWire.h>
#include <DallasTemperature.h>

// Pin où les sondes DS18B20 sont connectées
const int ONE_WIRE_BUS = 4;

// Configuration du bus OneWire
OneWire oneWire(ONE_WIRE_BUS);

// Configuration du capteur Dallas Temperature
DallasTemperature sensors(&oneWire);

// Adresses des sondes détectées
DeviceAddress spaProbe = {0x28, 0x30, 0xA2, 0x6A, 0x00, 0x00, 0x00, 0xC8};
DeviceAddress panelProbe = {0x28, 0xDA, 0x86, 0x6C, 0x00, 0x00, 0x00, 0xC0};

// Pin de contrôle pour la pompe
const int POMPE_PIN = 2;

void setup() {
  // Initialisation de la communication série
  Serial.begin(115200);
  Serial.println("Initialisation des sondes DS18B20...");

  // Initialisation des capteurs
  sensors.begin();

  // Configuration de la pin de la pompe en sortie
  pinMode(POMPE_PIN, OUTPUT);
  digitalWrite(POMPE_PIN, LOW); // Assure que la pompe est éteinte au démarrage
}

void loop(void) {
  // Demande de mise à jour des températures
  sensors.requestTemperatures();
  
  // Lecture des températures
  float tempSpa = sensors.getTempC(spaProbe);
  float tempPanel = sensors.getTempC(panelProbe);

  // Affichage des températures
  Serial.print("Température du spa : ");
  Serial.print(tempSpa);
  Serial.println(" °C");

  Serial.print("Température du panneau solaire : ");
  Serial.print(tempPanel);
  Serial.println(" °C");

  // Régulation de la pompe
  if (tempPanel > tempSpa + 5 && tempSpa < 37) {
    // Température du panneau solaire est suffisamment élevée et la température du spa est inférieure à 37°C
    digitalWrite(POMPE_PIN, HIGH); // Activer la pompe
    Serial.println("Pompe activée");
  } else {
    // Conditions non remplies
    digitalWrite(POMPE_PIN, LOW); // Désactiver la pompe
    Serial.println("Pompe désactivée");
  }

  // Attendre 5 secondes avant la prochaine lecture
  delay(5000);
}

Here is my yaml code that does not work at all:

esphome:
  name: solar-panel-spa
  friendly_name: SOLAR-PANEL-SPA

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "ojiPAfaSUemYjL3LjY7g3fy3ewxu9PZC6dv99voiCvI="

ota:
  - platform: esphome
    password: "233a44a993b4b60bdb652a156a6eae1e"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Solar-Panel-Spa Fallback Hotspot"
    password: "1cHFnR3nhOQq"

captive_portal:
    
# Ajout des composants pour les capteurs DS18B20
one_wire:
  - platform: gpio
    pin: GPIO4

sensor:
  - platform: dallas_temp
    address: 0x2830A26A000000C8
    name: "Température du spa"
  - platform: dallas_temp
    address: 0x28DA866C000000C0
    name: "Température du panneau solaire"

# Configuration de la sortie pour la pompe
switch:
  - platform: gpio
    pin: GPIO2
    name: "Pompe"
    id: pompe

automation:
  - trigger:
      - platform: time
        seconds: /5
      condition:
        - condition: and
          conditions:
            - condition: template
              value_template: "{{ states('sensor.temperature_du_panneau_solaire')|float > states('sensor.temperature_du_spa')|float + 5 }}"
            - condition: template
              value_template: "{{ states('sensor.temperature_du_spa')|float < 37 }}"
      action:
        - switch.turn_on: pompe
      
  - trigger:
    - platform: time
      seconds: /5
    condition:
      - condition: or
        conditions:
          - condition: template
            value_template: "{{ states('sensor.temperature_du_panneau_solaire')|float <= states('sensor.temperature_du_spa')|float + 5 }}"
          - condition: template
            value_template: "{{ states('sensor.temperature_du_spa')|float >= 37 }}"
    action:
      - switch.turn_off: pompe

Thanks for your help, but maybe there is another way to do it than coding the yaml file.

Not quite specific…
Just remove all that home assistant automation from your code and verify that your sensors and switch are working. Then move on. You could do the whole automation in Esphome, would improve reliability.

Oh thank you Karosm. I see my “appareil” now in HA. I continue the configuration.

Have a look at esphome documentation, especially automations and time component. Doing native automation in esphome gives you independency from home assistant.

Also, using lamdas you can almost copy-paste your ardu code…

ok, thank’s,.
here my new code yaml

captive_portal:

one_wire:
  - platform: gpio
    pin: GPIO4

sensor:
  - platform: dallas_temp
    address: 0x2830A26A000000C8
    id: temperature_spa
    name: "Température du spa"
  - platform: dallas_temp
    address: 0x28DA866C000000C0
    id: temperature_panel_solar
    name: "Température du panneau solaire"

switch:
  - platform: gpio
    pin: GPIO2
    name: "Pompe"
    id: pump

i see my pump and the two sensors temperature, but i don’t see the value of temperature in dashboard.

The temperatures are unknow :-/

look at the log, what’s going on
Also, as default it’s updating only ones a minute.

In log, there is an error :
“Component dallas_temp.sensor set Warning flag: scratch pad checksum invalid”

But in arduino ide logs, there are temperatures, so electronic wiring is ok.

UPDATE info :
Log arduino ide find address :
Adresse trouvée : 28:30:A2:6A:00:00:00:C8
Adresse trouvée : 28:DA:86:6C:00:00:00:C0

And in logs esp for one_wire :
0xc80000006aa23028 (DS18B20)
0xc00000006c86da28 (DS18B20)

What are the good address …

inverted byte order…

It’s work !

So, the code where is well :

esphome:
  name: solar-panel-spa
  friendly_name: SOLAR-PANEL-SPA

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "ojiPAfaSUemYjL3LjY7g3fy3ewxu9PZC6dv99voiCvI="

ota:
  - platform: esphome
    password: "233a44a993b4b60bdb652a156a6eae1e"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Solar-Panel-Spa Fallback Hotspot"
    password: "1cHFnR3nhOQq"

captive_portal:


external_components:
  - source: github://nrandell/dallasng

dallasng:
  - pin: GPIO4
    update_interval: 60s

sensor:
  # Spa sonde (1)
  - platform: dallasng
    index: 0
    id: temperature_spa
    name: "Température du spa"
    unit_of_measurement: "°C"
    icon: "mdi:thermometer-plus"
    device_class: "temperature"
    state_class: "measurement"

  # Panel sonde (2)
  - platform: dallasng
    index: 1
    id: temperature_panel_solar
    name: "Température du panneau solaire"
    unit_of_measurement: "°C"
    icon: "mdi:thermometer-plus"
    device_class: "temperature"
    state_class: "measurement"

switch:
  - platform: gpio
    pin: GPIO2
    name: "Pompe"
    id: pump

Thank you for your help

inverting the byte order it didn’t work??