Local control?

Hello everyone. I have a board with 8 relays controlled by an esp32. I recorded it with esphome. but I ran into a couple of problems that I would like to know how you guys do. When the internet is cut I have no way to turn on the relays and the most complicated thing for me is that the relays that controlled temperature or schedules do not do so because I manage this with automation. What can be done? Would I have to save the maximum and minimum time and temperature settings and control it locally? Could that be done with esphome? Thanks a lot

Yes that can be done with esphome :+1:

This bit of the docs is often missed, but really important reading. Automations and Templates — ESPHome

Also part what you have built is technically a climate device. Have you implemented it as such ESPHome — ESPHome

No. I only have two Dallas sensors that are only set to “sensors”. But the control is from Home Assistant. I’m going to read that to see how I do it. In theory, it is always controlled from esphome and it removed the Home Assistant authorizations, right?

Could you explain this a little further? Is home assistant and the esp on the same local network? Even if the internet is down, home assistant should still be able to connect to the esp. If the esp loses connection with home assistant, it will start to reboot.

Hello. It happened to me the other day that the switch was unplugged and the temperature of my aquarium dropped. I imagine various situations. The switch, the router, internet failure among other problems. The schedule of the aquarium is taken from home assistant and the maximum and minimum temperature as well. That’s why it seems to me that the safest thing is that esp32 takes care of this. But I would also like that when I change parameters in home assistant they change in esp32. I think I won’t be able to do the latter. The subject of the automation of esp32 saw that it is not complicated to configure. But I think the other could not do it. Thank you

Indeed, that’s the best way to get the most resilient solution.

Maybe just drop your yaml here so all dependencies to ha are visible and can be “moved” to esphome :rocket:

It can be done , however there are quite a few settings that have to be changed. First the API if it is disconnected from HA then it will reboot. Set reboot_timeout to zero.

This creates the next problem now if disconnected it will not reconnect without intervention. You will need to add a restart button to your config. You will also need to add the web server in order to activate it.

If there is a possibility of wifi going down you will need to add a captive portal. It will create a hotspot if the wifi goes down.

Okay now to the fun stuff your automations

I assume that you would want to change temperature from HA. The number component will create a similar entity to an input_number helper in HA. This will also allow adjustment directly via the web server.

Now since we are dealing with a situation where there can be an unintended restart, globals will allow you to set default values for any adjustable entity.

  - id: my_global
    type: int
    restore_value: yes
    initial_value: '80'

After a component you would use something like this:

    on_value:
      - globals.set:
          id: my_global
          value: !lambda 'return id(your_component_name).state;'

Then in the automation where you would normally add a number for the temperature, you would use this instead:

value: !lambda 'return id(my_global);'

uff It’s a lot of information together. Please be patient with me. Here I am copying the esphome code and the automation it had.

  1. I already found a problem and it is that I don’t know how to add 1.0 to the sensor.
  2. Lambda is used to bring a home assistant variable?
    This is something similar to what I had in automation. I don’t know how to proceed if I have to use the value set in a home assistant variable, for example maximum and minimum temperature. Thank you very much for everything you explained.
esphome:
  name: meteo

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "0664f60f6c5f7f8fe07e26bf0b9e1eac"

wifi:
  ssid: !secret wifi_ssid_ext
  password: !secret wifi_password

  ap:
    ssid: "meteo_ap"
    password: !secret wifi_password

captive_portal:
    
    
esp32_ble_tracker:

dallas:
  - pin: GPIO23

globals  
  - id: temp-max
    type: int
    restore_value: yes
    initial_value: '28.5'        
  - id: temp-min
    type: int
    restore_value: yes
    initial_value: '28.3'        

sensor:
  - platform: dallas
    address: 0x630516b54fc2ff28
    name: "Temperatura Sump"
    temperature:
      filters:
        - offset: 1.0
        
      on_value_range:
        - below: !lambda 'return id(temp-min);'
          for:
            minutes: 5
          then:
            - switch.turn_on: relay_2
            - switch.turn_on: relay_3
        - above: !lambda 'return id(temp-max);'
          for:
            minutes: 5
          then:
            - switch.turn_off: relay_2
            - switch.turn_off: relay_3

  - platform: dht
    pin: GPIO33
    temperature:
      name: "Temperatura Comedor"
    humidity:
      name: "Humedad Comedor"
    update_interval: 60s   
  - platform: pvvx_mithermometer
    mac_address: "A4:C1:38:7B:D2:35"
    temperature:
      name: "Temperatura cuarto principal"
      accuracy_decimals: 1
    humidity:
      name: "Humedad cuarto principal"
      accuracy_decimals: 0
    battery_level:
      name: "Nivel de Bateria cuarto principal"

switch:
  - platform: gpio
    name: "Relay 1"
    pin: GPIO27
    inverted: true
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Relay 2"
    pin: GPIO26
    inverted: true
    restore_mode: ALWAYS_OFF 
  - platform: gpio
    name: "Relay 3"
    pin: GPIO25
    inverted: true    
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Relay 4"
    pin: GPIO5
    inverted: true
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Relay 5"
    pin: GPIO18
    inverted: true
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Relay 6"
    pin: GPIO19
    inverted: true    
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Relay 7"
    pin: GPIO21
    inverted: true
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Relay 8"
    pin: GPIO22
    inverted: true
    restore_mode: ALWAYS_OFF 

Automation

  - alias: "Turn on the aquarium heater when the temperature is below 28.4"
    trigger:
      - platform: numeric_state
        entity_id: sensor.temperatura_plantado
        below: 28.4
        for:
          minutes: 5
      - platform: numeric_state
        entity_id: sensor.temperatura_plantado
        below: 28.2
      - platform: state
        entity_id: sensor.temperatura_plantado
        from: 
          - 'unavailable'
          - 'unknown'
    condition:
      - condition: numeric_state
        entity_id: sensor.temperatura_plantado
        below: 28.4
    action:
      - service: switch.turn_on
        target: 
          entity_id:      
            - switch.relay_2
            - switch.relay_3      

  - alias: "Turn off the aquarium heater when the temperature exceeds 28.4"
    trigger:
      - platform: numeric_state
        entity_id: sensor.temperatura_plantado
        above: 28.4
        for:
          minutes: 5
      - platform: numeric_state
        entity_id: sensor.temperatura_plantado
        above: 28.5
      - platform: state
        entity_id: sensor.temperatura_plantado
        from: 
          - 'unavailable'
          - 'unknown'
    condition:
      - condition: numeric_state
        entity_id: sensor.temperatura_plantado
        above: 28.4
    action:
      - service: switch.turn_off
        target:      
          entity_id: 
            - switch.relay_2
            - switch.relay_3

To bring a home assistant value into esphome, use one of the home assistant components. for example Home Assistant Sensor — ESPHome

Perfect @nickrout thanks.
What happens to that variable that I am importing if the internet or the router stops working? I want to be sure that everything will work before recording because it is very cold in my country and this is to control the temperature of my aquarium.

If you are trying to heat the aquarium instead of cooling it with the fans, the solution is simple, just get an automatic aquarium heater (which most aquarium heaters are) and set it to according to the fish you have in the aquarium. Do remember to use heaters according to the size of the aquarium in gallons. Cory has many videos on that topic

I have a similar setup, my ESP32 running ESPhome, which controls the water temperature during summers in one of my aquariums, 29 gallon one with triple fans.
In winter, I use the automatic water heater with a set temperature and ESP32 just monitors it.

The global type is set to int that is only whole numbers. To use a decimal use float

A lambda simply lets you use c++ code instead of yaml. It just allows additional functionality above what is available through yaml configuration. In this case we are recalling a global.

The only thing I see you are missing is the number component.

number:
  - platform: template
    name: "Set max temp"
    id: temp-max-set
    optimistic: true
    min_value: 28.0
    max_value: 29.0
    step: 0.1
    on_value:
      - globals.set:
          id: temp-max
          value: !lambda 'return id(temp-max-set).state;'
  - platform: template
    name: "Set min temp"
    id: temp-min-set
    optimistic: true
    min_value: 28.0
    max_value: 29.0
    step: 0.1
    on_value:
      - globals.set:
          id: temp-min
          value: !lambda 'return id(temp-min-set).state;'

Cool. The aquarium is 600L and varies greatly depending on the ambient temperature. That’s why I decided to put them a little hotter and regulate it with the esp32. Although I have a fan, it was never necessary to use it.

I already understood what lambda is. I know very little English and in the translation I lose half of the concepts. Now it is clear to me. thanks

For such a large aquarium, I could suggest using two heaters in opposite corners of the aquarium, each 200W automatic aquarium heater. ESP32 with Dallas sensor will be used just for monitoring.
this is the current state of my 29gal or 109L aquarium. The ambient temperature is 34C and Aquarium is at 30C.
This is the web interface mentioned in earlier posts, along with the shutdown and restart buttons.

There is something that I don’t know if

This code modifies the variable that I have in HA?

number:
  - platform: template
    name: "Set max temp"
    id: temp-max-set
    optimistic: true
    min_value: 28.0
    max_value: 29.0
    step: 0.1
    on_value:
      - globals.set:
          id: temp-max
          value: !lambda 'return id(temp-max-set).state;'

And this brings from HA the value that I have in the variables and saves it in the variables of esphome?

globals  
  - id: temp-max
    type: int
    restore_value: yes
    initial_value: '28.5'        
  - id: temp-min
    type: int
    restore_value: yes
    initial_value: '28.3'        

number:
  - platform: template
    name: "Set max temp"
    id: temp-max-set
    optimistic: true
    min_value: 28.0
    max_value: 29.0
    step: 0.1
    on_value:
      - globals.set:
          id: temp-max
          value: !lambda 'return id(temp-max-set).state;'

I have another fish tank under my aquarium that I use for all the filtration. And there I have two 300w heaters. The water circulates all the time with a pump that sends the water back up.

This is how it goes. I don’t want to record until I’m sure it’s going to work. Relays 2 and 3 are for the heaters. A heater for each relay. At some point I want only one to work as long as possible so I don’t consume so many watts

esphome:
  name: meteo

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "0664f60f6c5f7f8fe07e26bf0b9e1eac"

wifi:
  ssid: !secret wifi_ssid_ext
  password: !secret wifi_password

  ap:
    ssid: "meteo_ap"
    password: !secret wifi_password

captive_portal:
    
    
esp32_ble_tracker:

dallas:
  - pin: GPIO23

globals  
  - id: temp-max
    type: int
    restore_value: yes
    initial_value: '28.5'        
  - id: temp-min
    type: int
    restore_value: yes
    initial_value: '28.3'        

number:
  - platform: template
    name: "Set max temp"
    id: temp-max-set
    optimistic: true
    min_value: 28.0
    max_value: 29.0
    step: 0.1
    on_value:
      - globals.set:
          id: temp-max
          value: !lambda 'return id(temp-max-set).state;'
  - platform: template
    name: "Set min temp"
    id: temp-min-set
    optimistic: true
    min_value: 28.0
    max_value: 29.0
    step: 0.1
    on_value:
      - globals.set:
          id: temp-min
          value: !lambda 'return id(temp-min-set).state;'

sensor:
  - platform: dallas
    address: 0x630516b54fc2ff28
    name: "Temperatura Sump"
    temperature:
      filters:
        - offset: 1.0
        
      on_value_range:
        - below: !lambda 'return id(temp-min);'
          for:
            minutes: 5
          then:
            - switch.turn_on: relay_2
            - switch.turn_on: relay_3
        - above: !lambda 'return id(temp-max);'
          for:
            minutes: 5
          then:
            - switch.turn_off: relay_2
            - switch.turn_off: relay_3

  - platform: dht
    pin: GPIO33
    temperature:
      name: "Temperatura Comedor"
    humidity:
      name: "Humedad Comedor"
    update_interval: 60s   
  - platform: pvvx_mithermometer
    mac_address: "A4:C1:38:7B:D2:35"
    temperature:
      name: "Temperatura cuarto principal"
      accuracy_decimals: 1
    humidity:
      name: "Humedad cuarto principal"
      accuracy_decimals: 0
    battery_level:
      name: "Nivel de Bateria cuarto principal"

switch:
  - platform: gpio
    name: "Relay 1"
    pin: GPIO27
    inverted: true
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Relay 2"
    pin: GPIO26
    inverted: true
    restore_mode: ALWAYS_OFF 
  - platform: gpio
    name: "Relay 3"
    pin: GPIO25
    inverted: true    
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Relay 4"
    pin: GPIO5
    inverted: true
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Relay 5"
    pin: GPIO18
    inverted: true
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Relay 6"
    pin: GPIO19
    inverted: true    
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Relay 7"
    pin: GPIO21
    inverted: true
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Relay 8"
    pin: GPIO22
    inverted: true
    restore_mode: ALWAYS_OFF 

You are using a sump filter, with both heaters in the sump, I think that is the correct way to do it.
If those 300W heaters are automatic ones, they will turn off and on according to the temperature set. You don’t need any relays to turn those on or off.