Help a HA noob migrate away from an (old) MQTT based OpenHAB setup

So I had various forms of home automation happening with OpenHAB for the last 6 or so years and through an annoying sequence of events, I have bricked my OpenHAB setup. It was old and a bit clunky but done the job. It’s time to move on to HA I think…

Many of my items are DIY based on ESP8266 modules and utilize MQTT topics exclusively.

My MQTT server is still running fine and receiving data but in the short term, to get some minimum items back online, I want to link HA to the MQTT server and (hopefully) create objects similar to what I had in OpenHAB using HA.

An example:
I have an RFID reader which sends a code to -
ha/mod/ABCD/rf when a tag is read.
Openhab then used to process the received code, compare it to a known list and then publish back to ha/mod/ABCD/op with a message of 1 or 0 for allow or deny access.

This same module also functions as a doorbell, so when someone pushed the button, it would send a command to ha/mod/ABCD/db with a message of 1. OpenHAB would then play a sound on my Google Home speaker group.

Can someone give me some pointers on how I would go about this? Do I just have the MQTT integration subscribe to ha/mod/# (i have many modules) and somehow I create items that link to the further down path?

I’m really hoping I can get this working for now, as that’s the main functionality I am missing where I can’t just reflash it with ESPHome (yet).

I’m keen to learn it all, but I just need a leg up to get this core function enabled so I can migrate away over the next week or so. Please help :slight_smile:

You are thinking about this the wrong way. MQTT is just one tool in the box. Keep the MQTT server where it is. I started a few years ago with Node Red and Mosquitto (the MQTT Server) on a Raspberry Pi3. I upgraded to an Intel NUC i5 and installed the native X-86 HAOS image to it, but I kept the MQTT server on the Pi because too many of my devices had the IP address hard-coded into them,

Over the past year I have been moving more of my stuff running Arduino code to ESPHome. I only have five or six remaining that depend on MQTT.

By the way, about 20% of my devices are DIY, usually on Wemos D1 Mini boards.

So, here’s how to start. Buy a few Wemos D1 Minis. They are cheap, about $5 each, and are easy to work with using ESPHome. Use a breadboard and wire up some LEDs and buttons just to experiment with the ESPHome YAML code. Resist the siren call to go straight to the ESP32. It’s a bit more expensive, won’t fit on a standard breadboard, and for 99% of the simple IOT projects, overkill.

Try it. You’ll be hooked.

1 Like

Have a read of this for starters. It’s a little confusing because it launches straight into discovery topics etc, but right at the bottom talks about manual MQTT configuration.

So short story, install HAOS on suitable hardware, then the MQTT (Mosquitto) add-on (or use your existing broker, that’s fine).

Then you need to learn about yaml and add your devices to configuration.yaml. There are some examples in the docs - but I will paste some below.

Once you have migrated your existing devices, you can do as Stephen suggested and look at recoding them where you can, using ESPHome for example. That’s not completely necessary, I still have devices running in my HA environment with 5 year old Tasmota installs running them.

Some examples…

A light running through a Sonoff:

mqtt:

    light:
      - name: "BBQ Light"
        command_topic: "cmnd/sonoff1/power"
        state_topic: "stat/sonoff1/POWER"
        payload_on: "ON"
        payload_off: "OFF"
        retain: true

Some sensors:

    sensor:
      - name: "Kitchen Temperature"
        state_topic: "tele/sonoff5/SENSOR"
        value_template: "{{ value_json['AM2301'].Temperature }}"
        unit_of_measurement: "°C"

      - name: "Outside Air Pressure"
        state_topic: "tele/extbridge/SENSOR"
        value_template: "{{ value_json['BME280'].Pressure }}"
        unit_of_measurement: "hPa"
    
      - name: "Mailbox Battery"
        state_topic: "mailbox/sensor/battery/state"
        unit_of_measurement: "V"

A switch:

    switch:
      - name: Keypad
        command_topic: "MQTT/feeds/onoff"
        payload_on: "ON"
        payload_off: "OFF"

And some binary sensors for motion and alarms:

    binary_sensor:
      - name: "Shed PIR"
        state_topic: "stat/sonoff1/PIR"
        payload_on: "ON"
        payload_off: "OFF"
        device_class: motion
        
      - name: "Garage PIR"
        state_topic: "GaragePIR/tele/RESULT"
        value_template: "{{ value_json.TuyaReceived.CmndData | truncate(9, false, '') }}"
        payload_on: "650100010"
        off_delay: 3
        device_class: motion
    
      - name: "Garage PIR Tamper"
        state_topic: "GaragePIR/tele/RESULT"
        value_template: "{{ value_json.TuyaReceived.CmndData | truncate(9, false, '') }}"
        payload_on: "680100010"
        off_delay: 3
        device_class: vibration
1 Like

Also no, you need to define each sensor, switch etc as a seperate topic. Unless you rewrite your devices to send out discovery packets (not trivial).

1 Like

Either i didn’t explain myself properly or you missed my point. I totally understand and agree. All I need at this very moment is to be able to have a couple of essential functions work in HA via MQTT in the short term.

Thanks, that does help. I’ll have a play and see how far I get BUT - Where/How do I edit that file? Do I do it via SSH or is there somewhere in the web gui to be able to do it? I could not see it.

Yet another add on - the File Editor add-on.

Thanks so much for helping out.I spent the last few hours getting comfortable with the YAML and I have it working but I still feel like perhaps there’s a better way of doing things.

For example, this is the automation for my door:

alias: Open Cave Door
description: ""
trigger:
  - platform: mqtt
    topic: ha/mod/001C/rd
    payload: AABBCCDD
  - platform: mqtt
    topic: ha/mod/001C/rd
    payload: EEFFGG
  - platform: mqtt
    topic: ha/mod/001C/rd
    payload: HHIIJJ
  - platform: mqtt
    topic: ha/mod/001C/rd
    payload: KKLLMM
  - platform: mqtt
    topic: ha/mod/001C/rd
    payload: NNOOPP
  - platform: mqtt
    topic: ha/mod/001C/rd
    payload: QQRRSS
condition: []
action:
  - service: lock.open
    metadata: {}
    data: {}
    target:
      entity_id: lock.cave_lock
mode: single

I feel like I should be able to have a bunch of ‘or’ statements for the payload rather than having to define it so many times in the automation but I could not get that to work any way I formatted it. Is automation the right place to do it?

I’ve managed to set up a second scan to open another door too (based on a timer being started on read 1). Works perfect.

I’m still very fresh, and I generally don’t like asking for questions before I feel I have exhausted my own research but so far, I have not been able to come across a basic way of creating event based control like i would have before. i.e I would have assumed I could create a rule that will fire when an item is updated.

This means so far, although I have it working, it’s not as good as how I had it before.

i.e in that rule, I cant seem to create an if/else statement that would allow me to send a command back to the door module to beep as an access denied notification.

pseudocode for what I had with Openhab:
When rfid reader receives update
if update.value = x|y|z send command ‘open’
else
send command ‘denied’

All I seem to be able to do in the automation is:
if rfid reader receives update = x or
if rfid reader receives update = y or
if rfid reader receives update = z
send command open

I assume this is just because of my limited knowledge, i’m frustrated that I wanted to do this softly and take my time learning it but because openHAB died on me, i’ve had to move forward much faster than I anticipated.

Most of my devices I can flash with ESPHome and migrate over for a simpler setup but that will take a few weeks i think.

there is an if-then construct in HA:

if-then

it’s listed in the scripts section of the docs but it can be used in the action section of an automation (scripts are nothing more than an un-triggered section of automation actions).

I don’t use the UI editor for…well…anything that I don’t have to…I use yaml…but you should be able to select that from the UI automation editor as well if you use that.

And, I am exactly the opposite. (YAML-challenged). I use the UI when available and YAML when I must.

It depends on how you installed Home Assistant.
Mine is the native X-86 image on an Intel NUC, but this should work on any native binary install. I have the Samba Share add-on installed and this exposes the config folder. Any Samba client, like Windows File Explorer, can mount it. I have mine mapped to a drive letter (U: in my case) making it so easy. I just open drive U: in File explorer and double-click on whatever YAML file I want to edit.

Do not ask me anything about Proxmox, VM’s or other containers. I don’t use any of them. Why complicate an installation when it isn’t necessary? (Nod to the purists, I know that the native Home Assistant image is actually a Docker image running in a version of Debian called HAOS).

You can define the MQTT Trigger once then use its value_template option with a template to process the received value.

trigger:
  - platform: mqtt
    topic: ha/mod/001C/rd
    payload: 'on' 
    value_template: >
      {{ 'on' if value in ['AABBCCDD', 'EEFFGG', 'HHIIJJ', 'KKLLMM', 'NNOOPP', 'QQRRSS'] else 'off' }}
1 Like

There are many ways of integrating a device to Home Assistant. MQTT is only one. Personally I have been moving my Tasmota/MQTT devices to the ESPHome platform using the Home Assistant API instead of MQTT.

Let me clarify - I can see that there is an if/then statement but I can’t seem to use the item data for the statement, only the item state.

Once again, you missed my point. I meant specifically with MQTT and how I handle events. Right now, for these couple of items, I don’t have the time to rework the whole solution with a new PCB/UC/Wiring, I just need something to get me by, so trying to work with what I have. Which for the moment, is at least functional, just with some limitations.

I can see MQTT is not the best way to do things overall, and I will migrate away from it as much as I can but I guess I am used to much better scripted control over things whereas HA is somewhat restrictive to me so far. That seems to be both a combination of a lack of knowledge and some (perceived) limitations of HA.

First of all, don’t worry. MQTT is just fine

At the moment, you use mqtt as the trigger platform. This way, your solution with the bunch of triggers is fine.
The conventional approach would be to firstly create an entity with MQTT Sensor that reflects the topics content in it’s state and then automate from there.

So I looked into it more but simply could not get the results I wanted with the default automation setup. What I did do though was install node-red and I have been able to do everything I need so far.

Is this because of a lack of knowledge of HA automations? Probably, but it works great, the data is a bit easier to manage and it creates flows that are easily understandable.

Yes.

But if you’re more comfortable with Node-Red, then continue using it; whatever allows you to move forward.

I use Node Red for many of my functions (I don’t want to call them automations). It’s not that Home Assistant automations are missing something, but that there are some things that I can do in Node Red with just two or three nodes that would take me days to figure out how to do in Home Assistant.

I hope you installed the Node Red Add-on in Home Assistant. This populates your palette with Home Assistant nodes that you can’t get in the stand-alone Node Red.

Maybe you should edit your thread title. You certainly don’t come across as a Noob.