How to add integer to a timestamp?

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…

Thanks!

{{ ((as_timestamp(states("sensor.power_meter_battery_life_remaining")) | int) + (now().timestamp() | int)) | timestamp_custom("%Y-%m-%d %H:%M) }}

I think that should do it.

unfortunately not…

{{as_timestamp(states("sensor.power_meter_battery_life_remaining")) }}

returns a “None”

Like your thinking

I wrote this

set the replacement date basic on percentage use and date it was replace.

Assuming sensor.power_meter_battery_life_remaining is in seconds, should plainly ve

{{  (states("sensor.power_meter_battery_life_remaining") | int) + (now().timestamp() | int)) | timestamp_custom("%Y-%m-%d %H:%M) }}

Ahh… of course.
That is hours, not a timestamp…

{{ ((states("sensor.power_meter_battery_life_remaining") | int *3600) + (now().timestamp() | int)) | timestamp_custom("%Y-%m-%d %H:%M) }}

The state is string of hours, so int it then multiply with 3600 to get seconds and add to now, then convert to date string.

That’s it! It didn’t occur to me to convert both to int and then just add them up… thanks.

Final config:

      <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 until {{((states("sensor.power_meter_battery_life_remaining") | int * 3600) + (now().timestamp() | int)) | timestamp_custom("<b>%A, %b %-d</b> at <b>%H:%m</b>")}}</center>

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%}"