How to publish MQTT topic and payload in automation action

Hi, i am trying to establish my location detection more effective way by using the ping method and assign to device tracker.

The PING code in binary_sensors.yaml are as follows

# Ping
    - platform: ping
      host: 192.168.0.124
      name: Rama_Ping
      scan_interval: 30
      count: 1
    - platform: ping
      host: 192.168.0.243
      name: James_Ping
      scan_interval: 30
      count: 1
    - platform: ping
      host: 192.168.0.59
      name: Dilan_Ping
      scan_interval: 30
      count: 1

The device_tracker.yaml code as follows

  - platform: mqtt
    devices:
      rama_note8: 'location/rama'
      james_a7: 'location/james'
      dilan_note5: 'location/dilan'
    qos: 1
    payload_home: 'Marat-Office'
    payload_not_home: 'Away'

In automations.yaml

- alias: Update location
  trigger:
  - platform: state
    entity_id: 
    - binary_sensor.rama_ping
    - binary_sensor.dilan_ping
    - binary_sensor.james_ping
  condition: []
  action:
  - service: mqtt.publish
    data_template: >
       {% if is_state('binary_sensor.rama_ping', 'on') %}
          topic: 'location/rama'
          payload_home: 'Marat-Office' 
       {% elif is_state('binary_sensor.rama_ping','off') %}
          topic: 'location/rama' 
          payload_not_home: 'Away' 
       {% if is_state('binary_sensor.dilan_ping', 'on') %}
         topic: 'location/dilan'
         payload_home: 'Marat-Office' 
       {% elif is_state('binary_sensor.dilan_ping','off') %}
         topic: 'location/dilan'
         payload_not_home: 'Away'
       {% if is_state('binary_sensor.james_ping', 'on') %}
         topic: 'location/james'
         payload_home: 'Marat-Office' 
       {% elif is_state('binary_sensor.james_ping','off') %}
         topic: 'location/james'
         payload_not_home: 'Away' 
       {% endif %}
  mode: single

While i monitor MQTT explore, there is no changes take place. Please do advice

Your automation’s service call is invalid. Create separate mqtt.publish service calls, one for each binary_sensor.

In addition, there’s no option for mqtt.publish called payload_not_home. Use payload.

If you want to do it with a single service call, you need to use the Trigger variable like this:

- alias: Update location
  trigger:
  - platform: state
    entity_id: 
    - binary_sensor.rama_ping
    - binary_sensor.dilan_ping
    - binary_sensor.james_ping
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: "location/{{ trigger.to_state.object_id[0:-5] }}"
      payload: "{{ 'Marat-Office' if trigger.to_state.state == 'on' else 'Away' }}"
  mode: queued
1 Like

Hi, thank you for the explanation. Its works accordingly. But i don’t understand the topic coding with object id. Can i get some explanation and are there any reference i can refer too.

  - service: mqtt.publish
    data:
      topic: "location/{{ trigger.to_state.object_id[0:-5] }}"

It’s using python’s slice to get a portion of the string contained by object_id. The portion it is getting is from the zeroth position up to the fifth from last position. In other words, it is extracting the person’s name from the binary_sensor’s object_id.

dilan_ping
^    ^
|    |
0    -5

I got it, thank you