Hi all,
I am using 2 input booleans which i turn on when a shower or bath is taken. I also increase a counter every time a shower is taken.
I can see them on the history graph, i would like to get the total runtime these booleans were on and i was also hoping to get the average by dividing with the counter.
Any way of doing this other then starting timers (i think that would get complicated quickly) ?
Ah yes of course. I sort of assume that by being able to see a history graph of the input boolean already i wouldn’t need to setup another history_stats sensor. But maybe that’s not a history graph ?
An entity’s History graph simply shows you the entity’s recorded values (i.e. the values stored in the database). A History Stats sensor can perform calculations with the entity’s recorded values and report the result.
# First Bathroom Shower Use On Time Average
- sensor:
- name: "First Bathroom Shower In Use On Time Average"
state_class: measurement
unique_id: First Bathroom Shower In Use On Time Average
state: >-
{{ sensor.first_bathroom_shower_in_use_on_time / sensor.first_bathroom_shower_in_use_on_time_count }}
Question: Can i not do simple division/multiplication/adding/subtracting in a lovelace card and do it on the spot ? Creating a sensor seems like a lot of work for simple operations like this. Probably don’t understand fully.
It’s not the correct way of referencing an entity’s state value. I suggest you review the Templating section of the documentation, specifically this part: States.
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.
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).