MQTT Publish does not continuously working with automation

Hello,

I have configured two automation with my motion sensor but its not working correctly.

The first should be turn on my light and my android tablet’s screen through the WallPanel application

- alias: Turn on dining room light when there is movement
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d000289f382
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.dining_room_light
  action:
    service: mqtt.publish
    data_template:
      topic: 'wallpanel/mywallpanel/command'
      payload: "{\"wake\": true}"
      retain: true
      qos: 2

The second automation just turn off the light if there is no movement:

- alias: Turn off dining room light 3 minutes after last movement
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d000289f382
    to: 'off'
    for:
      minutes: 3
  action:
    service: light.turn_off
    entity_id: light.dining_room_light

Issue: The first automation publish (mqtt) only once in ~10 minutes or it just not turning on my light. Could you help me what am I doing wrong?

You can’t have 2 actions for an automation. What you need to do is make a list of actions.

A list in YAML is the ‘-’ icon.

- alias: Turn on dining room light when there is movement
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d000289f382
    to: 'on'
  action:
    - service: light.turn_on
      entity_id: light.dining_room_light
    - service: mqtt.publish
      data_template:
        topic: 'wallpanel/mywallpanel/command'
        payload: "{\"wake\": true}"
        retain: true
        qos: 2

So now our action is 2 things.

You actually can’t have the same key for any YAML. For Home Assistant, I’m pretty sure it just ignores duplicate keys, so it should only do the first one. But I’m not 100% what actually happens.

Thank you, it is working now.

Also the ‘problem’ was that the Xiaomi Motion sensor stays at ‘On’ status for 2 minutes… :slight_smile: