Home Assistant’s standard cards don’t support templates, which are needed for performing calculations (the sole exception is the Markdown card). Custom cards may support templating but I can’t comment on which ones do/don’t.
Understood, makes sense. Still seems a lot of work/overhead to create sensors for the more simple maths like division/subtraction etc.
I just edited my post, as one of the values did actually hold unknown. I tested this now and it works:
# Second Bathroom Shower In Use On Time Average
- sensor:
- name: "Second Bathroom Shower In Use On Time Average"
unique_id: Second Bathroom Shower In Use On Time Average
state: >-
{{ states('sensor.second_bathroom_shower_in_use_on_time') | float(0) / states('sensor.second_bathroom_shower_in_use_count') | float(0) }}
I am however running into a problem with the sensor if it hasn’t recorded anything yet and the value is unknown, and divides by zero. Maybe some sort of availability sensor is the most elegant way here. Or even just setting a default value for a sensor (a history_stats platform in this case).
In that case, the int(0) filter substitutes the value 0 and that’s probably why it produced a divide-by-zero error. Your template should be designed to guard against receiving unknown and unavailable values. Typically filters like int and float guard against it because they can be configured to supply a default value (in this example the default is zero). However when used in a calculation, a default value of zero for a divisor will, of course, cause an arithmetic error.
The template you posted is invalid (logic error) and the option’s name is incorrect for a Template Sensor defined in modern format.
I suggest you try what I suggested in my previous post. The only reason it may need to be enhanced is if the value of sensor.second_bathroom_shower_in_use_count can be zero under normal circumstances. In other words, a count of zero is a nominal value. Is that a possibility?
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.
FYI, you initially marked my post with the Solution tag, then marked your own post with the Solution tag. Only one post in the entire thread can bear the Solution tag so now it’s on your post (with the unnecessary list comparison).
It’s customary to assign it to the post that was first to suggest the solution (as explained in the FAQ).
Sorry to reawaken the thread but I landed here with a very similar question
I want to track how long my hot water is on each day
I’ve created the History Stats sensor
#Track Hot Water History
- platform: history_stats
name: Hot Water ON today
entity_id: sensor.hot_water
state: "On"
type: time
start: "{{ now().replace(hour=0, minute=0, second=0) }}"
end: "{{ now() }}"
Now, I may be misunderstanding this, but that reads to me that it will only give me the time today from midnight until now…
And having only just set it up it seems this is what it’s doing, since it has no data for past days:
This is fine, but what I really want to do is track the total for a 24hr period and have that stored day after day (in the standard history) so that I can see that today it was on for 3 hours, yesterday 2 hours etc
I’m guessing that if I used
start: "{{ 0 }}"
end: "{{ now() }}"
It would simply give me the sum total from all time, which isn’t what i want either
I actually found a way to do this after posting which I wasn’t sure would work but have just found that it does… Frustratingly can’t find the post to link it now, but basically;
I created an apex graph card which does the tracking with the existing sensor
That’s fine as long as you don’t want more than about 10 days of history. Recorder’s default storage duration for the state history of all entities is 10 days (older data is discarded).
What I proposed stores data in a separate table within the database that is not purged and can accumulate indefinitely.
Long term statistics are stored in a table within the database that isn’t periodically purged. The data in this table is collected from entities that meet the criteria explained in the following documentation: Long term Statistics
Entities produced by the Utility Meter integration meet the criteria.