Hello, I have set a sensor in my config file to count a given number of hours from my last watering time, in order to tell me when do I need to water again. I managed to get a date, but im missing a time…I’m sure this is a simple thing to solve, but I’m not managing to find my mistake.
Are you sure you’re getting a correct value from your input_button? Sounds more like it should be an input_datetime, shouldn’t it?
Nonetheless, take a look what state you really get on that input_button. It will most likely be a string (something like 2022-08-20T14:05:45.342805+00:00). If so, you need to change the type from string to timestamp by as_timestamp(states('your_input_button')). After that add the timedelta without .date(), just the timedelta (here as well, what format has that input?). And after all that, change it to the format you want to by using timestamp_custom with the correct formatting.
Taking this into account, I’d say something like this:
What I’m missing is the screenshot of the second input, the input_number.intervalo_entre_regas. But with the error message from your third screenshot I have a guess.
Try it with
next_watering_date:
friendly_name: "Next Watering"
icon_template: mdi:calendar
value_template: >
{% set hours_to_add = states('input_number.intervalo_entre_regas') | int %}
{% set start = as_timestamp(states('input_datetime.last_watering_time')) %}
{% set start = (start + timedelta(hours = hours_to_add )) %}
{{ start | timestamp_custom("%d.%m.%Y %H:%M") }}
The input_number is of type string, where the timedelta needs int… After these changes, it should work in the frontend as well - hopefully.
Just to note, there are shorter versions of this, but I think it is more readable this way.
I’m sorry for the late answer, I must have missed the notification from your answer or it didn’t show up…
Nonetheless we’re getting closer. Btw. your sensor will be “unavailable” as long as the error(s) exist. With the error(s), the sensor will not be setup correctly and then shows “unavailable”. So something like a follow up error.
Please try this and report back, if I finally got it right
I hate these vice versa conversions, but it seems to be the only way here… Just to explain, what I’m doing here. First I convert the input_datetime to a real datetime object and add the timedelta by converting the input_number to an int and add it.
Then I take this and convert it to a timestamp and format this timestamp to a local, readable format.
I’m really not sure, if this is the easiest way, but at least in my tests it works.
Why don’t I think of such things?! I totally forgot about just using the timestamp to do the math - not that I wouldn’t have that in my config several times…
@sergioxavier Try it with the version from @123 , it seems easier and better readable. Not to speak from the ressources it’s using far less than mine!