How to perform mDNS discovery

I’ve been reading through the documentation Discovery - Homey Apps SDK to set up discovery of devices, I can see the device advertise itself with avahi-browse:

oh2th@pi ~: avahi-browse -r _go-e_go-eCharger_V4._tcp
   hostname = [go-echarger_123456.local]
   address = [192.168.4.16]
   port = [80]
   txt = ["serial=123456" "friendly_name=123456" "manufacturer=go-e" "devicetype=go-eCharger_V4" "version=054.7" "protocol=2"]

However Homey does not discover it no matter what I do. What am a missing? The sample plugwise reference is a bit too complex to figure this out.

And yes, I know there is an app for go-e Chargers, however that one doesn’t use the new go-e api.

.homeycompose/discovery/go-eCharger_V4.json:

{
  "type": "mdns-sd",
  "mdns-sd": {
    "name": "go-e_go-eCharger_V4",
    "protocol": "tcp"
  },
  "id": "{{txt.serial}}"
}

drivers/go-e-charger-gemini/driver.compose.json:

{
  "name": {
    "en": "go-e Charger Gemini"
  },
  "class": "other",
  "capabilities": [
    "measure_current",
    "meter_power",
    "alarm_generic",
    "measure_power",
    "measure_temperature",
    "onoff",
    "measure_voltage"
  ],
  "platforms": [
    "local"
  ],
  "connectivity": [
    "lan"
  ],
  "images": {
    "small": "{{driverAssetsPath}}/images/small.png",
    "large": "{{driverAssetsPath}}/images/large.png",
    "xlarge": "{{driverAssetsPath}}/images/xlarge.png"
  },
  "discovery": "go-e_go-eCharger_V4",
  "pair": [
    {
      "id": "list_devices",
      "template": "list_devices",
      "navigation": {
        "next": "add_devices"
      }
    },
    {
      "id": "add_devices",
      "template": "add_devices"
    }
  ]
}

drivers/go-e-charger-gemini/driver.js:

'use strict';

const { Driver } = require('homey');

class MyDriver extends Driver {

  /**
   * onInit is called when the driver is initialized.
   */
  async onInit() {
    this.log('MyDriver has been initialized');
  }

  /**
   * onPairListDevices is called when a user is adding a device
   * and the 'list_devices' view is called.
   * This should return an array with the data of devices that are available for pairing.
   */
  async onPairListDevices() {
    this.log('MyDriver searching mDNS');

    const discoveryStrategy = this.getDiscoveryStrategy();
    const discoveryResults = discoveryStrategy.getDiscoveryResults();
    const devices = Object.values(discoveryResults).map(discoveryResult => {
      return {
        name: discoveryResult.txt.friendly_name,
        data: {
          id: discoveryResult.id,
          hostname: discoveryResult.hostname,
          address: discoveryResult.address,
          port: discoveryResult.port,
          devicetype: discoveryResult.txt.devicetype,
        },
      };
    });

    return devices;
  }

}

module.exports = MyDriver;

Try:

"mdns-sd": {
 "name": "go-e_go-eCharger_V4",
 "protocol": "tcp"
},

Thanks for the hint, but no help with that either. I’m still getting “No new devices have been found.”

Updated the original post to reflect the suggested change for the mdns-sd name to ‘go-e_go-eCharger_V4’

Are your Homey and the charger on the same network?

Also, even though the conditions field is as documented, it seems strange that it’s a nested array. Try this:

 "conditions": [
      {
        "field": "txt.manufacturer",
        "match": {
          "type": "string",
          "value": "go-e"
        }
      }
  ]

Or just try leaving out the conditions part entirely to see if that at least provides discovery results.

Yes, both are in the same network and my avahi-browse linux and my mac with Discovery-tool.

I thru away to condition now, since it in away is extra, as the query already is unique for go-e.
Still without a result.

As a comparison, Shelly devices are nicely found by Shelly Homey app from the network with mDNS.

Try running a tcpdump for mDNS packets:

sudo tcpdump -n host 224.0.0.251 and port 5353 | grep e_go

Also, try restarting your app a few times. I was helping out with the Plugwise app over the weekend and we found that the mDNS discovery has some issues that could cause it to not discover any devices (specifically after an mDNS cache flush from the device). After a restart the app would start finding the devices again.

And you can also try using standalone discovery: Discovery - Homey Apps SDK

I don’t know how long the SDK will wait for results to be discovered before it returns from getDiscoveryResults(), but it may not be long enough. The example code for the standalone discovery is useful for debugging to see if any results come in at all (even after getDiscoveryResults() has already returned).

Are you missing an _ of the beginning of the name?

The excerpt oh2th@pi ~: avahi-browse -r _go-e_go-eCharger_V4._tcp has an _ at the beginning but I’m not sure if that is part of the problem

AFAIK the SDK adds that underscore.

For instance, this is the mDNS discovery setup for the Plugwise app:

"mdns-sd": {
  "name": "plugwise",
  "protocol": "tcp"
}

And this is the avahi-browse command:

$ avahi-browse -r _plugwise._tcp
+    br0 IPv4 Plugwise on smile0109a7                       _plugwise._tcp       local
=    br0 IPv4 Plugwise on smile0109a7                       _plugwise._tcp       local
   hostname = [smile0109a7.local]
   address = [192.168.23.68]
   port = [80]
   txt = ["status=/system" "path=/" "version=4.3.12" "product=smile_thermo"]
1 Like

Thank you @robertklep, for your kind help. Got me to a better path over night.

go-eCharger advertise its’ service also with _http._tcp and switching to that brought a result for all http capable devices, including the go-e device.

Now with

{
  "type": "mdns-sd",
  "mdns-sd": {
    "name": "http",
    "protocol": "tcp"
  },
  "id": "{{txt.serial}}",
  "conditions": [
    [
      {
        "field": "txt.manufacturer",
        "match": {
          "type": "string",
          "value": "go-e"
        }
      }
    ]
  ]
}

I finally get a result with standalone discovery:

[log] 2022-11-29 04:47:01 [MyApp] Got result: DiscoveryResultMDNSSD {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  id: '123456',
  lastSeen: '2022-11-29T04:47:01.300Z',
  address: '192.168.4.16',
  host: 'go-echarger_123456',
  port: 80,
  name: '123456',
  fullname: '123456._http._tcp.local.',
  txt: {
    serial: '123456',
    friendly_name: '123456',
    manufacturer: 'go-e',
    devicetype: 'go-eCharger_V4',
    version: '054.7',
    protocol: '2'
  },
  [Symbol(kCapture)]: false
}

So there is progress, for some reason nothing is discovered the the go-e manufacturer specific service. But now I can continue and add the condition to filter only go-e results and even that looks like working now. Oh, and the condition indeed is an array of arrays. Homey complains, if it isn’t.

1 Like

FWIW, it looks like you’re right, someone else also ran into a very similar issue where discovery is likely to be failing because the service name contains a digit. I’ve created an issue for it on Github: mDNS discovery seems to fail for services that contain a digit in their name · Issue #284 · athombv/homey-apps-sdk-issues · GitHub

1 Like