I have added a DIY MQTT device (light dimmer) and it shows up in the dashboard and can be switched and dimmed fine using home assistant.
However, it does not show up in the list of devices in an action when I create an automation or a scene. What could be a reason for that?
It does show up as an “entity” under Configuration → Devices → Entities, but not as a device unter Configuration → Devices → Devices. My MQTT broker is Mosquitto.
Here is my configuration:
mqtt:
light:
- unique_id: 3dprinter_light
name: "3D Printer Light"
qos: 0
optimistic: false
retain: false
brightness_scale: "12345"
state_topic: ha/Workshop/Printer_Light
command_topic: ha/Workshop/Printer_Light/Control
brightness_state_topic: ha/Workshop/Printer_Light/Brightness
brightness_command_topic: ha/Workshop/Printer_Light/Brightness/Control
tom_l
March 6, 2023, 3:13pm
2
Use services, not device actions.
I am not sure I understand. The screenshot shows the scene editor. I see nothing about “services” on that screen.
francisp
(Francis)
March 6, 2023, 5:28pm
4
MQTT entries defined in .YAML can’t create devices, only entities. If you want to create a device, you need to create a MQTT discovery message and publish it to HA.
Thanks for the pointer! Just as you wrote that, I finally got discovery to work, phew.
However, the device still shows up as an entity, not as a device…
Finally it works! Here is the discovery part of my sketch that works:
void publishConfig() {
const size_t bufferSize = JSON_OBJECT_SIZE(30);
DynamicJsonDocument jsonDoc(bufferSize);
jsonDoc["uniq_id"] = DEVICE_ID;
jsonDoc["name"] = DEVICE_NAME;
jsonDoc["dev_cla"] = DEVICE_CLASS;
jsonDoc["stat_t"] = TOPIC_ON_STATE;
jsonDoc["cmd_t"] = TOPIC_ON_SET;
jsonDoc["bri_stat_t"] = TOPIC_BRIGHTNESS_STATE;
jsonDoc["brightness_command_topic"] = TOPIC_BRIGHTNESS_SET;
jsonDoc["brightness_scale"] = (char*)String(RANGE).c_str();
jsonDoc["optimistic"] = false;
//jsonDoc["retain"] = false;
JsonObject dev = jsonDoc.createNestedObject("dev");
JsonArray ids = dev.createNestedArray("ids");
ids.add(DEVICE_ID);
dev["name"] = DEVICE_NAME;
dev["mdl"] = DEVICE_MODEL;
dev["sw"] = DEVICE_FW_VERSION;
dev["mf"] = DEVICE_MANUFACTURER;
serializeJsonPretty(jsonDoc, Serial);
Serial.println("");
char message[512];
serializeJson(jsonDoc, message, sizeof(message));
Serial.println("Publishing config:");
Serial.println(TOPIC_CONF);
Serial.println(message);
client.publish(TOPIC_CONF.c_str(), message, sizeof(message));
}
Also don’t forget to increase the packet size as needed, this had me scratching my head for too long, as it fails silently…
client.setServer(mqtt_server, 1883);
client.setBufferSize(512);