MQTT how to configure sensor, publish

i created a sensor in my sensor.yaml file like this:

platform: mqtt
state_topic: “maindoor/lastopened”
name: maindoorlastopened

i want sensor.maindoorlastopened to show the last time the door sensor was triggered using mqtt.publish. how do i go about doing it? thank you

try this :

- id: '1567689065684'
  alias: maindoor opened
  trigger:
  - entity_id: binary_sensor.maindoor
    platform: state
    to: 'on'
  action:
  - data:
      payload: '{{ now().strftime("%H:%M:%S") }}' 
      topic: 'sensor/maindoorlastopened'
    service: mqtt.publish

1 Like

Thank you. I tried it but the sensor.maindoorlastopened state still shows Unknown.

I also tried topic: maindoor/lastopened



You probably need this :

- id: '1567689065684'
  alias: maindoor opened
  trigger:
  - entity_id: binary_sensor.maindoor
    platform: state
    to: 'on'
  action:
  - data:
      payload_template: '{{ now().strftime("%H:%M:%S") }}' 
      topic: 'sensor/maindoorlastopened'
    service: mqtt.publish

1 Like

That works!


How do I show it on the front end? There must be something wrong with my sensor setup

SOLVED:
had to add value_template: ‘{{ now().strftime("%H:%M") }}’

thank you