Can I create virtual devices

I have some ESP32 based devices (nodemcu) configured as MQTT devices with various functions. These functions can be configured with MQTT messages and states (eg light, temperature, humidity) can be reported.

In HA I can, in mqtt.yaml (or configuration.yaml) define sensors or switches with which I can control the devices. However I need to define a MQTT device for each function of these devices and for all devices separately.

I’d like to define a template-device which produces the various entities of the device. I’ve been reading and experimenting with blueprints, templates and helpers but I get no further and would like to ask if someone could help me further.

The controls I’d like to define are, for instance:

  • ssid and wifi-password (text)
  • reporting time (seconds)
  • sensor light/temp/humidity
  • the GPIO pins which are used for the periferals
  • etc

I’d be very grateful if someone could help me on the right track. Is it a blueprint, should it be made as a template or should I go another route. If this is going to work I’m also planning to create these sensors/controls for my tasmota devices which work as expected but configuration (using the tasmota web-console) is of course very inconvenient.

The working control I made in mqtt.yaml is:

light:
  - unique_id: ESP_01_light_brightness
    name: ESP 01 light brightness
    state_topic: stat/ESP32_01/POWER
    command_topic: cmnd/ESP32_01/POWER
    brightness_state_topic: stat/ESP32_01/Dimmer
    brightness_command_topic: cmnd/ESP32_01/Dimmer
    qos: 0 
    retain: false
    brightness_value_template: "{{ value_json.Dimmer  }}"
    state_value_template: "{{ value_json.POWER }}"
    payload_on: "ON"
    payload_off: "OFF"
    optimistic: false
switch:
  - unique_id: ESP_01 light switch
    name: ESP 01 light switch
    state_topic: stat/ESP32_01/POWER
    command_topic: cmnd/ESP32_01/POWER
    qos: 0
    retain: false
    state_on: "on"
    state_off: "off"
    payload_on: "on"
    payload_off: "off"

The experiment in blueprints I gone through, and which does not work, is:

blueprint:
    name: BLP - control ESP32 settings
    domain: automation
    description: Blueprint to make ESP32 controls available as entities in Home Assistant
    input:
        esp32_id:
            name: esp32
            description: Select the tasmota device to control
            selector:
                text:
                    type: text
        bool_light_switch:
            name: light_switch
            description: Light switch
            selector:
                boolean:
            default: true
        num_light_brightness:
            name: brightness
            selector:
                number:
                    min: 0
                    max: 100
            default: 50

mode: single

trigger: 
    - platform: state
      entity_id: !input bool_light_switch
      id: tr_bool_light_switch_triggered
    - platform: state
      entity_id: !input num_light_brightness
      id: tr_num_light_brightness_triggered
       
condition: []

variables:
    tr_bool_light_switch_triggered_var: 'cmnd/{{ esp32_id }}/POWER'
    tr_num_light_brightness_triggered_var: 'cmnd/{{ esp32_id }}/Dimmer'

action:
    - choose:
        - conditions:
            - condition: trigger
              id: tr_bool_light_switch_triggered
          sequence:
            - service: mqtt.publish
              data:
                 qos: 0
                 retain: false
                 topic:   tr_bool_light_switch_triggered_var 
                 payload: "toggle"
        - conditions:
            - condition: trigger
              id: tr_num_light_brightness_triggered
          sequence:
            - service: mqtt.publish
              data:
                 schema: template
                 qos: 0
                 retain: false
                 topic: tr_num_light_brightness_triggered_var
                 payload:  num_light_brightness 

Not really sure what you are trying to do, but a few things to remember.
An automation blueprint is an automation that is preset to do stuff, but still the same rules as an automation. A script Blueprint is a script that is preset to do stuff, but still a script.

You can use mqtt discovery from any of the platforms mentioned (Automations, blueprints, esp’s) to create devices, but devices everywhere are created 1 at a time, no matter if you are doing them manually or using backend integration to create them. They can be grouped together to appear that they were created at once using the device coding, but all are created one at a time.

Here is an example where I create entities in a blueprint using MQTT Discovery.
https://github.com/SirGoodenough/HA_Blueprints/blob/241faf3e5f7438eeeb9af0e9c1cc0eda53180f08/Automations/Octoprint_Additional_Buttons_Helper.yaml. I will tell you that when this is run, all the entities created show up as part of the same device because the device information matches.