Printing a sensor value with date + time in a dashboard

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.

Here is my config file code:

# TEMPLATE SENSORS  
  - platform: template
    sensors:
     
(....)

      next_watering_date:
        friendly_name: "Next Watering"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_button.watering_time') | as_datetime | as_local %}
          {{ (start + timedelta(hours = states('input_number.intervalo_entre_regas') | int)).date() }}

Here is the yaml of my dashboard card:

type: vertical-stack
cards:
  - show_name: true
    show_icon: true
    type: button
    tap_action:
      action: toggle
    entity: input_button.watering_time
    icon: mdi:watering-can-outline
    name: Watering Time
    show_state: true
  - type: entities
    entities:
      - entity: sensor.next_watering_date

And here is the result I’m getting in my dashboard:
10

Any thoughts? Thanks in advance.

change into datetime() ??

Tried that, but if I do, the sensor prints as “unavailable”…

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:

# TEMPLATE SENSORS  
  - platform: template
    sensors:
      next_watering_date:
        friendly_name: "Next Watering"
        icon_template: mdi:calendar
        value_template: >
          {% set start = as_timestamp(states('input_button.watering_time')) %}
          {% set start = (start + timedelta(hours = states('input_number.intervalo_entre_regas') %}
          {{ start | timestamp_custom("%d.%m.%Y %H:%M") }}

You’re right, I changed it to an input_datetime as it goes more coherently and is a more flexible input.

I updated my config file with your suggestion:

      next_watering_date:
        friendly_name: "Next Watering"
        icon_template: mdi:calendar
        value_template: >
          {% set start = as_timestamp(states('input_timedate.last_watering_time')) %}
          {% set start = (start + timedelta(hours = states('input_number.intervalo_entre_regas') %}
          {{ start | timestamp_custom("%d.%m.%Y %H:%M") }}

And now I get an error:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected '}', expected ')') for dictionary value @ data['sensors']['next_watering_date']['value_template']. Got '{% set start = as_timestamp(states(\'input_timedate.last_watering_time\')) %} {% set start = (start + timedelta(hours = states(\'input_number.intervalo_entre_regas\') %} {{ start | timestamp_custom("%d.%m.%Y %H:%M") }}\n'. (See ?, line ?).

Ups, I must have accidentally deleted some “)”… :open_mouth:

This should be better, let’s see if the result is what you wanted:

next_watering_date:
        friendly_name: "Next Watering"
        icon_template: mdi:calendar
        value_template: >
          {% set start = as_timestamp(states('input_datetime.last_watering_time')) %}
          {% set start = (start + timedelta(hours = states('input_number.intervalo_entre_regas') ) ) %}
          {{ start | timestamp_custom("%d.%m.%Y %H:%M") }}

There were two “)” missing in this line:

{% set start = (start + timedelta(hours = states('input_number.intervalo_entre_regas') ) ) %}

EDIT: I also changed input_timedate to input_datetime. :slight_smile:

Indeed, lots of things corrected! The error seems to be solved, but I still my next_watering as “unavailable”:

        value_template: >
          {% set start = as_timestamp(states('input_datetime.last_watering_time')) %}
          {% set start = (start + timedelta(hours = states('input_number.intervalo_entre_regas') ) ) %}
          {{ start | timestamp_custom("%d.%m.%Y %H:%M") }}

I also tried this but getting the same result:

        value_template: >
          {% set start = as_timestamp(states('input_datetime.last_watering_time')) %}
          {% set start = (start + timedelta(hours = states('input_number.intervalo_entre_regas'))) %}
          {{ start | timestamp_custom("%d.%m.%Y %H:%M") }}

Please post a screenshot (or copy&paste) of the values of both input_xx from Developer Tools > States.

Or you check it yourself in Developer Tools > Template. Add this, and see, how the “variable” start looks down the road:

{% set start = as_timestamp(states('input_datetime.last_watering_time')) %}
1: {{ start }}
{% set start = (start + timedelta(hours = states('input_number.intervalo_entre_regas') ) ) %}
2: {{ start }}
{{ start | timestamp_custom("%d.%m.%Y %H:%M") }}

Here are the states I get:


I checked both templates you suggested, and here are the results:



The second one seems to be working, but I dont get why it is not working in the dasboard…

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. :slight_smile:

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. :smiley:

Just to note, there are shorter versions of this, but I think it is more readable this way. :slight_smile:

Oh, of course, I forgot about input_number.intervalo_entre_regas…, here it is:

I tried the code you suggested, I feel we are closer, but still I’m getting my sensor as “unavailable”Screenshot 2022-08-12 at 09-48-21 Home – Home Assistant

Perhaps something with this string?

{% set hours_to_add = states('input_number.intervalo_entre_regas') | int %}

When in developer tools->template, it gives the following error:

TypeError: unsupported operand type(s) for +: 'float' and 'datetime.timedelta'

I’m sorry for the late answer, I must have missed the notification from your answer or it didn’t show up… :expressionless:

Nonetheless we’re getting closer. :smiley: 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”. :slight_smile: So something like a follow up error.

Please try this and report back, if I finally got it right :rofl: :smiley:

{% set start = states('input_datetime.last_watering_time') | as_datetime + timedelta(hours = states('input_number.intervalo_entre_regas') | int ) %}
{{ start | as_timestamp | timestamp_custom("%d.%m.%Y %H:%M") }}

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. :slight_smile:

I’m really not sure, if this is the easiest way, but at least in my tests it works. :slight_smile:

1 Like

You could perform the calculation entirely with timestamps.

{{ (states('input_datetime.last_watering_time') | as_timestamp
 + states('input_number.intervalo_entre_regas') | int(0) * 3600)
| timestamp_custom('%d.%m.%Y %H:%M') }}
2 Likes

Why don’t I think of such things?! :expressionless: :smiley: I totally forgot about just using the timestamp to do the math - not that I wouldn’t have that in my config several times… :rofl:

@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! :slight_smile:

Let us know, how it works for you! :slight_smile:

1 Like

This worked!

1 Like

This works too!

Thank you @paddy0174 and @123 for your generosity. It is working now! Reveive my best homie regards :slight_smile:

1 Like