Calculate days from specific date

Hi All,

I think it must be something very simple, but how do I calculate the days from a specific date.

For my example the days from 2018-02-02 till now. (Should be something like 335)

Background: I want to calculate the advance payments of energy and compare them to real consumed energy price to see de difference.

you can use this:

input_datetime:
  detectors_battery:
    name: Detectors Battery
    has_date: true
    has_time: false
    initial: '2019-12-31' 

and this:

sensor:
- platform: template
  sensors:
    detectors_battery_replacement_days2go:
      value_template: '{{ (((states.input_datetime.detectors_battery.attributes.timestamp)-as_timestamp(now())) | int /60/1440) | round(0) }}'
      unit_of_measurement: 'Days'
      entity_id: input_datetime.detectors_battery,sensor.time
8 Likes

I get negative -35 days as a result, how to make it a positive value ?

  - platform: template
    sensors:
      detectors_battery_replacement_days2go:
        value_template: '{{ (((states.input_datetime.detectors_battery.attributes.timestamp)-as_timestamp(now())) | int /60/1440) | round(0) }}'
        entity_id: input_datetime.detectors_battery,sensor.time

  terras_battery:
    name: Terras
    has_date: true
    has_time: false
    initial: '2018-11-29'

Look at the formula.

It uses two timestamps.

It subtracts the current timestamp (now) from the supplied timestamp (input_datetime).

If the supplied timestamp is in the past, the result of the subtraction will be negative.

If the supplied timestamp is in the past and you want the result to be positive, reverse the order of the subtraction.

1 Like

thank working great now.

image

Iā€™ve got it working with another formula:

{{ (as_timestamp(now()) - as_timestamp(ā€œ2018-1-31 00:00:00ā€) ) | timestamp_custom("%j")| int }}

4 Likes

I have this code and it works and gives the correct days between the two dates given.
BUT it will not auto update??? It only update if I reboot, change date manuallyā€¦ why is that?

You must have not included this:

        entity_id: input_datetime.detectors_battery,sensor.time

This is what updates his sensor periodically.

1 Like

Iā€™m sorry, but I donā€™t understand what you meanā€¦ I have
entity_id: input_datetime.detectors_battery

yes but sensor.time is what updates the sensor every minute. Without that, it will only update when the batteryā€™s update, i.e. it will not be live at all times.

EDIT:

you can also use sun.sun if you donā€™t have sensor.time implemented in your setup.

I have tried " ,sensor.time " also but still no auto update?

Do you have sensor.time implemented? You have to add that component to the system, you just canā€™t type sensor.time and expect it to work.

2 Likes

Looks like I only had - ā€˜date_timeā€™ :scream:
Now I have sensor.time as an entity. Tomorrow I will see if itā€™s working. :grin: Thank you.

Thank you! This is exactly what I was looking for.

how is it possible when the attribute has a time within saved?
must i first replace it?

my value 2022-06-11 15:00:00

i want to calculate how much days from now until the event. i think the problem is the time that is saved in the attribute too.

i tried this

{{ (((states.calendar.famillien_termine.attributes.start_time)-as_timestamp(now())) | int /60/1440) | round(0) }}

Itā€™s better to use state_attr.
And you need to convert the string to a timestamp.

{{ (((as_timestamp(state_attr('calendar.famillien_termine', 'start_time'))-as_timestamp(now())) | int /60/1440) | round(0) }}
1 Like

okay thank you, but you had a little typo in your code, you missing a )

{{ (((as_timestamp(state_attr('calendar.famillien_termine', 'start_time'))-as_timestamp(now()))) | int /60/1440) | round(0) }}

I donā€™t know what I am missing. I have added the time_date sensor, I have the input_datetime, and I have the tempalte sensor. What I donā€™t have is that nice card you have. Mind sharing how you created it?

Isnā€™t it a custom entity card? I think you like the title above the entities?

Hi,

I know, itā€™s a late reply, but you have to adjust the code.
The round at the end rounds the days, and if the time between now and your appointment is around 4,4 days away, you get a result of 4.
I would expect, that if you expect on a thuesday evening a result of 5, when you have an apointment of the sunday morning. Therefore I would change to rounding to "round(0,ā€˜ceilā€™). This always round up.