Comparison between values for Esphome

I’m working with esp01_1m
I will use an NTC sensor to acquire the temperature of a boiler, but for now I simulate it with “Tsens”.
Based on the chosen modes and the temperatures “Tsens” (detected) and “TsetOUT” (requested) the system will have to turn on or off two relays
For completeness of information I have to replace the board of an “Energie eco 250 esm” because it is damaged in the logical part and I want to take advantage of this to connect it to my Home Assistant

What it does:
I saw the sensors immediately without any problem and I can control it via Node-red without problems, but I would like to make it independent in case of problems with home assistant so equip it with logical autonomy

The problem:
I can’t implement (because it’s rusty)
When you receive a value from Tsens;
if you are in Normal mode (index =0)
if “Tsens”<“TsetOUT” turn on “resis” and “comp”
otherwise (always if you are in inde=0) turn off “resis” and “comp”

I can’t understand why every time I change the value “Tsens” turns everything off

part of code

select:
  - platform: template
    name: "Modus"
    id: modus_mode
    optimistic: true
    options:
    - Normal
    - Spento
    - Eco
    - Disinf
    - Ferie
    initial_option: Normal

number:
  - platform: template
    id: TsetOUT
    name: "TsetOUT"
    optimistic: true
    min_value: 35
    max_value: 70
    step: 5

  - platform: template
    id: Tsens
    name: "Tsens"
    min_value: 10
    max_value: 90
    update_interval: 5.0s
    step: 1
    optimistic: True
    on_value:
      then:
        - lambda: |-
            if (id(modus_mode).active_index() ==0)  {
              if (Tsens>id TsetOUT) {
                id (resis).turn_off();
                id(comp).turn_off();
              }else {
                if (Tsens< TsetOUT){
                  id (resis).turn_on();
                  id (comp).turn_on();
                }
              }
            } 

uh, why not use thermostat: Thermostat Climate Controller — ESPHome

1 Like

the thermostat component alone would be fine if the device had to do on/off on a single channel

in my case
Normal: for Tset 45° once 40°C is reached, turn off the heating element and leave the compressor on until 45°C
Eco: for Tsest 45°c always turn off the heating element and leave the compressor on until 45°C
BOOST: for Tsest 45°c leave the compressor and heating element on until 45°C
Disinfect: °Tset =70°C once 55°C is reached, turn off the compressor, leave the heating element on until 70°C and after 15 minutes activate relay 3 for thermostatic by-pass and turn on relay 4 for recirculation, wait 15 minutes, close relay 3 and 4 set the thermostat Normal
OFF: for Tset 10°C once 10°C is reached, turn off the heating element and compressor
Holidays: for Tset 25°C use only the compressor

Use id(the_number_id).state to read the values of your numbers.

if (x > id(TsetOUT).state)

Great solution but in the same code I can’t insert a delay in lambda

- platform: template
    id: Tsens
    name: "Tsens"
    min_value: 10
    max_value: 90
    update_interval: 5.0s
    step: 1
    optimistic: True
    on_value:
      then:
        - lambda: |-
            if (id(modus_mode).active_index() ==5)  {
              if (x < 60) {
                id (resis).turn_on();
                id(comp).turn_on();
              } else {
                if (x<70){
                  id (resis).turn_on();
                  id (comp).turn_off();
delay: 15min
                  id (resis).turn_off();
                }
              }
              
            }

but above all how can I force it to change the modus_mode from 5 to 1

I’m very rusty in recent years I’ve always only created the sensors in esphome and then thrown them in nod-red

thanks a lot for the help

Write your conditions in YAML instead of lambda or use a script to handle the delay and turn_off.

the delay would be valid for all modus_mode conditions
I need the delay (on board) only with modus_mode ==5

I’m not able to understand you.
And I have no idea what is that modbus_mode…

If you write the conditions in yaml, you can use any delays you want. Like this:

dear Karosm what you read is only a part of the code already reported at the beginning

To ask for help you have to be clear and therefore I post the complete code

for the bench tests I use “Tsens”, in reality I will use “TsenS” an NTC10k already on board the machine

esphome:
  name: "termodinamico"
  friendly_name: 'Energie eco 250 esm termodinamico '
  min_version: 2024.11.0
  name_add_mac_suffix: false

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "g+lkhXxO/NXy0lecesF/LsTNXq/gnAXmjt0J/ZQqpbk="

ota:
  - platform: esphome
    password: "5d27723df16b298e0593c77a8d6d67c7"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Rfid Fallback Hotspot"
    password: "BjogbVM0vdRC"

captive_portal:

sensor:
 
  - platform: adc
    id: source_sensor
    pin: A0
    filters:
      - multiply: 3.3

  - platform: ntc
    sensor: source_sensor
    calibration:
      b_constant: 3950
      reference_temperature: 5°C
      reference_resistance: 3Ohm
    name: Temperature sensore
    id: TsenS

switch:
- platform: gpio
  pin: GPIO12
  id: resis
  name: "resistenza"
  icon: "mdi:resistor-nodes"

- platform: gpio
  pin: GPIO13
  id: comp
  name: "compressore"
  icon: "mdi:pump"

select:
  - platform: template
    name: "Modus"
    id: modus_mode
    optimistic: true
  
    options:
    - Spento
    - Normal
    - Eco
    - Boost
    - Ferie
    - Disinf


    initial_option: Normal





number:
  - platform: template
    id: TsetOUT
    name: "TsetOUT"
    optimistic: true
    min_value: 35
    max_value: 70
    step: 5

  - platform: template
    id: Tsens
    name: "Tsens"
    min_value: 1
    max_value: 90
    update_interval: 2.0s
    step: 1
    optimistic: True
    on_value:
      then:
        - lambda: |-
            if (x>75){
              id(comp).turn_off();
              id(resis).turn_off();}

            if (x>id(TsetOUT).state){
              id(comp).turn_off();
              id(resis).turn_off();}

            if (id(modus_mode).active_index() ==0)  {
              if (x < 10) {
                id (resis).turn_on();
                id(comp).turn_on();
              }else {
                  id(comp).turn_off();
                  id(resis).turn_off();
                
              }
            }

            if (id(modus_mode).active_index() ==1)  {
              if (x < id(TsetOUT).state-5) {
                id (resis).turn_on();
                id(comp).turn_on();
              }else {
                if (x< id(TsetOUT).state){
                  id(comp).turn_off();
                  id(resis).turn_on();
                }
              }
            } 

            if (id(modus_mode).active_index() ==2)  {
              if (x < id(TsetOUT).state) {
                id (resis).turn_off();
                id(comp).turn_on();
              }
            }
            if (id(modus_mode).active_index() ==3)  {
              if (x < id(TsetOUT).state) {
                id (resis).turn_on();
                id(comp).turn_on();
              }
            }

            if (id(modus_mode).active_index() ==4)  {
              if (x < 25) {
                id (resis).turn_on();
                id(comp).turn_on();
              }else {
                  id(comp).turn_off();
                  id(resis).turn_off();
              }
            }

            if (id(modus_mode).active_index() ==5)  {
              if (x < 60) {
                id (resis).turn_on();
                id(comp).turn_on();
              } else {
                if (x<70){
                  id (resis).turn_on();
                  id (comp).turn_off();
                }else {
                  if (x>70){
                    id (resis).turn_off();
                    id (comp).turn_on();
- delay 15m
id (resis).turn_off();
id (comp).turn_off();
- select.set_index:
    id: modus_mode
    index: 1

                }

              }
              
            }


Ok, modbus_mode is select.
So you change the option:

- select.set_index:
    id: modus_mode
    index: 1

ok
I knew this command
but how can I insert it between the various if of the lambda, immediately after the delay

I had inserted the working yalm and not the one with problems

You can do that with lambda as well, but why to insist with lambdas, since you can’t use delays with lambdas?

That code doesn’t even compile.

the file does not compile for the lines above, which are the lines that I cannot insert at that point in the cycle
I cannot make it wait 15 minutes and then turn off the relays

Of course you can but not in lambda, look at the code on post#10.
Delay is in yaml code, not inside lambda.
You can use that structure to rewrite your code section.