Variables in ESPHome and HomeAssistant

Hello there!

This is my first post, so please let me know how to improve it! :slight_smile:

What I want to do
I want to build a simple automation for my pool in order to replace this:
Oku Suncontrol

What this basically does is mesuring two PT100 Sensors (one for pool temp and one for solar temp) and switch a valve accordingly (“heating”: water flows through filter + solar circuit).

What I have built (hardware)
I am using the nodeMCU development board as platform.
As temperature sensors, two Dallas BM1820 will replace (and be way better than) the PT100.
The valve then will be switched by simple 230 V Relais.

The hardware works so far.

Intended algorithm
The system shall work as follows:

It shall be possible to define a desired pool temperature in HomeAssistant.
The system shall compare the actual pool temperature with the desired temperature.
If the actual temperature is below the desired and temperature AND solar temperature is above (desired temperature + hysteresis), the valve shall switch to “solar circuit”.

If the actual pool temperature goes above (desired temperature + hysteresis), the valve shall switch to “filter circuit”.
If the solar temperature drops below (desired temperature + hysteresis), the valve shall switch to “filter circuit”.

I hope it is clear what I intend to build :slight_smile:

What I have built (software)
I managed to initialize and read the DS18B20.
I also used the “switch” template to be able to see (and manually change) the current valve state.
Therefore, I implemented two switches (“solarcircuit” and “filtercircuit”) and created a SW-interlock.

With hard-coded values, everything works as intended.

What I need help with
There is some funcionality missing that I am obviously not able to implement by myself.
So I kindly ask for Your help (as I could not find a working example in the ESPHome manual / guide or the forum yet).

  1. Variables within ESPHome
    Unfortunately, I was not even able to work with variables within ESPHome.
    I want to have the hysteresis as variable that I can easily access within my code.
    This is my first YAML project, so I really am an absolute beginner. I am kind of familiar with C-code though.

I tried this:

sensor:
  - platform: dallas
    address: 0x880921c10a744528
    name: "Pool Temperatur"
    id: pool_temperature
    on_value_range:
      - below: 25
        then:
          - lambda: |-
              if (id(solar_temperature).state > (id(esp_desired_pool_temperature)+10))
              {
                id(filtercircuit).turn_off();
                id(solarcircuit).turn_on();
              }
              else
              {
                id(solarcircuit).turn_off();
                id(filtercircuit).turn_on();
              }
      - above: 26.5
        then:
          - switch.turn_off: solarcircuit
          - switch.turn_on: filtercircuit

This works, but I was not able to replace the hardcoded values with variables.

  1. Import a variable from HomeAssistant
    As soon as working with variables within ESPHome works, I want to be able to change the desired pool temperature from within HomeAssistant.

If anyone could help me out, this would be awesome!
Also, please feel free to suggest anything else You would code differently.
Any help is appreciated! :slight_smile:

Thank You very much in advance!

To get homeassistant values into esphome use a homeassistant sensor Home Assistant Sensor — ESPHome

Also I think you should look at the climate components :slight_smile:

Also, you’re more likely to get support on the pure Home Assistant side than ESPHome, so I would suggest to keep the esp code to a minimum, and do all your logic on the HA side.

For your hysteresis issue, for instance, there is a Threshold helper in HA that does that

This assuming you plan to have a continuous link between your ESP and HA

Thank You!
Just to make sure I get this right:

I have the input_number in the configuration.yaml of HA.
I then need a sensor within HA that reads the input_number?
If so, where is the correct place to implement this sensor? In configuration.yaml I get the error message:
Integration error: sensors - Integration ‘sensors’ not found. Integration error: platform - Integration ‘platform’ not found.

Here is the configuration.yaml:

input_number:
  pool_desired_temp:
    name: Pool Temperature
    initial: 25
    min: 15
    max: 50
    step: 1
    icon: mdi:pool-thermometer
    
#This code produces the above mentioned error
platform: template
sensors:
 pool_desired_temp:
  value_template: '{{states.input_number.pool_desired_temp.state | float}}'

Also, @zoogara mentioned in this post to get rid of the sensor and use the input_number directly - is that even possible? I can’t figure out how.

Thank You!

I plan to do some more stuff with this “pool control” (pump, time-based switch, drive some more external components, etc.). In order to “relief” the Raspi with HA, I want this to run on the ESP.
Also, this should run independent and without a permanent connection to HA.

Does this make sense or is it a bad idea?

I will have a look at the threshold helper!
Maybe there is way to run / import this to the ESP.

Making it standalone-capable isn’t at all unreasonable. You definitely can pull values back into ESPHome devices from HA. Create a helper in HA to hold the value, then a sensor in ESPHome like this one:

# Example configuration entry
sensor:
  - platform: homeassistant
    id: current_temperature
    entity_id: climate.living_room
    attribute: current_temperature

You may also want to do some trickery with lambdas to copy that value over to ESPHome (and set it in HA from ESPHome), so it’s not dependent on HA to get to it at all times.