How to pass numeric value to esphome - SOLVED

Hello,

this is driving me crazy for days now!

I have a ESP D1 mini connected to a Victron IP22 battery charger in order to control the charging current. The control from esphome via UART is working fine.

But I did not manage to pass the current as a numerical value from homeassistant!
I have found a couple of threads explaining this, and I think I followed the examples to setup the HA side as well as the esphome side.

Here is the homeassistant part that defines an input number and a sensor that gets the value. This part seem to work, as I can set values with the GUI slider and see the senbsor state change in developer tools page.

Homeassistant configuration.yaml:

input_number:
  num_charger_current:
    name: AC Charger Current
    initial: 3
    min: 3
    max: 12
    step: 1

sensor:
  # This is a sensor to be read by esphome to control the charging current
  - platform: template
    sensors:
      ac_charger_current:
        value_template: "{{ states('input_number.num_charger_current') | int }}"
        unit_of_measurement: "A"

Here is the esphome side.
I defined the sensor of the homeassistant side (platform: homeassistant) referenced the entity id.
In order to check, if the value is passed, I defined another template sensor, that is displayed in the web interface. However this sensor is always showing “NAN”, regardless what value I set with the slider.

esphome yaml:

sensor:
  # This is to get the charging current from HA
  - platform: homeassistant
    name: "AC Charger Current"
    entity_id: sensor.ac_charger_current
    id: id_ac_charger_current
    unit_of_measurement: 'A'

  # Show value of charging current received from HA
  - platform : template
    name: charge current received
    unit_of_measurement: 'A'
    lambda: |-
      return { id(id_ac_charger_current).state };
    update_interval: 1s

I wonder, how I can verify, if the value is actually passed via the api interface?
I did set the esphome logger level to DEBUG, but see no output, if I move the slider.

Can someone please have a look at the code for any mistakes or guide me on how to debug the transfer of the value via the api?

I deeply appreciate any help on this issue.
Thank you.
nob

Edit: Maybe this is helpful: I noticed that there are API connection errors every minute or so:

WARNING victron-mppt @ 192.168.181.81: Connection error occurred: [Errno 104] Connection reset by peer
INFO Processing unexpected disconnect from ESPHome API for victron-mppt @ 192.168.181.81
WARNING Disconnected from API
INFO Successfully connected to victron-mppt @ 192.168.181.81 in 0.005s
INFO Successful handshake with victron-mppt @ 192.168.181.81 in 3.011s

I don’t think you need that template sensor to see if the value is passed, just look at the ESPHome log for that device. You should see something like this:

[16:17:45][D][homeassistant.sensor:024]: ‘input_number.wmin_pause’: Got state 11.00
[16:17:45][D][sensor:094]: ‘wmin_pause_set’: Sending state 11.00000 with 1 decimals of accuracy

Please post your log and complete esp yaml, so we can help you in a better way.

Hi Jos,

sure, here they are.

esp yaml:

esphome:
  name: victron-ve-direct
  friendly_name: victron-ve-direct

esp8266:
  board: d1_mini

# Enable logging
logger:
  baud_rate: 0
  esp8266_store_log_strings_in_flash: false
  level: DEBUG # NONE, ERROR, WARN, INFO, DEBUG (default), VERBOSE, VERY_VERBOSE
  logs:
    sensor: DEBUG

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

ota:
  password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

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

captive_portal:

web_server:
  port: 80

time:
  - platform: homeassistant

sensor:
  # This is to control the charging current from HA
  - platform: homeassistant
    name: "AC Charger Current"
    entity_id: sensor.ac_charger_current
    id: id_ac_charger_current
    unit_of_measurement: 'A'
    on_value:
      then:
        - lambda: !lambda |-
            ESP_LOGW("main", "new value received %f", id(id_ac_charger_current).state);


  # Show value of charging current received from HA
  - platform : template
    name: charge current received
    #id: charge_current
    unit_of_measurement: 'A'
    lambda: |-
      return { id(id_ac_charger_current).state };
    update_interval: 5s
 

During runtime I changed several time the number value in HA GUI, but seems there is nothing received by esp:

INFO ESPHome 2023.12.9
INFO Reading configuration /config/esphome/victron-ve-direct.yaml...
INFO Detected timezone 'Europe/Berlin'
INFO Generating C++ source...
INFO Compiling app...
Processing victron-ve-direct (board: d1_mini; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
Dependency Graph
|-- ESPAsyncTCP-esphome @ 2.0.0
|-- ESPAsyncWebServer-esphome @ 3.1.0
|-- DNSServer @ 1.1.1
|-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|-- noise-c @ 0.1.4
|-- ArduinoJson @ 6.18.5
Compiling .pioenvs/victron-ve-direct/src/main.cpp.o
Linking .pioenvs/victron-ve-direct/firmware.elf
RAM:   [=====     ]  49.8% (used 40784 bytes from 81920 bytes)
Flash: [=====     ]  48.8% (used 509721 bytes from 1044464 bytes)
Building .pioenvs/victron-ve-direct/firmware.bin
esp8266_copy_factory_bin([".pioenvs/victron-ve-direct/firmware.bin"], [".pioenvs/victron-ve-direct/firmware.elf"])
========================= [SUCCESS] Took 22.07 seconds =========================
INFO Successfully compiled program.
INFO Connecting to 192.168.181.81
INFO Uploading /data/build/victron-ve-direct/.pioenvs/victron-ve-direct/firmware.bin (513872 bytes)
INFO Compressed to 360412 bytes
Uploading: [============================================================] 100% Done...

INFO Upload took 3.95 seconds, waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from 192.168.181.81 using esphome API
INFO Successfully connected to victron-ve-direct @ 192.168.181.81 in 31.615s
INFO Successful handshake with victron-ve-direct @ 192.168.181.81 in 2.444s
[16:38:16][I][app:102]: ESPHome version 2023.12.9 compiled on Feb 10 2024, 16:37:18
[16:38:16][C][wifi:573]: WiFi:
[16:38:16][C][wifi:405]:   Local MAC: xx:xx:xx:xx:xx:xx
[16:38:16][C][wifi:410]:   SSID: 'Jabahudu2.4GHz'[redacted]
[16:38:16][C][wifi:411]:   IP Address: 192.168.181.81
[16:38:16][C][wifi:412]:   BSSID: xx:xx:xx:xx:xx:xx[redacted]
[16:38:16][C][wifi:414]:   Hostname: 'victron-ve-direct'
[16:38:16][C][wifi:416]:   Signal strength: -47 dB ▂▄▆█
[16:38:16][C][wifi:420]:   Channel: 1
[16:38:16][C][wifi:421]:   Subnet: 255.255.255.0
[16:38:16][C][wifi:422]:   Gateway: 192.168.181.1
[16:38:16][C][wifi:423]:   DNS1: 192.168.181.29
[16:38:16][C][wifi:424]:   DNS2: 0.0.0.0
[16:38:16][C][logger:439]: Logger:
[16:38:16][C][logger:440]:   Level: DEBUG
[16:38:16][C][logger:441]:   Log Baud Rate: 0
[16:38:16][C][logger:443]:   Hardware UART: UART0
[16:38:16][C][logger:447]:   Level for 'sensor': DEBUG
[16:38:16][C][template.sensor:022]: Template Sensor 'charge current received'
[16:38:16][C][template.sensor:022]:   State Class: ''
[16:38:16][C][template.sensor:022]:   Unit of Measurement: 'A'
[16:38:16][C][template.sensor:022]:   Accuracy Decimals: 1
[16:38:16][C][template.sensor:023]:   Update Interval: 5.0s
[16:38:16][C][homeassistant.time:010]: Home Assistant Time:
[16:38:16][C][homeassistant.time:011]:   Timezone: 'CET-1CEST,M3.5.0,M10.5.0/3'
[16:38:16][C][captive_portal:088]: Captive Portal:
[16:38:16][C][web_server:168]: Web Server:
[16:38:16][C][web_server:169]:   Address: victron-ve-direct.local:80
[16:38:16][C][mdns:115]: mDNS:
[16:38:16][C][mdns:116]:   Hostname: victron-ve-direct
[16:38:16][C][ota:097]: Over-The-Air Updates:
[16:38:16][C][ota:098]:   Address: victron-ve-direct.local:8266
[16:38:16][C][ota:101]:   Using Password.
[16:38:16][C][api:139]: API Server:
[16:38:16][C][api:140]:   Address: victron-ve-direct.local:6053
[16:38:16][C][api:142]:   Using noise encryption: YES
[16:38:16][C][homeassistant.sensor:030]: Homeassistant Sensor 'AC Charger Current'
[16:38:16][C][homeassistant.sensor:030]:   State Class: ''
[16:38:16][C][homeassistant.sensor:030]:   Unit of Measurement: 'A'
[16:38:16][C][homeassistant.sensor:030]:   Accuracy Decimals: 1
[16:38:16][C][homeassistant.sensor:031]:   Entity ID: 'sensor.ac_charger_current'
[16:38:18][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:38:23][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:38:28][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:38:33][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:38:38][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:38:43][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:38:48][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:38:53][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:38:58][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:39:03][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:39:08][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:39:13][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:39:18][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy
[16:39:23][D][sensor:093]: 'charge current received': Sending state nan A with 1 decimals of accuracy

I have just found the solution for my Problem:

I had not added the device to homeassistant :pensive: because I did not want HA to create entities as long as I am in experimental phase.
I just use the esphome web gui, but api was just not available.
So I fooled myself - stupid!

Now when I change the value with the slider, it immediately shows up in esphome.

Yeah, that happens to all of us sometimes :man_facepalming:

I am very new to HA and trying to get my IP22 charger connected…I don’t fully follow all the steps you did for this. I have my Victron SmartShunt connected to HA via ESP so am trying to add the Victron charger. Could you post all of your .yaml and whatever else you had to do for this? Thanks.

Hi Tony,
Let me ask some questions first:
How did you physically connect the SmartShunt to ESP?
Have you already connected the IP22 charger via its internal serial serial pins?

Actually I am no longer using the IP22 chargers, but maybe I still have the code somewhere…
Nob