ESPHome variables set from home assistant

Hey! I’m sure this topic has been covered before, but I’ve been troubleshooting this issue for a month and I just can’t wrap my head around it.

I want to change a global variable in my ESPHome code from Home Assistant. I’ve tried many examples online, but none of them seem to work for me.

My current code is this:

globals:
  - id: current_value
    type: int
    restore_value: false
    initial_value: '10'

sensor:
  - platform: homeassistant
    id: ha_sensor
    entity_id: input_number.test_int
    on_value:
      then:
        - lambda: |-
            id(current_value) = (int)x;
            ESP_LOGI("main", "Value changed, it is now: %d", id(current_value));
            
interval:
  - interval: 3s
    then:
      - lambda: |-
          ESP_LOGI("main", "Input value: %d", id(current_value));

Do you have any idea what might be the issue?

Why do you use a global?
Can’t you just use the number entity?

Add a number component that is tied to the global. When the value of the number is updated set it to the global value.

Thank you for the reply Hellis, I need the global because I want the Esp to store the value once set and be independent from HA.

Thanks Mikefila, could you help me out understand how it would look like? Because I have tried this

globals:
  - id: current_value
    type: int
    restore_value: false
    initial_value: '10'

number:
  - platform: template
    id: value_from_HA
    min_value: 0
    max_value: 24
    step: 1
    set_action:
      - lambda: |-
          id(current_value) = (int)x;
          ESP_LOGI("main", "Number updated, current_value is now: %d", id(current_value));

interval:
  - interval: 3s
    then:
      - lambda: |-
          ESP_LOGI("main", "Global value: %d", id(current_value));


but I still get this output:

[15:42:03.792][I][main:051]: Global value: 10
[15:42:06.795][I][main:051]: Global value: 10
[15:42:09.796][I][main:051]: Global value: 10
[15:42:12.797][I][main:051]: Global value: 10
[15:42:15.798][I][main:051]: Global value: 10
[15:42:18.800][I][main:051]: Global value: 10
[15:42:21.801][I][main:051]: Global value: 10
[15:42:24.800][I][main:051]: Global value: 10
[15:42:27.801][I][main:051]: Global value: 10
[15:42:30.802][I][main:051]: Global value: 10
[15:42:33.803][I][main:051]: Global value: 10
[15:42:36.803][I][main:051]: Global value: 10
[15:42:39.806][I][main:051]: Global value: 10
[15:42:42.812][I][main:051]: Global value: 10
[15:42:45.818][I][main:051]: Global value: 10

Even if i change the input val.

try

number:
  - platform: template
    id: value_from_HA
    min_value: 0
    max_value: 24
    step: 1
    on_value:
      - globals.set:
          id: current_value
          value: !lambda 'return id(value_from_HA).state;'
1 Like

It will be independent and be stored on the ESP.

It will be just as independent and as much stored on the device as a global is at least.

You aren’t doing anything that will make a difference more than adding complexity where it isn’t needed

1 Like

Try like this:

number:
  - platform: template
    name: "My  Number"
    lambda: |-
      return id(current_value);
    set_action:
      then:
        - lambda: |-
            id(current_value) = x;
1 Like

Thank you all for the great support!

@Karosm i have tried what you propose but still wouldn’t update.

@Mikefila Your code neither seems to work :frowning_face:

@Hellis81 thank you for the explanation, there is still much I do not understand of EspHome, could you share how the code would look like? So maybe I can finally make it work :wink:

The helper in HA is input_number.test_int

Hard to understand why it wouldn’t.
So what you get in logs when you change the “My Number” number?

Is this what you where envisioning?

globals:
  - id: current_value
    type: int
    restore_value: false
    initial_value: '10'

sensor:
  - platform: homeassistant
    id: ha_sensor
    entity_id: input_number.test_int
    on_value:
      then:
        - lambda: |-
            id(current_value) = (int)x;
            ESP_LOGI("main", "Value changed, it is now: %d", id(current_value));

number:
  - platform: template
    id: my_number
    name: "My Number"
    min_value: 0
    max_value: 100
    step: 1
    lambda: |-
      return id(current_value);
    set_action:
      - lambda: |-
          id(current_value) = (int)x;
          ESP_LOGI("main", "Template number set to %d", id(current_value));

interval:
  - interval: 3s
    then:
      - lambda: |-
          ESP_LOGI("main", "Input value: %d", id(current_value));

I get this response over and over:

[17:22:27.447][I][main:064]: Input value: 10
[17:22:30.453][I][main:064]: Input value: 10
[17:22:33.459][I][main:064]: Input value: 10
[17:22:36.460][I][main:064]: Input value: 10
[17:22:39.461][I][main:064]: Input value: 10
[17:22:42.464][I][main:064]: Input value: 10
[17:22:45.466][I][main:064]: Input value: 10
[17:22:48.466][I][main:064]: Input value: 10
[17:22:51.466][I][main:064]: Input value: 10
[17:22:54.467][I][main:064]: Input value: 10
[17:22:57.470][I][main:064]: Input value: 10
[17:23:00.474][I][main:064]: Input value: 10
[17:23:03.475][I][main:064]: Input value: 10
[17:23:06.477][I][main:064]: Input value: 10
[17:23:09.481][I][main:064]: Input value: 10

No, I referred to this:

It gives you My Number on HA, and if that doesn’t change the state of the global, I would be surprised.

No HA sensor needed.

I am sorry but I don’t think I am getting you, what should this do? Should it go like this?

globals:
  - id: current_value
    type: int
    restore_value: false
    initial_value: '10'

number:
  - platform: template
    name: "My  Number"
    min_value: 0
    max_value: 24
    step: 1
    lambda: |-
      return id(current_value);
    set_action:
      then:
        - lambda: |-
            id(current_value) = x;

interval:
  - interval: 3s
    then:
      - lambda: |-
          ESP_LOGI("main", "Input value: %d", id(current_value));

What is the expected outcome? Where does the helper input_number.test_int come into play?

It doesn’t.
You use directly the number component (My Number).

Ok so what should be outcome be? What I do no understand what we would look for, can you help me understand?

Number entity, a slider that you control…

Ahhh… sorry I didn’t know, once installed on the EspHome should it appear in the Home Assitant entities?

For now I flashed it

number:
  - platform: template
    name: "My  Number"
    id: my_value
    min_value: 0
    max_value: 24
    step: 1
    optimistic: True

interval:
  - interval: 3s
    then:
      - lambda: |-
          ESP_LOGI("main", "Input value: %d", id(my_value));

and it logs:

[17:56:40.205][I][main:043]: Input value: 1070186556
[17:56:43.208][I][main:043]: Input value: 1070186556
[17:56:46.210][I][main:043]: Input value: 1070186556
[17:56:49.213][I][main:043]: Input value: 1070186556
[17:56:52.214][I][main:043]: Input value: 1070186556

So you don’t like the code I proposed?

What you have here below is what I thought of, except without the interval.

This makes no sense at all!
You said it should be on the ESP and not on Home Assistant.

As Hellis initially mentioned, you shouldn’t normally need to use a global or an input_helper.
All your ESPHome components are shown on the HA > settings > integrations > ESPHome > [devicename] page, and they can be changed from there or via lovelace panels. Whenever you change a value on your HA display panel it is automagically updated on the ESP32, and so you don’t need to store a copy of it.

That works because ESP devices are normally always available.
But there are scenarios where you might need the ESP32 to run when it is not connected to HA. If so, please let us know more about your project so we can give more focussed advice.

Actually this is what happens when you use deep_sleep on the ESP32 … because the ESP32 is physically powered off when it is “asleep”, HA cannot contact it to update variables. And then you find yourself furiously trying to make changes in the brief time when the ESP32 is awake.

In this case (and I guess this is what you actually want to know)…

  1. In HA set up your input helper - you said yours is input_number.test_int.

  2. In ESPHome, setup a sensor (or other component which allows the homeassistant platform). The entity_id: is what links this ESPHome component to the input_helper in HA.

sensor:
  - platform: homeassistant
    id: ha_sensor
    entity_id: input_number.number_test
    on_raw_value:
      then:
        - logger.log:
            format: "**********  ha_sensor  on_raw_value  ***** x=%f "
            args: [ 'x' ]
  1. That’s it. When the ESP32 is awake it checks the current value of number_test from HA and synchronises with it. Just use ha_sensor as you normally would.

i have used Global variables for retaining values through a period of deep_sleep.


Looking back at the code you posted in your first post … I don’t see anything wrong with it - which suggests the problem was elsewhere.