Printing datetime in lambda

I am trying to do something seemingly simple, and have exhausted my abilities searching through the forums which end up throwing me in different directions, which use different methods, and so far, 15 different attempts and I am no closer to achieving my goal.

Goal: Use the timestamp from a door/window sensor to print to an e-paper display using lambdas. I only want to access the date/time for the most recent event/state when the door/window sensor was either opened, or closed - it doesn’t matter to me because the sensor is used to detect when a dog food container was last opened/closed. Both opening and closing will happen within a relatively quick time.

I have been directed to create a sensor using templates, but to me, it seems like you should just be able to pull the timestamp from the latest sensor log as the timestamp is there - why the need to create a sensor when said timestamp is already stored. I am a pretty big newbie and have programming challenges, so I would love it if someone could dumb this down a bit. Thanks for any help!

EDIT: I believe I am closer, but still feels like I am unable to do this. I now have a DateTime helper which is working to store the timestamp of the entity. However, I do not know how to use this in the display portion with lambda and keep getting an error because I do not know how to declare and/or store the variable.

This is what I have:

sensor:
  - platform: homeassistant
    entity_id: input_datetime.charlielastfed
    id: charlie

display:
  - platform: waveshare_epaper
    id: my_display
    cs_pin: 5
    dc_pin: 22
    busy_pin: 4
    reset_pin: 21
    model: 2.90inv2 # 296x128
    rotation: 270
    reset_duration: 200ms
    update_interval: 30s
    full_update_every: 1
    lambda: |-
      it.printf(0, 0, id(font2), TextAlign::BASELINE_LEFT , "%Y-%m-%d %H:%M:%S", charlie);

There are people that have written blueprints to to that. Perhaps look those up and use or snag the code.
Also I believe there is a native Z2M way to do that in the Z2M add-on/container.

a simple entity have a " last_change " status, which you can refers to

type: entities
entities:
  - entity: binary_sensor.motion_hallway
    secondary_info: last-changed

Look slightly different in a template thou

Something like this

{{ (states.binary_sensor.motion_hallway.last_changed) }}

Trying to print the last_changed time from a sensor to e-paper. How do I take the output from a datetime sensor:

sensor:
  - platform: homeassistant
    entity_id: input_datetime.charlielastfed
    id: charlie

and use the input_datetime value for a print statement here:

display:
  - platform: waveshare_epaper
    id: my_display
    cs_pin: 5
    dc_pin: 22
    busy_pin: 4
    reset_pin: 21
    model: 2.90inv2 # 296x128
    rotation: 270
    reset_duration: 200ms
    update_interval: 30s
    full_update_every: 1
    lambda: |-
      it.print(0, 0, id(font1), "Charlie was last fed at this time");
      it.print(0,24, id(font2), "Input Datetime from sensor goes here");

I am unable to format the input_datetime for printing to the screen.

I don’t know if this will work, but you can try using strftime():

it.strftime(0, 0, id(font1), "%Y-%m-%d %H:%M", id(charlie).state);

or you can import the input_datetime as a text_sensor and use this:

it.print(0, 0, id(font1), id(charlie).state.c_str());

Thank you, I am very close, and have gotten this to work (it prints (2024-06-23 13:52:00) using the code below. However, I would love to format it as (Sunday, 1:52 PM).

Using strftime, with a regular sensor (not text_sensor) complains that it cannot convert float to char. Using strftime with my text_sensor and using %d, etc. I have been completely unsuccessful.

Is this a limitation that I am going to have to live with?

text_sensor:
  - platform: homeassistant
    entity_id: input_datetime.charlielastfed
    id: charlie

display:
  - platform: waveshare_epaper
    id: my_display
    cs_pin: 5
    dc_pin: 22
    busy_pin: 4
    reset_pin: 21
    model: 2.90inv2 # 296x128
    rotation: 270
    reset_duration: 2ms
    update_interval: 30s
    full_update_every: 1
    lambda: |-
      it.print(0, 6, id(font1), "Charlie was last fed on");
      it.printf(0, 42, id(font2), "%s", id(charlie).state.c_str());

Create a template sensor in HA that turns the ISO date string in the input datetime into the format that you want (see the Time section of the Templating doc) and send that to your ESP device.

Done it for you:

{{ states('input_datetime.charlielastfed')|as_timestamp|timestamp_custom('%A, %I:%M %p') }}
1 Like