From 1522646b7eadb8b4804cab1bf7ccdec0468bd987 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Wed, 7 Feb 2024 11:36:52 +0100 Subject: [PATCH] Add mqtt sensor example processing epoch timestamps (#31287) * Add mqtt sensor example processing epoch timestamps * Explain how to test * Fix * tiny tweaks --------- Co-authored-by: c0ffeeca7 <38767475+c0ffeeca7@users.noreply.github.com> --- source/_integrations/sensor.mqtt.markdown | 66 ++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/source/_integrations/sensor.mqtt.markdown b/source/_integrations/sensor.mqtt.markdown index a7060a113c8..bef44db83af 100644 --- a/source/_integrations/sensor.mqtt.markdown +++ b/source/_integrations/sensor.mqtt.markdown @@ -200,7 +200,71 @@ value_template: ## Examples -In this section you find some real-life examples of how to use this sensor. +In this section, you find some real-life examples showing how to use this sensor. + +### Processing Unix EPOCH timestamps + +The example below shows how an MQTT sensor can process a Unix EPOCH payload. + +{% raw %} + +Set up via YAML: + +```yaml +# Example configuration.yaml entry +mqtt: + sensor: + - name: "turned on" + state_topic: "pump/timestamp_on" + device_class: "timestamp" + value_template: "{{ as_datetime(value) }}" + unique_id: "hp_1231232_ts_on" + device: + name: "Heat pump" + identifiers: + - "hp_1231232" +``` + +{% endraw %} + +Or set up via MQTT discovery: + +Discovery topic: `homeassistant/sensor/hp_1231232/config` + +{% raw %} + +```json +{ + "name": "turned on", + "state_topic": "pump/timestamp_on", + "device_class": "timestamp", + "value_template": "{{ as_datetime(value) }}", + "unique_id": "hp_1231232_ts_on", + "device": { + "name": "Heat pump", + "identifiers": [ + "hp_1231232" + ] + } +} +``` + +{% endraw %} + +To test, you can use the command line tool `mosquitto_pub` shipped with `mosquitto` or the `mosquitto-clients` package to send MQTT messages. + +Payload topic: `pump/timestamp_on` +Payload: `1707294116` + +To set the state of the sensor manually: + +```bash +mosquitto_pub -h 127.0.0.1 -u username -p some_password -t pump/timestamp_on -m '1707294116' +``` + +Make sure the IP address of your MQTT broker is used and that user credentials have been set up correctly. + +The `value_template` will render the Unix EPOCH timestamp to correct format: `2024-02-07 08:21:56+00:00`. ### JSON attributes topic configuration