Time stamp of last motion

Hi,

I have a small display by my computer that I use just to display some basic info.

I have 3 pages on it and I want the 3rd page to display the time that the last motion was detected at my front door, but I am not having any luck.

esphome:

  name: "caddy-battery"

  friendly_name: Caddy Battery Voltage

esp32:

  board: esp32dev

  framework:

    type: arduino

# Enable logging

logger:

# Enable Home Assistant API

api:

  encryption:

    key: "****************************************************"

ota:

wifi:

  ssid: !secret wifi_ssid

  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails

  ap:

    ssid: "Esphome-Web-4D0Ef4"

    password: "wUgxHpogRl2F"

captive_portal:

  # Example configuration entry

time:

  - platform: homeassistant

    id: homeassistant_time

sensor:

  - platform: homeassistant

    name: Caddy Battery

    id: current_voltage_caddy

    entity_id: sensor.shellyuni_4417931b70b4_adc

    unit_of_measurement: 'V'

    accuracy_decimals: 4

    filters:

      sliding_window_moving_average:

        window_size: 15

        send_every: 15

  - platform: homeassistant

    name: Toyota Battery

    id: current_voltage_toyota

    entity_id: sensor.shellyuni_3494547ae2ae_adc

    unit_of_measurement: 'V'

    accuracy_decimals: 4

    filters:

      sliding_window_moving_average:

        window_size: 15

        send_every: 15

  - platform: homeassistant

    name: Door last motion

    device_class: timestamp

    id: door_motion

    entity_id: sensor.front_door_last_motion

  - platform: template

    name: Template Sensor

    id: template_motion

    on_value:

      - sensor.template.publish:

          id: door_motion

          state: !lambda |-

            auto timestamp = id(homeassistant_time).now();

            auto hours = timestamp.hour;

            auto minutes = timestamp.minute;

            auto seconds = timestamp.second;

            auto time_in_seconds = hours * 3600 + minutes * 60 + seconds;

            return time_in_seconds;

   

i2c:

  sda: GPIO21

  scl: GPIO22

  scan: True

  frequency: 400kHz

     

display:

  - platform: ssd1306_i2c

    model: "SSD1306 128X64"

    address: 0x3C

    update_interval: 5s

    id: my_display

    pages:

      - id: page1

        lambda: |-

          it.printf(0, 0, id(bebas), "Caddy Battery");

          it.printf(0, 16, id(bebas_large), "%.2fV", id(current_voltage_caddy).state);

      - id: page2

        lambda: |-

          it.printf(0, 0, id(bebas), "Toyota Battery");

          it.printf(0, 16, id(bebas_large), "%.2fV", id(current_voltage_toyota).state);

      - id: page3

        lambda: |-

          it.printf(0, 0, id(bebas), "Front Door");

          it.printf(0, 16, id(bebas_large), "%X", id(template_motion).state);

interval:

  - interval: 5s

    then:

      - display.page.show_next: my_display

      - component.update: my_display

font:

  - file: "BebasNeue-Regular.ttf"

    id: bebas

    size: 20

  - file: "BebasNeue-Regular.ttf"

    id: bebas_large

    size: 60

An error I get when looking at the log is

[08:59:27][W][homeassistant.sensor:015]: 'sensor.front_door_last_motion': Can't convert '2023-09-16T06:58:35+00:00' to number!
[08:59:27][D][sensor:094]: 'Door last motion': Sending state nan  with 1 decimals of accuracy!

Maybe try importing sensor.front_door_last_motion as a text sensor?

Thanks

I got it going with this

sensor:
  - platform: homeassistant
    name: Caddy Battery
    id: current_voltage_caddy
    entity_id: sensor.shellyuni_4417931b70b4_adc
    unit_of_measurement: 'V'
    accuracy_decimals: 4
    filters:
      sliding_window_moving_average:
        window_size: 15
        send_every: 15
  - platform: homeassistant
    name: Toyota Battery
    id: current_voltage_toyota
    entity_id: sensor.shellyuni_3494547ae2ae_adc
    unit_of_measurement: 'V'
    accuracy_decimals: 4
    filters:
      sliding_window_moving_average:
        window_size: 15
        send_every: 15


text_sensor:
  - platform: homeassistant
    name: door movement text
    id: door_movement
    entity_id: sensor.front_door_last_motion


i2c:
  sda: GPIO21
  scl: GPIO22
  scan: True
  frequency: 400kHz
      
display:
  - platform: ssd1306_i2c
    model: "SSD1306 128X64"
    address: 0x3C
    update_interval: 5s
    id: my_display
    pages:
      - id: page1
        lambda: |-
          it.printf(0, 0, id(bebas), "Caddy Battery");
          it.printf(0, 16, id(bebas_large), "%.2fV", id(current_voltage_caddy).state);
      - id: page2
        lambda: |-
          it.printf(0, 0, id(bebas), "Toyota Battery");
          it.printf(0, 16, id(bebas_large), "%.2fV", id(current_voltage_toyota).state);
      - id: page3
        lambda: |-
          auto door_movement_text = id(door_movement).state.c_str();
          char time_str[6]; // HH:MM + null terminator
          int timestamp_pos = -1;
          for (int i = 0; door_movement_text[i] != '\0'; i++) {
            if (door_movement_text[i] == 'T') {
              timestamp_pos = i;
              break;
            }
          }
          if (timestamp_pos != -1 && strlen(door_movement_text) > (timestamp_pos + 5)) {
            strncpy(time_str, door_movement_text + timestamp_pos + 1, 5);
            time_str[5] = '\0'; // Null-terminate the string
            // Parse hours and minutes
            int hours, minutes;
            sscanf(time_str, "%d:%d", &hours, &minutes);
            // Add 2 hours
            hours += 2;
            if (hours >= 24) {
              hours -= 24;
            }
            // Print the adjusted time
            it.printf(0, 0, id(bebas), "Front Door");
            it.printf(0, 16, id(bebas_large), "%02d:%02d", hours, minutes);
          } else {
            strcpy(time_str, "N/A"); // Display "N/A" if the timestamp format is invalid
            it.printf(0, 0, id(bebas), "Front Door");
            it.printf(0, 16, id(bebas_large), time_str);
          }
1 Like