HomeAssistant MQTT Discovery losing channels

Maybe in the debug or trace level logs from the Home Asssitant binding.

Going forward please use code fences for logs. It makes them much easier to read.

```
code goes here
```

Unfortunately, you need to correlate the entries in openhab.log with those entries from events.log to understand why the Thing went offline and came back online.

The problem occurred again today. At some point, the channels were “forgotten” again. When I disabled the Thing and then enabled it again at around 14:20 today, the channels were back.

I have included the events.log and openhab.log for that time period here.

However, I can’t see anything unusual or obvious in the logs.

Can you make anything of this?

Thanks!

log.log (35.2 KB)

I don’t read German so I can only really understand the entries from events.log.

I think this was mentioned earlier, but you can avoid the HA thing(s) from going offline by linking your items to the topics in the top section of the MQTT explorer using a generic MQTT thing(s). I haven’t read all this post, but the HA thing includes an availability that triggers the offline. One possible thought is the hm2mqtt app marks a device unavailable if it hasn’t heard from it in a while (could be no activity-fully discharged and no solar, or could have a communication drop, or something else). It seems from the post that the published topic by hm2mqtt does not change so with a generic MQTT availability won’t come into play. Obviously, the item value could be stale, but that might be okay. You could also add the availability topic to the generic as a switch to track, but it won’t cause the generic thing to go offline. Just a suggestion, but is what I would do.

FWIW I only use generic MQTT but did try HA things in the beginning. It is a little more advanced to set up using generic, so is not for everyone. My main reason for generic things could be described as “neatness”. I only create the channels that I use, the HA things have too much clutter (IMO).

how about this: Thing Configuration

I guess this will be the way I will go (generic MQTT)

Is there a easy way to format the existing yaml code from the HA discovered thing to a generic mqtt code?

At least give it a try maybe on one device and see if you like it better. You can always go back to HA.

The topic is easy. It is the highlighted one in your post 7. The trickier part is the Transformation. I may have used AI in my similar situation with a shelly device. However, once you get one working you can just copy/paste in the code tab. Not an expert, but this is a working example with a JSON

The transformation to extract the temperature (100, 104 are separate thermocouples). On the code tab

      Furnace_Return_Air:
        type: number
        label: Furnace Return Air Temperature
        config:
          stateTopic: shellyplusuni-8c4f00a6cbec/events/rpc
          transformationPattern:
            - "JSONPATH:$.params['temperature:100'].tF" //This drills down to what you want. In my case it is the tF in the above picture
          formatBeforePublish: '%s'
          retained: false
          postCommand: false
          step: 1
          unit: °F
      Furnace_Main_Floor_Vent:
        type: number
        label: Furnace Main Floor Outlet Temperature
        config:
          stateTopic: shellyplusuni-8c4f00a6cbec/events/rpc
          transformationPattern:
            - "JSONPATH:$.params['temperature:104'].tF"
          formatBeforePublish: '%s'
          retained: false
          postCommand: false
          step: 1
          unit: °F

I managed to create all retrieving channels but I am struggling hard with the command ones .

This is the HA automatic discovered part

```
channels:
  adaptive_mode:
    type: switch
    label: Adaptive Mode
    config:
      component: switch
      nodeid: HMJ-2_009b08a56a51
      objectid:
        - adaptive_mode
      config:
        - "{\"name\":\"Adaptive Mode\",\"icon\":\"mdi:auto-fix\",\"state_topic\":\"hm2mqtt/HMJ-2/device/009b08a56a51/data\",\"value_template\":\"{{ value_json.adaptiveMode }}\",\"command_topic\":\"hm2mqtt/HMJ-2/control/009b08a56a51/adaptive-mode\",\"payload_on\":\"true\",\"payload_off\":\"false\",\"state_on\":true,\"state_off\":false,\"availability\":[{\"topic\":\"hm2mqtt/availability\",\"payload_available\":\"online\",\"payload_not_available\":\"offline\"},{\"topic\":\"hm2mqtt/HMJ-2/availability/009b08a56a51\",\"payload_available\":\"online\",\"payload_not_available\":\"offline\"}],\"unique_id\":\"009b08a56a51_adaptive_mode\",\"device\":{\"ids\":[\"hame_energy_009b08a56a51\"],\"name\":\"HAME Energy HMJ-2 009b08a56a51\",\"model_id\":\"HMJ-2\",\"manufacturer\":\"HAME Energy\",\"sw_version\":\"116.6\",\"connections\":[[\"bluetooth\",\"00:9B:08:A5:6A:51\"]]},\"origin\":{\"name\":\"hm2mqtt\",\"url\":\"https://github.com/tomquist/hm2mqtt\"}}"

```

I tried as commandTopic: hm2mqtt/HMJ-2/control/009b08a56a51/adaptive-mode

But a look into mqtt explorer does not show a topic like this
If i hit the switch the value in mqtt explorer is not changing and it a new topic control is appearing?
KI is not a big help in this case, I tried

What do you see in the openhab.log? If that topic doesn’t appear in MQTT Explorer it means the message wasn’t publishes which is likely caused by an error on the OH side.

Also, what is being sent as the message? It’s almost certainly not going to accept just “ON” and “OFF” as the message.

Until the topic has a message in it, MQTT Explorer will not show that the topic exists.

Here an example with the command part:

things:
  mqtt:topic:Pool:
    bridge: mqtt:broker:MosquittoMqttBroker
    label: Pool Sicherung
    channels:
      state:
        type: switch
        label: State
        config:
          stateTopic: zigbee2mqtt/Hoch1/state
          commandTopic: zigbee2mqtt/Hoch1/set
          formatBeforePublish: '%s'
          retained: false
          postCommand: false
          "on": "ON"
          "off": "OFF"

or with format before publish

config:
          commandTopic: zigbee2mqtt/LichtWohn/set
          formatBeforePublish: "{\"scene_recall\": %s}"
          retained: false
          postCommand: false
          step: 1

I didn’t see that parameter in your data either. Do you have a way to trigger that from the hm2.Mqtt application and see if something is published in Explorer? It may not be retained, so will only appear for a second or two. You really need to see that in Explorer, otherwise you are just guessing

As to to the message, the other responses are right. From the HA config it look like a true or false, and as noted command usually have the trailing “set”.

EDIT: My Shelly doesn’t have control channels, but here is another switch example (with true/false)

      Shop_Area_Lights_n:
        type: switch
        label: Shop Area Lights
        config:
          stateTopic: zwave2/Shop_Lights/switch_binary/endpoint_0/currentValue
          commandTopic: zwave2/Shop_Lights/switch_binary/endpoint_0/targetValue/set
          transformationPattern:
            - JSONPATH:$.value
          "on": "true"
          "off": "false"

but I have explorer data for reference

I found out that the normally not existing topic path is the right one for posting commands.

hm2mqtt is listening for such entries.
The switches are now ok.
But I have one where I can not find a way to work.
This is the original channel config:
```

- "{\\"name\\":\\"Charging Mode\\",\\"state_topic\\":\\"hm2mqtt/HMJ-2/device/009b08a56a51/data\\",\\"value_template\\":\\"{% set mapping = {\\\\\\"chargeDischargeSimultaneously\\\\\\":\\\\\\"Simultaneous Charging/Discharging\\\\\\",\\\\\\"chargeThenDischarge\\\\\\":\\\\\\"Fully Charge Then Discharge\\\\\\"} %}{% set stringifiedValue = value_json.chargingMode | string %}{% if stringifiedValue in mapping %}{{ mapping\[stringifiedValue\] }}{% else %}{{ stringifiedValue }}{% endif %}\\",\\"command_template\\":\\"{% set mapping = {\\\\\\"Simultaneous Charging/Discharging\\\\\\":\\\\\\"chargeDischargeSimultaneously\\\\\\",\\\\\\"Fully Charge Then Discharge\\\\\\":\\\\\\"chargeThenDischarge\\\\\\"} %}{% set stringifiedValue = value | string %}{% if stringifiedValue in mapping %}{{ mapping\[stringifiedValue\] }}{% else %}{{ stringifiedValue }}{% endif %}\\",\\"command_topic\\":\\"hm2mqtt/HMJ-2/control/009b08a56a51/charging-mode\\",\\"options\\":\[\\"Simultaneous Charging/Discharging\\",\\"Fully Charge Then Discharge\\"\],\\"availability\\":\[{\\"topic\\":\\"hm2mqtt/availability\\",\\"payload_available\\":\\"online\\",\\"payload_not_available\\":\\"offline\\"},{\\"topic\\":\\"hm2mqtt/HMJ-2/availability/009b08a56a51\\",\\"payload_available\\":\\"online\\",\\"payload_not_available\\":\\"offline\\"}\],\\"unique_id\\":\\"009b08a56a51_charging_mode\\",\\"device\\":{\\"ids\\":\[\\"hame_energy_009b08a56a51\\"\],\\"name\\":\\"HAME Energy HMJ-2 009b08a56a51\\",\\"model_id\\":\\"HMJ-2\\",\\"manufacturer\\":\\"HAME Energy\\",\\"sw_version\\":\\"116.6\\",\\"connections\\":\[\[\\"bluetooth\\",\\"00:9B:08:A5:6A:51\\"\]\]},\\"origin\\":{\\"name\\":\\"hm2mqtt\\",\\"url\\":\\"https://github.com/tomquist/hm2mqtt\\"}}"

```
And this is what I tried in channel config
```

MARSTEK_Speicher_C_MQTT_HMJ2_009b08a56a51_Charging_Mode: type: string label: MARSTEK Speicher C HMJ2 009b08a56a51 Charging Mode description: "" config: stateTopic: hm2mqtt/HMJ-2/device/009b08a56a51/data commandTopic: hm2mqtt/HMJ-2/control/009b08a56a51/charging-mode transformationPattern: - JSONPATH:$.chargingMode retained: false postCommand: false commandTransformation: - "SCRIPT:javascript:|- var value = input; if (value == 'Simultaneous Charging/Discharging') { value = 'chargeDischargeSimultaneously'; } else if (value == 'Fully Charge Then Discharge') { value = 'chargeThenDischarge'; } value;"

```

Any Idea for this one?

I already adaptet the item:

```

metadata:
stateDescription:
value: ""
config:
options: "chargeDischargeSimultaneously=Simultaneous Charging/Discharging,chargeThenDischarge=Fully Charge Then Discharge"

```

Edit:
I struggle hard with this code editing text …

I had AI reverse engineer your config, maybe this will help.

{
  "name": "Charging Mode",
  "state_topic": "hm2mqtt/HMJ-2/device/009b08a56a51/data",
  "value_template": "{% set mapping = {\"chargeDischargeSimultaneously\":\"Simultaneous Charging/Discharging\",\"chargeThenDischarge\":\"Fully Charge Then Discharge\"} %}{% set stringifiedValue = value_json.chargingMode | string %}{% if stringifiedValue in mapping %}{{ mapping[stringifiedValue] }}{% else %}{{ stringifiedValue }}{% endif %}",
  "command_template": "{% set mapping = {\"Simultaneous Charging/Discharging\":\"chargeDischargeSimultaneously\",\"Fully Charge Then Discharge\":\"chargeThenDischarge\"} %}{% set stringifiedValue = value | string %}{% if stringifiedValue in mapping %}{{ mapping[stringifiedValue] }}{% else %}{{ stringifiedValue }}{% endif %}",
  "command_topic": "hm2mqtt/HMJ-2/control/009b08a56a51/charging-mode",
  "options": [
    "Simultaneous Charging/Discharging",
    "Fully Charge Then Discharge"
  ],
  "availability": [
    {
      "topic": "hm2mqtt/availability",
      "payload_available": "online",
      "payload_not_available": "offline"
    },
    {
      "topic": "hm2mqtt/HMJ-2/availability/009b08a56a51",
      "payload_available": "online",
      "payload_not_available": "offline"
    }
  ],
  "unique_id": "009b08a56a51_charging_mode",
  "device": {
    "ids": ["hame_energy_009b08a56a51"],
    "name": "HAME Energy HMJ-2 009b08a56a51",
    "model_id": "HMJ-2",
    "manufacturer": "HAME Energy",
    "sw_version": "116.6",
    "connections": [
      ["bluetooth", "00:9B:08:A5:6A:51"]
    ]
  },
  "origin": {
    "name": "hm2mqtt",
    "url": "https://github.com/tomquist/hm2mqtt"
  }
}

I did caution that generic MQTT is a little more complex. :wink:

I had success even if the reverse example did not work.

Fun fact: after changing one thing to the mqtt generic thing all others seem to work too.

No issues since some days.

Interessting is this:
All items with informations from the relevant thing are working fine but the gui shows this

grafik

You have to configure the Generic MQTT thing’s availability settings.

Thanks, that’s it