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
elRadix
(elRadix)
January 2, 2019, 12:52pm
3
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'
123
(Taras)
January 2, 2019, 1:09pm
4
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
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
Frax
(Frax)
January 15, 2019, 9:05pm
7
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?
petro
(Petro)
January 18, 2019, 6:02pm
8
You must have not included this:
entity_id: input_datetime.detectors_battery,sensor.time
This is what updates his sensor periodically.
1 Like
Frax
(Frax)
January 18, 2019, 7:15pm
9
Iām sorry, but I donāt understand what you meanā¦ I have
entity_id: input_datetime.detectors_battery
petro
(Petro)
January 18, 2019, 7:17pm
10
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.
Frax
(Frax)
January 18, 2019, 7:19pm
11
I have tried " ,sensor.time " also but still no auto update?
petro
(Petro)
January 18, 2019, 7:23pm
12
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
Frax
(Frax)
January 18, 2019, 7:30pm
13
Looks like I only had - ādate_timeā
Now I have sensor.time as an entity. Tomorrow I will see if itās working. Thank you.
Thank you! This is exactly what I was looking for.
htpc2308
(lulu)
June 9, 2022, 11:05am
15
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) }}
Hellis81
(Hellis81)
June 9, 2022, 11:36am
16
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
htpc2308
(lulu)
June 9, 2022, 11:57am
17
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) }}
shirasp
(Shira)
June 24, 2022, 3:36pm
18
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?
Senbei
(Tristan's Smartes Heim)
January 3, 2023, 8:39pm
20
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.