MQTT: Record timestamp of event

Introduction

If you want to record timestamps of different events you need to create several automations that generate MQTT messages and post them to corresponding topics of your choice.

For instance, you may want to record last timestamp the motion was detected, door or window opened or closed a button pressed, or a water leak or smoke detector triggers.

The blueprint below helps to simplify creation of such automations.

Import blueprint from here

Documentation

Required inputs

The required inputs are Topic and Trigger Entity

Topic

Specify here the MQTT root topic you want to publish generated event messages, for example event/last_triggered. Note that you can use slashes ‘/’ to specify a path. You should not put ‘/’ at the end of the path.
The full path of MQTT topic will be constructed based on the template below:

<Topic>/<Trigger_Entity_id>/<Trigger_Entity_State>

Trigger entity id here will be without a domain.

Trigger Entity

The entity you want to monitor. Once the state of this entity changes a new MQTT message will be published to specified topic.

Optional inputs

Trigger Entity State

If omitted, any state change of Trigger Entity will generate a message. If you want to record only specific states, use Trigger Entity State for this. For instance, to record timestamps of only single button presses, specify button entity at Trigger Entity and single in this input. Similarly, if you want to record motion detected (not ‘no motion’) events, specify your motion sensor at Trigger Entity and on state here.
The default is none which means ‘any state change’.

reatin

Set this to false if you do not want MQTT to retain this message. true is the default value.

Enabling Entity

Use Enabling Entity to make the automation to trigger based on entity’s state (see below)

Enabling entity state

The automation will trigger only if Enabling Entity is present and it is in specified state.

Example automation

The automation below will publish MQTT message for all motion sensor detected motions

  automation:
    - id: "test_mqtt"
      alias: Test MQTT
      description: ''
      use_blueprint:
        path: yuri-vashchenko/mqtt_record_event_timestamp.yaml
        input:
          topic: 'events/last_motion'
          trigger_entity: binary_sensor.motion
          trigger_entity_state: 'on'