Controlling sonoff with esphome

I have ESP32 based temp measuring device in my greenhouse. Data is sent via mqtt protocol. Node-Red is capturing mqtt message and saves data to influxDB. I measure temp in several locations, but here important are temp at the top of greenhouse and soil temp. I would like to have solution where I can turn device when certain temp conditions are met. I have ventilator that moves air from the top of greenhouse under surface and therefore warm up soil during cold spring days.

I have flashed ESPhome to sonoff device and not sure what can I do next. I have tested sonoff device with Home Assistant, I can turn it on and off.

What kind of solution can I make ?

  1. Best would be that NodeRed checks temp vs condition that I set up and send message to turn on or off to sonoff device?
  2. Can Home Assistant be used for that purpose?
  3. I could change micropython code in ESP32 to check conditions, but then again, I need to send some message to sonoff device.

You can use a Home Assistant automation or automate it in node red, or even automate it in ESPHome.

The advantage of doing the automation in ESPHome is that it will continue to work while Home Assistant is offline. However this is only possible if your switch and sensors are on the one device. If you have to send the sensor information to the switch running ESPHome via home assistant then you might as well do the automation there.

e.g. You could try an automation like this in the Home Assistant automation editor:

trigger:
  platform: numeric_state
  entity_id: sensor.greenhouse_temperature
  above: 30
  for:
    minutes: 3
action:
  - service: switch.turn_on
    entity_id: switch.greenhouse_ventillator 

You can write another automation to turn it off when the temperature drops below a certain limit.

trigger:
  platform: numeric_state
  entity_id: sensor.greenhouse_temperature
  below: 22
  for:
    minutes: 3
action:
  - service: switch.turn_off
    entity_id: switch.greenhouse_ventillator 

Search the documents and read about automations, triggers, conditions and the services available for devices (a switch in this case).