Skip to content

Commit f3a663f

Browse files
committed
docs for mqtt_trigger
1 parent d1e63ea commit f3a663f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

docs/reference.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,33 @@ see that the ``EVENT_CALL_SERVICE`` event has parameters ``domain`` set to ``lig
569569
This `wiki page <https://github.com/custom-components/pyscript/wiki/Event-based-triggers>`__ gives
570570
more examples of built-in and user events and how to create triggers for them.
571571

572+
@mqtt_trigger
573+
^^^^^^^^^^^^^
574+
575+
.. code:: python
576+
577+
@mqtt_trigger(topic, str_expr=None)
578+
579+
``@mqtt_trigger`` subscribes to the given MQTT ``topic`` and triggers whenever a message is received
580+
on that topic. An optional ``str_expr`` can be used to match the MQTT message data, and the trigger
581+
will only occur if that expression evaluates to ``True`` or non-zero. This expression has available
582+
these four variables:
583+
584+
- ``trigger_type`` is set to “mqtt”
585+
- ``topic`` is set to the topic the message was received on
586+
- ``payload`` is set to the string payload of the message
587+
- ``payload_json`` if the payload was valid JSON, this will be set to the native python object
588+
representing that payload.
589+
590+
When the ``@mqtt_trigger`` occurs, those same variables are passed as keyword arguments to the
591+
function in case it needs them.
592+
593+
Wildcards in topics are supported. The ``topic`` variables will be set to the full expanded topic
594+
the message arrived on.
595+
596+
NOTE: The `MQTT Integration in Home Assistant <https://www.home-assistant.io/integrations/mqtt/>`__
597+
must be set up to use ``@mqtt_trigger``.
598+
572599
@task_unique
573600
^^^^^^^^^^^^
574601

@@ -859,6 +886,9 @@ It takes the following keyword arguments (all are optional):
859886
- ``event_trigger=None`` can be set to a string or list of two strings, just like
860887
``@event_trigger``. The first string is the name of the event, and the second string
861888
(when the setting is a two-element list) is an expression based on the event parameters.
889+
- ``mqtt_trigger=None`` can be set to a string or list of two strings, just like
890+
``@mqtt_trigger``. The first string is the MQTT topic, and the second string
891+
(when the setting is a two-element list) is an expression based on the message variables.
862892
- ``timeout=None`` an overall timeout in seconds, which can be floating point.
863893
- ``state_check_now=True`` if set, ``task.wait_until()`` checks any ``state_trigger``
864894
immediately to see if it is already ``True``, and will return immediately if so. If

0 commit comments

Comments
 (0)