Arbitrary Entity ID for automation

Hi everyone,

I’m new to Home Assistant and the template syntax so apologies if this question has been asked before, or the answer is easily searchable.

I have a scenario in which I want to externally control many switches provisioned on Home Assistant through MQTT JSON strings.

Is there a way to do select the entity ID through a json string? Or is this an incorrect understanding on how the configuration.yaml file works? If so could anyone point me in the right direction documentation wise?

example

  - alias: 'Test MQTT Switch Toggle'
    trigger:
      - platform: mqtt
        topic: testing/test
        payload : {{ JSON_STRING_FORMAT }} (?)
        encoding: 'utf-8'
    action:
      - service: switch.toggle
        entity_id:   '{{ value_json.deviceID] }}'

Or would I need to iterate through all of my devices and select the correct one?

Thanks for your help.

Interesting idea.
When I publish a payload of switch.kitchen_light to the switch/kitchen topic, this toggles the switch.kitchen_light:

- id: test
  trigger:
  - platform: mqtt
    topic: switch/kitchen
    encoding: 'utf-8'
  action:
  - service: switch.toggle
    data_template:
      entity_id: "{{ trigger.get('payload') }}"

Fun exercise!!

If you publish JSON payloads to testing/test, this automation will extract the entity from the payload and toggle it.

- id: 'entity_toggle'
  alias: Entity Toggle
  trigger:
    - platform: mqtt
      topic: 'testing/test'
  action:
    - service: homeassistant.toggle
      data_template:
        entity_id: "{{trigger.payload_json.entity}}"

Sample payloads:

{"entity": "light.kitchen"}
{"entity": "switch.pump"}
{"entity": "group.bedroom_lights"}

It isn’t necessary to publish the payload in JSON format. If you simply publish light.kitchen you can just use trigger.payload to get the payload. The advantage of using JSON is if you wish to pass additional information (brightness, color, etc) and then modify the automation’s action section to use it accordingly.

Thanks for the replies, I think my core issue is I’m struggling with the syntax. I am also trying to check in the trigger or condition sections if there is a key present in the received json string, as I don’t want this script to run for every mqtt message received.

Is there a succinct way to do this? Essentially the goal is to have these devices controllable/query-able for an external MQTT source.

I seem to of solved my own above issue. Adding this is effective

    condition:
      condition: template
      value_template: "{{ trigger.payload_json.key_value is defined }}"

One last question I had is how would I access an arbitrary entity’s information? Here is an example that hopefully explains

    action:
      - service: mqtt.publish
        data_template:
          topic: 'homeassistant/uplink'
          payload : "{{ {{ trigger.payload_json['plugID'] }}['voltage'] | float }}"

This does not correctly parse, is there a way to do this?

It can’t be completely arbitrary. The template must have some means of determining the entity’s type so it can intelligently select the appropriate attribute.

Do you have examples of the entities the template should handle?

Yes, I am looking to access information from a TP-Link Smart Plug. I am not sure of what specific device I will need until I receive the identifier (to be determined) from a MQTT Json packet.