I am trying to create a script based on below template:
service: input_number.set_value
data:
value: {{ (as_timestamp(now()) - as_timestamp(states('input_datetime.time_of_last_reported_rainfall')) )| timestamp_custom("%j")| int }}
target:
entity_id: input_number.days_since_last_rainfall
But after I save this scrip and return back to it, i get this result:
- service: input_number.set_value
data:
value:
"[object Object]": null
target:
entity_id: input_number.days_since_last_rainfall
What i am doing wrong ?
123
(Taras)
4
service: input_number.set_value
data:
value: >
{{ (now() - states('input_datetime.time_of_last_reported_rainfall') | as_datetime | as_local).days }}
target:
entity_id: input_number.days_since_last_rainfall
petro
(Petro)
5
If you’re interested, I made a semi-brainless time calculation library that makes this easier than performing the math yourself.
{% from 'easy_time.jinja' import count_the_days %}
{{ count_the_days('input_datetime.time_of_last_reported_rainfall') }}
You can just make a template sensor out of this too without the need for a input number.
template:
- sensor:
- name: Last Rainfall
unique_id: last_rainfall_days
unit_of_measurement: days
state: >
{% from 'easy_time.jinja' import count_the_days %}
{{ count_the_days('input_datetime.time_of_last_reported_rainfall') }}
And if you want a phrase for a notification, you can simply use this macro
{% from 'easy_time.jinja' import speak_the_days%}
{{ speak_the_days('input_datetime.time_of_last_reported_rainfall') }}
It will return today
, tomorrow
, the corresponding day up to 14 days away with next
, beyond 14 days will return in x days
.
If you aren’t an English speaker, you can set your language and it will return translated phrases.
1 Like