How to make an automation for a specific MQTT event (in my case for Sleep As Android)

I’m absolutly new to MQTT and I’m trying to make an automation that triggers only if the MQTT "event": "sleep_tracking_stopped" is triggered. I found that the MQTT event looks like this in the MQTT Broker

Message 15 received on SleepAsAndroid at 9:26 PM:
{
    "event": "sleep_tracking_stopped"
}

I’m guessing the code must look something like this, but I’m not quite there:

description: Test
mode: single
trigger:
  - platform: mqtt
    topic: SleepAsAndroid
condition:
  - condition: template
    value_template: "value_template: '{{ "event" == "sleep_tracking_started"}}'
action:
  - type: toggle
    device_id: xxxxxx
    entity_id: light.licht_arbeitszimmer_standlampe_light
    domain: light

Nobody ???

This has a condition example…Issues with Automation Parsing MQTT json Value - #5 by dfiore

1 Like

I’m not at home right but when I compare them this should be the correct syntax:

condition:
  - condition: template
    value_template: >-
      {{ trigger.payload_json.event == "sleep_tracking_stopped" }}

UPDATE:
Yes it worked!!! Thank you very much. I looked everywhere for a comparable automation…

@Walteppich Hi, can you please explain how to configure Sleep as android! I created a Mosquitto MQTT User in Home assistant but what should I enter in the Sleep as android app? For example, how do I find out the URL?