How can I update a variable in Home Assistant

My ESP32 code can read a variable from HA, but I can’t figure out how to update it with a new value.

I have tried: internal: false, and I have checked the box “Allow the device to perform Home Assistant actions.”

Code:

captive_portal:
    
number:
  - platform: homeassistant
    id: my_test_number
    internal: false
    entity_id: input_number.TestNumber

interval:
  - interval: 15s
    then:
      - script.execute: values_update

script:
   - id: values_update
     then:
        - lambda: |-
            ESP_LOGD("-------my_test_number", "my_test_number: %.2f", id(my_test_number).state);
            id(my_test_number).state = 234;
            ESP_LOGD("----------my_test_number", "my_test_number: %.2f", id(my_test_number).state);

Output:

[17:31:48][D][homeassistant.number:022]: 'input_number.testnumber': Got state 123.0
[17:31:48][D][number:012]: 'my_test_number': Sending state 123.000000
[17:31:48][C][api:140]: API Server:
[17:31:48][C][api:141]:   Address: numbertest.local:6053
[17:31:48][C][api:143]:   Using noise encryption: YES
[17:31:48][C][homeassistant.number:072]: Homeassistant Number 'my_test_number'
[17:31:48][C][homeassistant.number:073]:   Entity ID: 'input_number.testnumber'
[17:31:48][D][homeassistant.number:032]: 'my_test_number': Min retrieved: 0.0
[17:31:48][D][homeassistant.number:042]: 'my_test_number': Max retrieved: 10000.0
[17:31:48][D][homeassistant.number:052]: 'my_test_number': Step Retrieved 1.0
[17:31:53][D][-------my_test_number:048]: my_test_number: 123.00
[17:31:53][D][----------my_test_number:050]: my_test_number: 234.00

It gets updated locally, but doesn’t get written back to HA. What do I have to do to update the value in HA?

Are you asking how to update the value from the ESPHome device?

Number Component - Automations

There are a few actions like number.set and number.increment, but I can’t find any indication that there is a way to make them work with an input_number… only number entities seem to work.

For input_number entities I think you have to use a homeassistant_action:

number:
  - platform: homeassistant
    id: my_test_number
    internal: false
    entity_id: input_number.TestNumber

interval:
  - interval: 15s
    then:
      - script.execute: values_update

script:
   - id: values_update
     then:
        - lambda: |-
            ESP_LOGD("-------my_test_number", "my_test_number: %.2f", id(my_test_number).state);
        - homeassistant.action:
            action: input_number.set_value
            data:
              entity_id: input_number.TestNumber
              value: "23"

Try to replace that with:

auto call = id(my_test_number).make_call();
call.set_value(234);
call.perform();

This worked. Many thanks!

This doesn’t work for me:

auto call = id(my_test_number).make_call();
call.set_value(234);
call.perform();

It doesn’t compile for a sensor:, if I change it to a number: it compiles but doesn’t update the value on HA. Is there something else I need to do?

No because it’s for number.

It does what you tried to do here.