Homekit & Siri voice control for Nymea (using Homebridge & MQTT)

Hi all,

I love the excellent work in this hackster post that adds voice control to nymea: Low Code Private Voice controlled Home Automation - Hackster.io

However, I didn’t want to invest in another microphone at this point, so I created a setup to use Siri to activate nymea scenes. Since this approach uses nymea scripts, it should allow you to do pretty much anything nymea can do with Siri voice commands.

In short, I used the mqttthing plugin for Homebridge to create switches that can be triggered by Siri (or the Home app), and send an MQTT message that is picked up by Nymea and translated into an operation of my choice, e.g. toggling a generic switch that triggers a scene.

I’m only giving a brief explanation here, but if you have any questions, feel free to post them in this topic.

  • Background: if you’re not familiar with Nymea’s MQTT broker, have a look here first: sensor-on-mqtt-server · documentation

  • Set up nymea MQTT broker as explained in the above background link, using the following settings for the MQTT Server Interface:

    Interface: “Localhost” → I installed Homebridge on the same RPi as my Nymea setup
    Port: 1884 (default port 1883 is in use)
    SSL enabled: no (uncheck)
    Login required: no (uncheck) → this is required because the homebridge plugin I use, generates a new client ID at each reboot and Nymea requires a fixed client ID (and since we’re only accepting localhost connections, this seems fine from a security point of view)

  • Add MQTT permissions:

    Client info: not relevant, since we disabled login requirements in the MQTT Server Interface
    Allowed publish topics: nymea/setScene/# (# serves as wildcard to allow multiple scene names)
    Allowed subscribe filters: none (so remove #)

  • Install Homebridge: in my example, I’m running Nymea on a Raspberry Pi, and installed Homebridge on the same RPi. Setup instructions can be found here: Install Homebridge on Debian or Ubuntu Linux · homebridge/homebridge Wiki · GitHub

  • Install mqttthing plugin for Homebridge (via the Homebridge UI): GitHub - arachnetech/homebridge-mqttthing: A plugin for Homebridge allowing the integration of many different accessory types using MQTT. – this plugin will allow you to create switches that can be toggled in Homekit (using the Home app or Siri voice commands).

  • Create MQTT switches for the Homebridge plugin: via the homebridge-mqttthing plugin settings, add switches that will allow you to trigger MQTT commands to nymea.

  • Via the Homebridge Config Editor, you can change the config to match what we need: for this example, I created 2 switches that can be turned on manually and switch off automatically after a few seconds, so the config looks like this (127.0.0.1 is Localhost):

    "accessories": [
        {
            "type": "switch",
            "name": "Entertainment Off",
            "url": "127.0.0.1:1884",
            "topics": {
                "setOn": "nymea/setScene/entertainmentOff"
            },
            "turnOffAfterms": 3500,
            "accessory": "mqttthing"
        },
        {
            "type": "switch",
            "name": "Watch TV",
            "url": "127.0.0.1:1884",
            "topics": {
                "setOn": "nymea/setScene/watchTV"
            },
            "turnOffAfterms": 3500,
            "accessory": "mqttthing"
        }
    ]
    
  • Save your config and restart Homebridge, so the buttons will show up in Homekit.

  • Add an Internal MQTT client to Nymea via Configure Things, as explained in the background link (you may need to install nymea-plugin-mqttclient first), and have it listen to the following topic:

    Subscription topic filter: nymea/setScene/#

  • Using magic, you can now add the actions you want Nymea to execute when each message is received by the Internal MQTT client (make sure to select only “Payload = true”, otherwise the scene will also be triggered when Homebridge turns the button off after a few seconds (Payload = false)) → the same set of actions can also be converted to a scene, so you can also trigger it via Nymea-app:

You can now activate the Nymea scenes you created via the Home app and via Siri: “Hey Siri, watch tv” and “Hey Siri, entertainment off” will trigger the Watch TV scene or the Entertainment Off scene in Nymea respectively. Siri will respond with “OK, the Watch TV is on” or “OK, the Entertainment Off is on”, which sounds a bit weird, but hey… it works :slight_smile:

Edit: going one step further, I changed the name of the switches in Homekit to e.g. “Watch TV button” and created scenes (called f.i. “Watch TV”) that switch on the corresponding button. Siri now replies with “OK” or “Done” which makes more sense :slight_smile:

You can also create switches that can be used to turn on/off individual things, … so knock yourselves out.

I hope this can be of use to some of you. If you see ways to improve this setup, please let me know!

Best,
Rob

3 Likes

This is great work Rob and very clever use of MQTT. I will try it out soon.

Will

1 Like

Meanwhile I created some two-way-traffic MQTT between Nymea & Homebridge.

Using the above steps, I created a switch with the following config, that will directly control a thing’s power in nymea, and will also reflect the thing’s power status back from nymea:

        {
            "type": "switch",
            "name": "thingName",
            "url": "127.0.0.1:1884",
            "topics": {
                "setOn": "nymea/setScene/thingOnOff",
                "getOn": "homebridge/getStatus/thingOn"
            },
            "accessory": "mqttthing"
        }

Setting the power status via Homekit:
This is similar to the initial post. The setOn and setOff use the same topic, but the payload will be true of false respectively. The above nymea magic will therefore have to be altered a little: the “only consider event if payload is true” will have to be unchecked and the power status from Homekit will be used in the “execute those actions” step, where the (nymea) thing’s power value will be set to the value that was communicated in the MQTT message.

Setting the power status via Nymea:
This is new compared to the initial post. When the power status of the thing is changed in nymea, this will trigger magic that will send the new status to Homebridge so the status will be displayed correctly in Homekit.
A new magic rules have to be created in Nymea, that is triggered when the (nymea) thing’s power status is changed: the “execute those actions” of the rule will contain the topic in getOn above, and payload will the the (nymea) thing’s power status.

In order for the homebridge/getStatus/# topics to be allowed by Nymea, they have to be added to the Internal MQTT client configuration (see Add MQTT permissions in the initial post).

With the above setup you can control the MQTT switches in Homekit from Nymea. In addition you will need to add automations in Homekit to let the real devices do things when the state of the MQTT switches changes (and vice versa, otherwise the status of the MQTT switch and the status of the thing in Nymea will no longer reflect the status of the device the are supposed to simulate).

1 Like