Using input_number as variable in esphome development platform

I’ve done so far…

In HA, settings > device & services > helpers, I’ve defined my input numbers:

setpoint_humedad
name: Setpoint Humedad
icon: mdi:water-percent-alert
initial: 30
min: 30
max: 70
step: 1

In HA, developer tools > template, I’ve defined my sensor template using the
previous input_numbers:

  • platform: template
    sensors:
    setpoint_humedad:
    value_template: ‘{{ states.input_number.setpoint_humedad.state | int }}’

Lastly, in ESPHome, (my wemos D1 mini pro device, edit yaml), I’ve defined
the homeassistant sensors:

  • platform: homeassistant
    name: “Setpoint Humedad”
    entity_id: sensor.setpoint_humedad

Then as last part, i"ve to use the sensor in my ESPHome automation:

*** this is non-direction issue, I can not go further, so far I’ve see entity status
change and relay on/off, I would like to setup an setpoint wich define my humidity
fan start/stop.
Here’s part of the esp code (yaml)


switch:
  - platform: gpio
    pin: D6
    id: "relay"
    name: "Extractor Ducha"
    inverted: true
    
sensor:
  - platform: dht
    model: "DHT11"
    pin: D3
    temperature:
     id: "temperatura"
     name: "Temperatura Ducha"
    humidity:
     id: "humedad"
     name: "Humedad Ducha"
    
     on_value_range:
      - above: 60.0
        then:
          - switch.turn_on: "relay"
      - below: 50.0
        then:
          - switch.turn_off: "relay"
    update_interval: 10s

  - platform: homeassistant
    name: "Setpoint Humedad"
    entity_id: sensor.setpoint_humedad
    id: setpoint
    internal: true

i2c:
  sda: D2
  scl: D1

font:
  - file: 'NunitoSans-Regular.ttf'
    id: font1
    size: 17
    
  - file: 'NunitoSans-Regular.ttf'    
    id: font2
    size: 22
    
  - file: 'NunitoSans-Regular.ttf'   
    id: font3
    size: 40   

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    lambda: |-
     it.printf(0, 0, id(font2), "%.1f°C  %.f%%", id(temperatura).state, id(humedad).state);
     it.printf(0, 25, id(font1), "Setpoint"); 
     it.printf(0, 42, id(font1), "Humedad"); 
     it.printf(80, 20, id(font3), "60");  #### number 60 harcoded in order to see how it looks ####   

Thing is that I cannot use either for SSD1306 display (just to replace the number 60, and to compare
the input_number value with id: “humedad” in order to turn on/off the switch “relay”

switch:
  - platform: gpio
    pin: D6
    id: "relay"

thanks in advance

this is how it looks so far

Would a Global Variable help you with this?

All homeassistant sensors are text sensors. If you want to display on a screen - I find it best to convert to a format that you can easily apply a print formatting string to. E.g:

    lambda: |-
     int sp = atoi(id(setpoint).state.c_str());
     it.printf(0, 0, id(font2), "%.1f°C  %.f%%", id(temperatura).state, id(humedad).state);
     it.printf(0, 25, id(font1), "Setpoint"); 
     it.printf(0, 42, id(font1), "Humedad"); 
     it.printf(80, 20, id(font3), "%i", sp);  

Hi, thanks for your time.
I have an error message, sorry but I read a lot and I don’t know how to start with YAML, with arduino IDE no problem at all, but this is quite a problem…

INFO Reading configuration /config/esphome/ducha-suite.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing ducha-suite (board: d1_mini_pro; framework: arduino; platform: platformio/espressif8266 @ 3.2.0)
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 16MB Flash
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
Dependency Graph
|-- <ESPAsyncTCP-esphome> 1.2.3
|-- <ESPAsyncWebServer-esphome> 2.1.0
|   |-- <ESPAsyncTCP-esphome> 1.2.3
|   |-- <Hash> 1.0
|   |-- <ESP8266WiFi> 1.0
|-- <DNSServer> 1.1.1
|-- <ESP8266WiFi> 1.0
|-- <ESP8266mDNS> 1.2
|-- <noise-c> 0.1.4
|   |-- <libsodium> 1.10018.1
|-- <Wire> 1.0
Compiling /data/ducha-suite/.pioenvs/ducha-suite/src/main.cpp.o
/config/esphome/ducha-suite.yaml: In lambda function:
/config/esphome/ducha-suite.yaml:90:37: error: request for member 'c_str' in 'setpoint->esphome::homeassistant::HomeassistantSensor::<anonymous>.esphome::sensor::Sensor::state', which is of non-class type 'float'
   90 |      int sp = atoi(id(setpoint).state.c_str());
      |                                     ^~~~~
*** [/data/ducha-suite/.pioenvs/ducha-suite/src/main.cpp.o] Error 1
========================== [FAILED] Took 9.94 seconds ==========================

thanks again

By the way… this variable kind number will stay into the esp8266 after wifi fall? I mean if wifi is down the number I have chosen in input_number still working as normal, hope this is clear, I am no native english speaker.
Regards

That error seems to imply that the set point value is a float. Hmmm I’m not a C++ expert so only guessing - but try:

    lambda: |-
     float sp = atof(id(setpoint).state.c_str());
     it.printf(0, 0, id(font2), "%.1f°C  %.f%%", id(temperatura).state, id(humedad).state);
     it.printf(0, 25, id(font1), "Setpoint"); 
     it.printf(0, 42, id(font1), "Humedad"); 
     it.printf(80, 20, id(font3), "%.f", sp);  

Sorry - don’t know. You could however check and see if the value is valid before assigning to the variable and if it isn’t use the last known good value.

Sorry still not work, the input_number value is ok into HA dashboard, but I’m frustrating not to show that value into my esp8266

here’s a pic of that value
image

I just did a test compile of one of my displays with input number and it works fine as a string:

  - platform: homeassistant
    name: "Rain since 12am"
    entity_id: input_number.radio_volume
    id: total_rain
          it.printf(235, 290, id(calibri_20), TextAlign::BOTTOM_RIGHT, "%s mm", id(total_rain).state.c_str());

So far so good. I then changed the input number to a field that I do a calculation on:

  - platform: homeassistant
    name: "Current Wind Dir"
    entity_id: input_number.radio_volume
    id: wind_dir
          int dir = atoi(id(wind_dir).state.c_str());

Still no compile errors.

So maybe one of two things:

  • the internal=true is causing an issue OR

  • get rid of the template sensor and access the input_number directly

HA is definetely not for me… print section does not work, stiill errors and not upload, touching here touching there it compiles and wemos star to 5 cicles of power in power out, never seen this, so in short, unfortunately the solution does not work, I was thinking the the device has no more than the entities I’ve created at the very begining, Temperature, Humidity & relay on/off switch, Do I have to see another entities corresponding to the input number jus created?
Best Regards, and sorry to bother you

finally it works, final code below,
but (always there is a but…) I’ve noticed the esp8266 after restart it does not have the last input_number I can see on the display nan, efter a while setpoint appears, I should look for a solution to this, also I can not use the setpoint (input_number) to replace the harcoded 60 and 58 into this sketch

     on_value_range:
      - above: 60.0
        then:
          - switch.turn_on: "relay"
      - below: 58.0
        then:
          - switch.turn_off: "relay"

final code so far…

switch:
  - platform: gpio
    pin: D6
    id: "relay"
    name: "Extractor Ducha"
    inverted: true
    
sensor:
  - platform: dht
    model: "DHT11"
    pin: D3
    temperature:
     id: temperatura
     name: "Temperatura Ducha"
    humidity:
     id: humedad
     name: "Humedad Ducha"
    
     on_value_range:
      - above: 60.0
        then:
          - switch.turn_on: "relay"
      - below: 58.0
        then:
          - switch.turn_off: "relay"
    update_interval: 10s

  - platform: homeassistant
    name: "Setpoint Humedad"
    entity_id: input_number.setpoint_humedad
    id: setpoint
    internal: true

i2c:
  sda: D2
  scl: D1

font:
  - file: 'NunitoSans-Regular.ttf'
    id: font1
    size: 17
    
  - file: 'NunitoSans-Regular.ttf'    
    id: font2
    size: 22
    
  - file: 'NunitoSans-Regular.ttf'   
    id: font3
    size: 40   

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    lambda: |-
     it.printf(0, 0, id(font2), "%.1f°C  %.f%%", id(temperatura).state, id(humedad).state);
     it.printf(0, 25, id(font1), "Setpoint"); 
     it.printf(0, 42, id(font1), "Humedad"); 
     it.printf(80, 20, id(font3), "%.f", id(setpoint).state); 

Thank you for your patience!!!

1 Like

If you want a value to be saved between restarts, you can use a global variable.

On ESP8266 this is a little bit more complicated than on ESP32 but achievable.

You would:

  • Create a global variable
  • Turn on restore from flash in the ESP8266 config
  • When you get a valid value for your set point from HA, store it in that global variable
  • On restart - until the value from the HA sensor is updated, use the stored value in the global variable.

I, thanks again…
Do you have an example, in my opinion github, esphome, and others source of resources are for rocket scientist… I dont know why there is so much complicated… there are many many people able to write an small example, but no… there are youtube videos that you can not follow because the creator video “mises” some important path or click here, there, Im strugling trying to setup dwayne lovelace control panel and I can not find some paths files, etc…