I have a D1 mini setup with just a battery voltage reading off A0 with a resistor inline and a NO magnetic reed switch which will be a simple mailbox alert. I have MQTT sending two messages, one with the battery level and the other just as an alert that the door was open. I am trying to get the timestamp off the log in HA from the message to use as my “Last Opened” date/time. I have that working correctly up to a point, it is giving me the previous time the door was opened although if I click on the sensor in the Overview dashboard is shows “x seconds ago”. I also see the issue in the log file when compared to my PC clock. I do not understand why its showing a one message delayed timestamp. Any suggestions?
YAML:
mqtt:
sensor:
- state_topic: "Battery"
name: "Mailbox Battery"
unique_id: mailbox_battery
device_class: battery
unit_of_measurement: "%"
- state_topic: "Mail"
name: "Mailbox Last Opened"
unique_id: mailxbox_last_opened
force_update: true
value_template: "{{ as_timestamp(states.sensor.mailbox_last_opened.last_reported) | timestamp_custom ('%a %m/%d %-I:%M %p' )}}"