I am using this template in a markdown card to see when my power meter battery was last changed and how much time is still remaining before i need to change it again:
<center>Battery last changed <b>{{((as_timestamp(now()) -
as_timestamp(states("input_datetime.power_meter_battery_changed"))) /
3600) | int }}h</b> ago on
{{as_timestamp(states("input_datetime.power_meter_battery_changed")) |
timestamp_custom("<b>%A, %b %-d </b>at<b> %H:%m</b>")}}.<br/>Estimated<b>
{{states("sensor.power_meter_battery_life_remaining")}}h</b>
remaining</center>
all works great, I get this result:
Battery last changed **16h** ago on **Thursday, Sep 16** at **17:09** .
Estimated **244h** remaining
Now what I’m trying to do is to also include the time when the battery is about to run out, i.e. add the integer stored in sensor.power_meter_battery_life_remaining to now()… and no matter what I do I can’t get the types to line up, i.e. I get a str from as_timestamp() and I can’t figure out how to add the int (correctly multiplied…) in the sensor to it so that I can then translate the resulting timestamp to a date… any ideas? I also tried using timedelta, once again no luck with type match…
Neat, unfortunately my use case doesn’t allow that, I am powering an ESP32 with two different power banks that deliver a constant voltage regardless of their battery level so the only thing I can use is knowing how long they last… fortunately they have a distinct enough voltage for me to check which one is connected and then use an input_number to determine the total lifetime of each one of them plus a helper that get’s reset to now() everytime the ESP restarts (after I confirm it via a actionable notification)
power_meter_battery_life_remaining:
friendly_name: "Power Meter Battery Life Remaining"
unit_of_measurement: 'h'
icon_template: "mdi:battery"
value_template: "{% if states('sensor.power_meter_input_voltage') | float >= 4.97 %} {{states('input_number.battery_life_power_bank_11200') | int - ((as_timestamp(now()) - as_timestamp(state
s('input_datetime.power_meter_battery_changed'))) / 3600) | int }}{% else %} {{states('input_number.battery_life_power_bank_18650') | int- ((as_timestamp(now()) - as_timestamp(states('input_datetim
e.power_meter_battery_changed'))) / 3600) | int }}{%endif%}"