From 9258dfb91d0276848b7bf393df7c26972985dffc Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Tue, 29 Aug 2023 17:29:48 +0200 Subject: [PATCH] Add mqtt event example with value_template (#28723) * Add mqtt event example with value_template * alternate syntax * tweak * tiny tweaks --------- Co-authored-by: c0ffeeca7 <38767475+c0ffeeca7@users.noreply.github.com> --- source/_integrations/event.mqtt.markdown | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/source/_integrations/event.mqtt.markdown b/source/_integrations/event.mqtt.markdown index ab01e916815..491bb1fa364 100644 --- a/source/_integrations/event.mqtt.markdown +++ b/source/_integrations/event.mqtt.markdown @@ -221,3 +221,33 @@ The example below demonstrates how event attributes can be added to the event da ```bash mosquitto_pub -h 127.0.0.1 -t home/doorbell/state -m '{"event_type": "press", "duration": 0.1}' ``` + +### Example: processing event data using a value template + +In many cases, translation of an existing published payload is needed. +The example config below translates the payload `{"Button1": {"Action": "SINGLE"}}` of +the device `Button1` with event type `single` to the required format. +An extra attribute `button` will be set to `Button1` and be added to the entity, +but only if the `Action` property is set. Empty dictionaries will be ignored. + +{% raw %} + +```yaml +mqtt: + - event: + name: "Desk button" + state_topic: "desk/button/state" + event_types: + - single + - double + device_class: "button" + value_template: | + { {% for key in value_json %} + {% if value_json[key].get("Action") %} + "button": "{{ key }}", + "event_type": "{{ value_json[key].Action | lower }}" + {% endif %} + {% endfor %} } +``` + +{% endraw %} \ No newline at end of file