Similar request, I have a reed switch on a door, I would like to know, in and via esphome, when it last changed. I can see it in HA via “last updated” but due to wifi issues it keeps resetting, so merged all of the above together and came up with:
binary_sensor:
- platform: gpio
pin:
number: 23
mode: INPUT_PULLUP
name: ${friendly_name} Door
id: ${device_name}_door_sensor
device_class: door
on_state:
- component.update: systime
- lambda:
if (id(${device_name}_door_sensor).state) {
id(door_last_update).publish_state("Opened - " + id(systime).state);
} else {
id(door_last_update).publish_state("Closed - " + id(systime).state);
}
text_sensor:
- platform: template
id: systime
internal: true
disabled_by_default: true
update_interval: never
lambda: |-
char str[25];
time_t currTime = id(my_time).now().timestamp;
strftime(str, sizeof(str), "%d/%m/%Y %H:%M", gmtime(&currTime));
return (std::string) str;
- platform: template
update_interval: never
name: ${friendly_name} Door Last Changed
id: door_last_update
time:
- platform: sntp
id: my_time