Set Mqtt topic based on the entity

Hi,

I’ve not been able to find a solution for this.

I have 6 fans which are handled by one call-service.
On a double tap, I would like to send a Mqtt message based on the entity. The topic path would include the entity.
For example for the den fan, the topic sent would be:
homeassistant/fan/den/set

I could copy this for each entity, 6 fans total, but I’m trying to reduce yaml code


                - entities:
                    - fan.master_bed_room_fan
                    - fan.south_bed_room_fan
                    - fan.north_bed_room_fan
                    - fan.living_fan
                    - fan.sun_fan
                    - fan.den_fan
                  double_tap_action:
                    action: call-service
                    service: mqtt.publish
                    target: {}
                    data:
                      topic: homeassistant/fan/<set based on entity>/set
                      payload: '{"disable": "yes"}'    
                  tap_action:
                    action: call-service
                  ......
                  ......

Sadly no response to my issue :frowning_face:

I decided to used a script to do this.

Replaced the double_tap action with the following:

                  double_tap_action:
                    action: call-service
                    service: script.disable_motion_for_light
                    data:
                      entity_id: entity

Added the following (just showing two fans) to the scripts.yaml:

disable_motion_for_light:
  fields:
    entity_id:
      description: "The title of the notification"
      example: "State change"
  sequence:
    - if: "{{ entity_id == 'light.den_light' }}"
      then:
        - service: mqtt.publish
          data:
            qos: "1"
            retain: "false"
            topic: "homeassistant/motion/den/set"
            payload: '{"disable": "yes"}'

    - if: "{{ entity_id == 'light.living_light' }}"
      then:
        - service: mqtt.publish
          data:
            qos: "1"
            retain: "false"
            topic: "homeassistant/motion/living/set"
            payload: '{"disable": "yes"}'