Appropriate use of platform: template


I consider myself an ESPHome amateur at this point. I have flashed a few light bulbs and smart outlets with ESPHome using the recommended configurations and now I have a completely from scratch module using a WeMos D1 Mini mostly working.

I keep running into something that I hope someone here can help provide some insight on:

Every time I want to create a component, such as a binary_sensor, switch, button, or text_sensor, that is not connected to a physical thing (i.e. not gpio), I never know what to put in the platform attribute. I end up using “template” and it always seems to work but it seems to be overkill. I understand template to be primarily used with lambdas and I usually don’t need a lambda. I do tend to shy away from lambdas - not because I am afraid of C/C++ - though it has been about 20 years and C and I never got along well anyway :slight_smile: , it just seems that it is more direct/more maintainable/less brittle to use existing actions. I know platform: template can be used without a lambda but I was wondering if there is something more appropriate. What I usually want is simply a sensor to hold state and be visible from Home Assistant.

Some examples:

  - platform: template
    name: "$friendly_name Button 1"
        - script.execute: openclose_big_gate

Could this ^^^ be something other than template?

Another example (this one I have not solved yet - haven’t figured out how to set the value):

  - platform: template
    id: ${id_prefix}_current_action
    name: "${friendly_name} Current Action"

I want this to be a short text description of the Action being executed, for example “Activating Big Gate”.
Then in a script that activates the big gate:

  - id: openclose_big_gate
      - logger.log: "OpenCloseBigGate"
# Don't know what to put here? - text_sensor.${id_prefix}_current_action.set_value: "Activating Big Gate"
      - light.turn_on: built_in_led_light
      - switch.turn_on: big_gate_relay
     # etc...

I am wondering if a platform besides template will make it easier for me to simply set the text value of the sensor at the appropriate time in each script.

Thanks for any assistance!


Using your first example, your choices are here

i.e. template, factory-reset, generic-output, restart, safe mode, shutdown, uart, wake on lan. (Core is just the generic button stuff which shows the stuff that is common to all buttons)

So unless you can find something more appropriate to your use case, template seems to be right.

Don’t forget that a template refers to the automation via esphome, as well as c++ lambdas.

IMHO if need for button/switch/sensor not connected to any physical stuff - platform: template is a single choice. You can use lambda to calculate state of entity or publish state from script etc.

Like publish texts to text_sensor:

Thank you both! This is what I needed.

Specifically what I was missing was:

{sensor type}.template.publish:
  id: sensor_id
  state: whatever

With that bit of info, the template sensors all make sense - and I don’t need to use a lambda except for complex situations.