How to turn on/off a relay based on temperatures?

Hi there. I’m using ESPHOME, and I have an ESP8266, hooked with a bridged 4 dallas sensors… They work just fine, I´m able to see the temperatures.

but now, I have a relay and a computer fan, and I would like to turn this fan on when MODEM sensor reaches 58 degrees C, and then turn it off when it goes down to 55 degrees C.

PS: the relay works fine (and also the fan) when I turn it on by the entity created.

Thank you in advance!

one_wire:
  - platform: gpio
    pin: D2
  
sensor:
  - platform: dallas_temp
    address: 0xc0f444b00164ff28
    name: "Modem"
    id: modem
  - platform: dallas_temp
    address: 0x7a5f1ab40164ff28
    name: "PS4-XboxSeriesS"
    id: games
  - platform: dallas_temp
    address: 0xc49217b40164ff28
    name: "NET"
    id: net
  - platform: dallas_temp
    address: 0x724460b70164ff28
    name: "Denon"  
    id: receiver

switch:
  - platform: gpio
    name: "Relay_Cooler_Modem"
    inverted: on
    pin: D4

Lots of ways to do it, but this should get you started:

Or if you want to be able to set the temperature the fan turn on at from your dashboard:

1 Like

@tom_l is on the button, but you really should read this too.

or just use the generic thermostat it’s what it does.

I think he was looking for an esphome solution, but that is equally valid.

You use the sensors from esp in the generic thermostat and have it switch the relay.

Or if you must do it in esphome

Why try to write scripts to do what is already available?