Receiving, Changing, Sending helper values HA <-> Esphome

Hi there,

In my esphome script I try to get the value of a home assistant helper, change it on device and send the calculated value back to home assistant.
In this case a have a relay with pumps attached on it. Every time I activate the relay I take the starting time. On turn off I fetch the water level (mentioned helper), calculate the consumption and I want to write back the new calculated water level back to home assistant. When I refill the water tank I want to reset that helper back to the new level.

What I did so far:

  1. I configured a helper in home assistant

  2. Here my esphome yml

globals:
  - id: water_consumption_per_sec_in_ml
    type: int
    # 23,2 p/sec
    initial_value: '24'
  - id: pump_1_start_time
    type: time_t
time:
  - platform: homeassistant
    id: ha_time
    timezone: Europe/Berlin
switch:
  - platform: gpio
    id: pump_1
    name: "Pumpe 1"
    pin:
      number: 26
      mode: OUTPUT
      inverted: true
    on_turn_on:
      lambda: |-
        id(pump_1_start_time) = id(ha_time).now().timestamp;
     on_turn_off:
      lambda: |-
        int consumption = (id(ha_time).now().timestamp - id(pump_1_start_time)) * id(water_consumption_per_sec_in_ml);
        int waterLevel = id(water_level_tank_1).state - consumption;
        id(water_level_tank_1).publish_state(waterLevel);
sensor:
  - platform: homeassistant
    id: water_level_tank_1
    entity_id: input_number.water_level_tank_1

The value on home assistant backend doesn’t change after publish_state. So my question is: Is this two way binding possible and how could I solve this issue?

Best regards
i7i5

Yes there was a very recent thread, where we solved it in the last few posts!

The solution is to use the api to send data back. Weird Problem: Text not updating from HA sensors, all other sensor are fine - #20 by nickrout

1 Like

Thanks a lot, Ill check it out tomorrow :slight_smile:

Edit
I have read the whole post mentioned above but wasn’t sure if this is the solution for my issue. Because of the links posted there I did again a bit more research and ended up with the following solution:

I configured the number input within esphome:

number:
  - platform: template
    id: water_level_tank_1
    name: water lvl
    step: 1
    max_value: 3000
    min_value: 0
    initial_value: 0
    optimistic: true

image

The rest stays the same as posted above, publish_state now works as expected (id(water_level_tank_1).publish_state(waterLevel);). Not sure why this does work and the sensor one doesn’t, maybe because of the optimistic flag :woman_shrugging: