I think my scenario fall similarly into the one described here
Unfortunately It’s beyond my current knowledge and I’m not succeeding with what I would like to.
In short I have a template sensor where I calculated how long my home battery would last in hr depending on it’s capacity and current house consumption (I’d like to improve it and use the average of the last hour rather than the last instant but will think about it later):
battery_duration:
friendly_name: "Batteria Autonomia"
icon_template: mdi:battery-alert
unit_of_measurement: 'Hrs'
value_template: "{{ ((states('sensor.solaredge_storage_level') | int - 9) | int * 19600 | int / 100 | int / (states('sensor.solaredge_power_consumption') | float * 1000)) | round(1) }}"
Not that sensor.solaredge_storage_level is a percentage and the output of sensor. battery_duration is a single digit number like (in example) 3.5 (hrs)
Now I would like to have a template reporting at what time I will switch to grid consumption as battery is out of capacity:
I spent 2 hrs last night playing with this code and while trying to revert to my original I left some mistakes. The problem I have with this approach is that the entity is shown as unavailable in Lovelace
Your sensor state is a string and needs converting to an int (or float if you need fractional hours) to add to the timestamp.
Always use Developer Tools / Templates for debugging this sort of thing. I pasted my prior attempt in, replaced your sensor with one that exists in my setup, and got this:
TypeError: unsupported type for timedelta hours component: str