Using an input helper to update a timestamp sensor

Hey gang,

Coding noob question that’s hopefully easy for someone not as inept with coding as me lol…

Long story short - I’m using a blueprint in an automation, and for whatever reason the blueprint author defined it to trigger by timestamp sensor. The automation is for a family member who doesn’t have admin so I want to use an input helper to allow the sensor value to be defined from the Lovelace interface.

So here’s my questions:

  1. How do I create a timestamp sensor? I googled and found the help page for sensors but it doesn’t include any YAML examples. Not sure where to start here.

  2. How do set the sensor value to match the value of a datetime input helper every time the input helper is updated?

1 Like

Why not just change the blueprint and be done with it?

Where is the blueprint posted?

I looked at the code in the blueprint and quickly got over my head. Here’s the thread for the original blueprint:

I see how to modify it but explaining the necessary changes is long-winded (and then you won’t be able to easily accept any of the author’s blueprint updates) so let’s make a Template Sensor based on an input_datetime like you requested.

Will the family member be setting both date and time or just time? In other words, will the input_datetime store date and time or just time?

I think just time. The idea is they can set it the night before.

Assuming the input_datetime contains date and time:

template:
  - sensor:
      - name: whatever
        device_class: timestamp
        state: "{{ states('input_datetime.something') | as_datetime | as_local }}"

Assuming the input_datetime contains time only:

template:
  - sensor:
      - name: whatever
        device_class: timestamp
        state: "{{ today_at(states('input_datetime.something')) }}"

EDIT

Correction. Added as_local filter to ensure datetime has timezone information.

6 Likes

Awesome thanks! Trying it out now.

Works! Thanks again!

1 Like

Is it still working? I have no idea how to set it up… it always says that the Unique-ID of the entity is missing…

Hi,

I have to do exactly what Loric76 was asking in this thread.
My input_datetime contains both date and time, so I’m using your first solution. The thing is that the sensor value stays “Unknown”. I can’t get a way around this.
My guess is that the issue is somewhat related to timestamp, every time I update my input_datetime (both manually and from a script) I find this in the log section:

  • sensor.ultimo_orario_cibo_gatti rendered timestamp without timezone: 2023-01-04 17:44:36

Do you have any idea on what could I do?
Thank you

Try adding as_local to the template.

template:
  - sensor:
      - name: whatever
        device_class: timestamp
        state: "{{ states('input_datetime.something') | as_datetime | as_local }}"
2 Likes

That did the trick!
I was going insane overt this :smiley:

Thank you very much!
Regards

Andrea

1 Like

I came here looking for the same solution for the same automation. I implemented the “time only” version above but the sensor still uses datetime format so it has the correct time but the date of when the sensor was last updated so the automation does not trigger.

Is there any way using this template to ensure the automation triggers as it should, at a set time every day?

Hi together,
I came here by searching the exact same problem as the original author. I’m using a time only time only sensor and have configured my template sensor for converting it to a time stamp the following way, after trying nearly any possible configuration mentioned in this or any remotely connected thread. However this at least provides me with a string (“2024-01-31T06:30:00+01:00”) in an ISO 8601 compliant format, which is required for timestamp sensors according to the documentation: Templating - Home Assistant

If your template is returning a timestamp that should be displayed in the frontend (e.g., as a sensor entity with device_class: timestamp), you have to ensure that it is the ISO 8601 format (meaning it has the “T” separator between the date and time portion). Otherwise, frontend rendering on macOS and iOS devices will show an error. The following value template would result in such an error:
{{ states.sun.sun.last_changed }} => 2023-07-30 20:03:49.253717+00:00(missing “T” separator)
To fix it, enforce the ISO conversion via isoformat():
{{ states.sun.sun.last_changed.isoformat() }} => 2023-07-30T20:03:49.253717+00:00 (contains “T” separator)

My current sensor:

template:
  - sensor:
      - name: lichtwecker_weckzeit_timestamp
        device_class: timestamp
        state: "{{ today_at(states('input_datetime.lichtwecker_weckzeit')).isoformat()}}"

However, when I set the dropdown of the Device Class to Timestamp it says the value of the sensor is “Unknown”. But, when I set the device class sonly in the template code, it isn’t selectable (and also not working if manually setting in YAML) in the Blueprint.

I’d appreciate any advice bring it to work this way before I’m going to edit the Blueprint.

I just had a similar problem.
It looks like with a recent update, you can get rid of your first 4 lines and the beginning of the the 5th line, so you only need the actual “state” part

1 Like