Esphome mqtt number item for battery powered esp8266 device

I have a custom esp8266 device that I’m looking to run over battery power. So far so good, I have this appearing in OpenHAB via the MQTT/HomeAssistant binding.

This essentially exposes two MQTT topics:

  • homeassistant/catdoor-battery/number/catdoorposition/state
  • homeassistant/catdoor-battery/number/catdoorposition/command

When the device is running, I can modify this Number value of the device, by having a text item linked to the Thing Channel

Number Catdoor_Battery_Number "Cat door position" {channel="mqtt:homeassistant:4d0a5b9184:catdoor-battery:ESPnumbercatdoorposition#number"}

This will update the MQTT command topic with the new value correctly. But often, this device will be in deep sleep mode and will not receive the command. Does the binding have the ability to add the RETAINED flag to messages? I presume not as I can’t find any documentation along those lines.

FWIW, I have a workaround where I use a JSRule to manually send the value to the topic with the retained flag, but it’s not nice and requires I match up the Topic names, which is prone to error. I was hoping to avoid this. Did I miss something that would allow this to be neater or am I stuck with this workaround?

  name: "Manage changes in catdoor (battery) state from UI or rules",
  description: "",
  triggers: [
    triggers.ItemStateChangeTrigger("Catdoor_Battery_Position", undefined),
  tags: [],
  id: "Catdoor_Battery_MoveCatdoor",
  execute: (event) => {
    // allowed positions
    // open, in only, closed`Received catdoor (battery) chamge to: ${event.newState}`);
    const newState = event.newState;
    let newPosition = null;

    switch(newState) {
      case "open" :
        newPosition = -100;
      case 'in only' :
        newPosition = 100;
      case 'closed' :
        newPosition = 55;

    if(newPosition !== null) {`Sending request to update Cat door (battery) to position: ${newPosition}`);
      actions.get("mqtt", "mqtt:broker:4d0a5b9184").publishMQTT("homeassistant/catdoor-battery/number/catdoorposition/command", newPosition.toString(), true);

That’s a Number Item. Do you mean a Text element on a sitemap?

If you use a Generic MQTT Thing instead of HomeAssistant you have full control over all the settings, including setting the retained flag. Typically commands are not retained because for many cases it leads to undesirable behaviors. So standards and like HomeAssistant and Homie do not usually retain commands, only states.

Yes you’re right… not shown, I use a Selection element on a sitemap.

Ahh. That’s a good suggestion. Given this is an esphome device I defaulted to using that given the device is autodiscovered. Reading the docs that looks fairly straightforward to configure, so I’ll take a look at that instead.

Thanks for your help!