In an attempt to calculate days alive in a template sensor, I’ve tried to put birthdays in an input_datetime first
alias: New Script
sequence:
- service: input_datetime.set_datetime
data:
date: '2001-01-01'
target:
entity_id:
- input_datetime.birthdate_sam
The above works, but anything in or before that year doesn’t work. Is this a known limitation, or am I really missing out on something here?
petro
(Petro)
May 11, 2022, 4:45pm
2
Are there errors in your log when you attempt to set that?
None, whatsoever. The date just doesn’t change.
Tinkerer
(aka DubhAd on GitHub)
May 11, 2022, 4:48pm
4
I just tried with one and could set it to before 2001 just fine.
I’ve been quite stupid for about an hour, I see.
I was mixing up the changing of the input_datetime.set_datetime (which works), with the template sensor not calculating.
sam_days_alive:
value_template: "{{((as_timestamp(now()) - as_timestamp(states('input_datetime.birthdate_sam'))) / 86400) | int }}"
friendly_name: Sam days Alive
petro
(Petro)
May 11, 2022, 4:59pm
10
ah, well
value_template: {{ (now() - states('input_datetime.birthdate_sam') | as_datetime | as_local).days }}
1 Like