I just went through this same frustration. My goal was to have an automation use some numbers from other entities, do some math on them, and display the result in the UI. Seems simple enough, right?
Using an input_number template would certainly work, but when it’s displayed in the UI it’s presented as a box into which the user can type, well, input. Not what I wanted.
I could have created an input_number template, then another template which takes its value from that first one whenever it changes. This would also work, but it seems silly to create two templates just to maintain and display one.
These limitations steered me to trigger-based templates. Since these can do similar logic to what an automation can, this seemed like the simplest solution. Unfortunately I found documentation on these very limited, and even a couple of AI bots struggled to get the syntax correct.
It’s kind of an odd-ball project, but I’ll post my YAML here in case anyone is interested. I have three sump pumps. The sumps are kinda small and the pumps large, so they don’t stay running long. Each time one kicks on, it pumps a fixed amount of water before it kicks off a few seconds later. I wanted to know how many gallons they’ve pumped today:
#
template:
- trigger:
- platform: state
entity_id:
- binary_sensor.east_pump_cycle_on
- binary_sensor.west_pump_cycle_on
- binary_sensor.bkup_pump_cycle_on
to: 'on'
- platform: time
at: '23:59:30'
sensor:
- name: "Gallons Pumped Today"
unique_id: gallons_pumped_today
state: >
{% set current_value = states('sensor.gallons_pumped_today') | float(0) %}
{% if now().hour == 23 and now().minute == 59 %}
0
{% elif trigger.entity_id == 'binary_sensor.east_pump_cycle_on' %}
{{ (current_value + 4.625) }}
{% elif trigger.entity_id == 'binary_sensor.west_pump_cycle_on' %}
{{ (current_value + 8.125) }}
{% elif trigger.entity_id == 'binary_sensor.bkup_pump_cycle_on' %}
{{ (current_value + 8.125) }}
{% endif %}
unit_of_measurement: "G"
I could walk you through the code, but this post is already getting long. If anyone is interested in why things were done this way, I’ll respond. Or, if someone has a better way, I’d like to hear it.
One thing I will add is that creating lots of template entities risks spamming the Recorder database. Be sure to review your Recorder excludes and includes after creating any new entities!