Number value not stored as set

Hi,

I am trying to use a number as user input from HA and use the value within an ESPHome program. However, it seems as the value, after set with either the HA GUI (Integrations → ESPHome device → input field) or the service number.set, is only updated in the lambda variable “x” but not as the actual state.

The config is as below:

number:
  - platform: template
    name: $device_name Light 0
    id: light0_symbol
    entity_category: config
    mode: box
    min_value: 1
    max_value: 6
    step: 1
    set_action:
      then:
        - lambda: |-
            byte symbol = 2 * x;
            symbol += 20;
            if (id(light0).state) {
              symbol += 1;
            }
            id(disp1).send_command_printf("Lights.light0.pic=%i", symbol);
            ESP_LOGW("main", "Number set to: %f (%f)", id(light0_symbol).state, x);

The symbol code is not relevant here, but the bottom line in the lambda shows the challenge. Whenever the value is changed, I receive the following in the log:

14:42:45][D][number:010]: 'nspanel-dev Light 0' - Setting
[14:42:45][D][number:029]:   Value: 2.000000
[14:42:45][W][main:807]: Number set to: 1.000000 (2.000000)
[14:42:55][D][number:010]: 'nspanel-dev Light 0' - Setting
[14:42:55][D][number:029]:   Value: 3.000000
[14:42:55][W][main:807]: Number set to: 1.000000 (3.000000)
[14:45:20][D][number:010]: 'nspanel-dev Light 0' - Setting
[14:45:20][D][number:029]:   Value: 2.000000
[14:45:20][W][main:807]: Number set to: 1.000000 (2.000000)
[14:45:27][D][number:010]: 'nspanel-dev Light 0' - Setting
[14:45:27][D][number:029]:   Value: 2.000000
[14:45:27][W][main:807]: Number set to: 1.000000 (2.000000)

When I read the value later on with id(light0_symbol).state it is always 1.

What am I missing? Why is not the “actual” state updated but only x variable in the lambda part?

I can’t speak to the actual behavior, why it is that way but can you use call.set_value(x); to set x as the value?

Hi,

Thank you for your reply!

I have tried to implement that in the code with a check that the call is only made if the state of the number object is different than the new value (x). The intention of this is to avoid an infinite loop, as the call will update the value if I understand it correctly. However, the result is a infinite loop.

The code:

  - platform: template
    name: $device_name Light 0
    id: light0_symbol
    entity_category: config
    mode: box
    min_value: 1
    max_value: 6
    step: 1
    set_action:
      then:
        - lambda: |-
            if (id(light0_symbol).state != x) {
             auto call = id(light0_symbol).make_call();
             call.set_value(x);
             call.perform();
              byte symbol = 2 * x;
              symbol += 20;
              if (id(light0).state) {
                symbol += 1;
              }
              id(disp1).send_command_printf("Lights.light0.pic=%i", symbol);
              ESP_LOGW("main", "Number set to: %f (%f)", id(light0_symbol).state, x);
            }

The result:

[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
[12:32:42][D][number:010]: 'nspanel-dev Light 0' - Setting
[12:32:42][D][number:029]:   Value: 2.000000
INFO nspanel.local: Error while reading incoming messages: Error while reading data: [Errno 104] Connection reset by peer
INFO Disconnected from ESPHome API for nspanel.local
WARNING Disconnected from API
INFO nspanel.local: Ping Failed: Error while reading data: [Errno 104] Connection reset by peer
INFO Successfully connected to nspanel.local

The infinite loop seem to start immediately and crashes the ESP and the OTA logs terminal. :frowning:

That makes sense, the looping. How about setting the value to a global or another number, then use that for your automations instead?

I think that is the easiest approach. However, I think it’s a pity to not be able to use the input number (or input select, same issue there). I want to use it for a setting, and now the actual value cannot be seen in the same selector as where the setting is saved… :frowning: