ESPHome Global Variables as Entities

Hi,

I am using a small display with ESP32-S3 that I bought recently, It is Working great. But to control and display more stuff on the Display, I need a global variable that I need to control from the home assistant dashboard. Like,

  1. I want to show a Custom Text on a field I dedicated
  2. Change the Time Format to 12/24 Hrs using a toggle on HA Dashboard
    etc.

I thought of using globals in ESPHome but that doesn’t expose the variable to homeassistant.

Sample YAML

globals:
  - id: Temperature_Unit
    type: int
    restore_value: False
    initial_value: "0" 
    
  - id: Message
    type: std::string
    restore_value: False
    initial_value: "Hi"  

I also tried some advice on the Forum as follows,

sensor:
  - platform: homeassistant
    entity_id: input_number.Temperature_Unit
    name: Temperature_Unit
    id: Temperature_Unit_Set
    on_value:
      then:
        - lambda: !lambda |-
            id(Temperature_Unit) = id(Temperature_Unit_Set).state;
            ESP_LOGW("new Unit","Temperature_Unit: %.1f Unit: %d", id(Temperature_Unit_Set).state, id(Temperature_Unit));

Still No entity on HA.

I am currently not OK with solution with service because, I couldn’t bring that easily on the dashboard. Suppose you can help me with that. I may proceed with that.

A global is a storage point not meant to be directly interacted with. It’s somewhere you can store a value and have it persist through restarts.

So for example if you want to store a number, first create a number component in esphome. Under the number add an automation with the trigger on_value and write the number value to the global.

Then add an on_boot automation, so when the board restarts it will set the number component to the global value. Now the number component will represent the global value.

A text entity is like a text_sensor that can read a value from a device, but is useful when that value can be set by the user/frontend.

The template switch platform allows you to create simple switches out of just actions and an optional value lambda. Once defined, it will automatically appear in Home Assistant as a switch and can be controlled through the frontend.