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?
rules.JSRule({
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
logger.info(`Received catdoor (battery) chamge to: ${event.newState}`);
const newState = event.newState;
let newPosition = null;
switch(newState) {
case "open" :
newPosition = -100;
break;
case 'in only' :
newPosition = 100;
break;
case 'closed' :
newPosition = 55;
break;
}
if(newPosition !== null) {
logger.info(`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);
}
}
});