Add a value (string) to current time

Hi

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:

       switch_to_grid:
          friendly_name: "Batteria Esaurita alle"  
          value_template: "{{ as_timestamp(now()) + timedelta( hours = states('sensor.battery_duration')) | timestamp_custom('%H:%M') }}""
          icon_template: mdi:clock-alert-outline
          device_class: timestamp

I have already defined in configuration.yaml

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'

Thanks for your help and inputs

Not totally clear on your question, but the above line has an extra double quote at the end and brackets in the wrong place. Try:

          value_template: "{{ as_timestamp(now() + timedelta(hours = states('sensor.battery_duration'))) | timestamp_custom('%H:%M') }}"

Thank you!

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

Ah, OK — try this:

          value_template: "{{ as_timestamp(now() + timedelta(hours = states('sensor.battery_duration')|int)) | timestamp_custom('%H:%M') }}"

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

which tipped me off as to where the problem was.

1 Like

Hey Troon thanks first of all!

the entity in Lovelace report ‘invalid timestamp

Thanks a lot for teaching me how to debug! I test it in the Templates section and it give me

Result type: string

value_template: “16:23”

I removed device_class: timestamp and it works!!!

Thanks!!!

If you include device_class: timestamp then the timestamp’s value must include date, time, and timezone offset, like this:

2021-04-15T22:13:01.966932-04:00

Your template only produces this:

22:13

which is why the Lovelace UI reported “invalid timestamp”.

Effectively, you didn’t want a true timestamp sensor (date, time, timezone) just a sensor that displays time.

1 Like