Template sensor to measure battery percentage when decreasing?


i have a dreamebot that clean house… there is an entity that measure battery in percentage.

I want to sum when battery change decreasing but not when it increase. For example:

The bot start at 100%
When it wash entity change to 99%, 98% and when it reach 20% it back to charge…
When battery is at 95% it back to wash and, for example, it finish to clean house with 50%

The battery is used so (100-20) + (95-50) = 125

How can i make an entity from a template that count only when original entity decrease?

I can’t think of how to do this with a template sensor, but you could do it with an number helper and an automation.

The trigger for the automation would be a change in state of the batter sensor.

The condition would be a template to check if the value is decreasing:

{{ trigger.to_state.state | int < trigger.from_state.state | int }}

Then call input_number.set_value with the value set to something like:

{{ states('input_number.your_helper') + 
    (trigger.from_state.state | int - trigger.to_state.state | int) }}