Calculate cumulative subtractions

I have a pet feeder that weighs the amount of food.
When I fill the bowl, it adds 40grams, and when the cat eats during the day it subtracts 8grams, 4 grams, et cetera.
I have a sensor that shows the current weight of the food. So in my example it starts with zero, than goes to 40, then to 32, then to 28 etc.
I want to calculate the total amount food eaten by the cat in the current day. So just the subtractions of my sensor.
I have no clue on hoe to do this and cant find a hint on the forum.
Does anyone have a clue for the template that can calculate this?

The amount your cat ate will just be the starting weight (40g) minus the current weight.

You can use a template sensor for that.

1 Like

I know, bit thats not what I want to acheive. Because when my spouse fills the tray during the day, the result will be off.
For instance:
Fill 40, eat 20+10+10, fill 20, eat 10 would result in eaten total: 10. In stead of eaten total: 50

So I want to make a template sensor that calculate every subtraction

Well this is the first time you have mentioned refilling during the day and refilling amounts other than 40g.

You should have included these details in the original question. We are not mind readers.

What is the entity_id of the food weight sensor?

Is it noisy?

i.e. does the food weight jump up and down while the cat is feeding?

Include a history graph of the sensor.

Just in case it’s relevant, I’ll add som more info on the setup and my earlier thoughts.

The steup
I use a surepet Microchip reader. That has an integrated scale. When I open the tray and fill the bowl, it’s detected, weighed and sent to the surepet app/cloud.Whe the cat approaches the bowl it opens, when the lid closes the bowl sends the new weighed of the food to the surepet cloud/app.
Using the HA integration, I get a reading of the current weight of food in the bowl with the entity sensor.feeder_de_frituur. (“de_frituur” is the name assigned to the feeder).
Because sometimes the sensor is unavailable, I made a template sensor sensor.voer_noxie_in_gram, using this template:

      voer_noxie_in_gram:
        friendly_name: "Voer in bakje van Noxie"
        unit_of_measurement: "gram"
        icon_template: mdi:paw
        value_template: >-
          {% if states.sensor.feeder_de_frituur != 'unavailable' -%}
          {{ states('sensor.feeder_de_frituur') |float |round(0) }}
          {%- endif %}

The result is shown in this histogram:

the use case
As you can see, the cat eats all of the food when it’s added to the bowl.
This results in situations where I fill the bowl, the cat’s eats it and half an hour later when someone in my family sees an empty bowl, he/she refills the bowl and the cat eats too much.
Off course I can communicatie with the family members with post-its, a whatsapp message or have them install the surfeed app on their phone, but where is the fun in that when I have a home assistant setup with a dashboard in the kitchen that can show the actual amount eaten?

my earlier attempts
I tried making an Automation in HA, that is triggered at the change of sensor.voer_noxie_in_gram, calculates the difference between the new and the old state, and than adds it to the value of a numeric helper. It takes another automation tot reset the helper every day. It works, but I’m not a big fan of such use of automations. I would rather have 1 sensor that does it all.
That leads me to the question: to have 1 (template) sensor that does the trick, I would have to have one that can handle just the subtractions of the value of sensor.voer_noxie_in_gram during the day.

I hope I haven’t let anything relevant out of my description :slight_smile:
I appreciate your time and effort reading this and helping me on this one

I would use a trigger-based template sensor, with two triggers: One would be a state trigger based on sensor.voer_noxie_in_gram and the other would be a time trigger for midnight. You can use trigger ID’s to figure out which trigger has caused the update.

I haven’t tried this or error-checked it, but it would be something like the following:

If the trigger is the state trigger,

{% set decrease = max( 0, trigger.from_state.state | float(0) -  trigger.to_state.state | float(0) ) %}
{{ this.state | float(0) + decrease }}`

If the trigger is time,
0

That should give you a sensor which reports how much the cat has eaten today.

Cool, I didn’t know triggered sensors existed :slight_smile: (exactly why I visit the forum; to learn things).
I will try out your suggestion and report my findings here :slight_smile: