Using MQTT Discovery for buttons

Hello people,

I am using HA for my house, with Arduino Mega controllers on a wired network as endpoints for the actual control entities:

  • switches for relays
  • lights for both relays and dimmers
  • covers to control my sunscreens
  • locks to control my door locks
  • binary sensors for my pir sensors and magnet switches

All of these I have managed to integrate with the MQTT discovery mechanism (as do the Tasmotized wifi controllers I use), with a lot of trial and error. I have got it working now, which means I only have to setup my controller, and all the items (switches, lights, binary_sensors, covers, locks) are generated automatically in HA.
The only item on my list are the buttons I use to control the lights in my house. I could use a binary sensor, but that only gives me on/off behaviour. I also want things like ‘long press’ to be exposed.
At the moment I use my automations to read from a MQTT topic, while my Arduino code sends a ‘button0’ or ‘button0_long’ message, but I saw in the MQTT discovery documentation there is also a ‘device trigger’ (https://www.home-assistant.io/integrations/device_trigger.mqtt/) method I could use.
Reading the docs I am at a loss: the way to generate this seems similar to the other items, but I cannot understand how it works: Do I set up a ‘device trigger’ for each type of button press I want first, and then send output on the topic I just defined? And what command should I output then? In the setup I give the following config:

{"automation_type":"trigger","topic":"domus/test/uit/button","type":"button_short_press","subtype":"button_1","dev":{"ids":["domus_test_json"],"cns":[["ip","192.168.178.79"],["mac","F0:01:02:03:04:4B"]],"name":"Domus Mega Test","mdl":"Mega 2560","mf":"Arduino"}}

But nothing gets defined when I look at the integrations screen, where all the other items do show up.
What would be helpful is an example of both the setup of the discovery phase and an automation which uses this ‘device trigger’.

Thanks for all the help in advance,

Peter Mansvelder

1 Like

I finally figured it out myself: I made a configuration which sends the following info for each ‘button event’:

automation_type: trigger
topic: domus/mk/uit/button
type: button_short_release
subtype: button_1
payload: Button0
device:
  identifiers:
    - domus_meterkast_screens
  connections:
    - - ip
      - 192.168.178.43
    - - mac
      - '00:01:02:03:04:0B'
  name: Domus Mega Meterkast
  model: Mega 2560
  manufacturer: Arduino
platform: mqtt

And I published it on the MQTT topic ’ homeassistant/device_automation/mk_button1/config', making a different topic for each action type (button short press, button long press, etc.).

The only way I could see these items was in the ‘Automation edit GUI’, where the device triggers show up as separate triggers (First button releases, First Button continuously pressed, etc.), and I can use these as triggers for my automations. An example automation looks like this:

- id: knop_1_huiskamer
  alias: Knop 1 Huiskamer
  trigger:
  - device_id: 8aff222da27449a5869894917c6f00a9
    discovery_id: mk_button1
    domain: mqtt
    platform: device
    subtype: button_1
    type: button_short_release
  condition: []
  action:
  - entity_id: light.sierlamp_huiskamer
    service: homeassistant.toggle

The only issue I have with this is with testing: I expected to be able to see the ‘device triggers’ in the development screen under ‘events’, but even if I monitor all events with ‘*’, the device triggers don’t show up here. I am using the latest version: Home Assistant 0.109.1.

Hi Robert, I see you deleted your question; I was wondering, does that mean you figured it out, or do you still have questions? As I indicated in my answer, I finally got it working, so I would be glad to share this.

Hi Peter,

Thank you for your response. I deleted my question because I realized what I was trying to accomplish wasn’t related to device_triggers, and so probably didn’t belong in this thread.

Furthermore, it would seem I’m missing a core understanding of how HA works and how each of the components (triggers/services/automations/scripts/etc) fit together, and should spend some more time digging/reading before pestering others with questions whose answers I may uncover myself.

Thanks again for the response, and perhaps I’ll bug ya with a question down the road :wink:

Hello Robert,
you are very welcome, I am working on the same things, and I have just completed a library for Arduino with which I can build a controller that publishes its own set of configuration items to HA, so that I can use the “MQTT Discovery” integration to set up all of the required functions:

  • switches
  • lights (both switched and PWM-controlled brightness)
  • sensors
  • binary sensors (like pir sensors)
  • locks
  • covers
  • device triggers (buttons)
    This saves me a lot of configuration in HA, all of the separate entities are created by the discovery mechanism.
    So if you need a quick 101 on how to set up HA in combination with Arduino, I will be very glad to help.

Peter

Hey Peter,

So far I have figured out how to use the PubSubClient Arduino Library to connect to the MQTT broker running on HA and have been able to use MQTT Discovery to publish sensor data from Arduino to HA, and use binary_switch to toggle things on the Arduino. And as of last night (after the first post here) learned how to add a button to the Lovelace UI that when clicked publishes a message under a given topic which the Arduino then subscribes to.

That said, at this point it has all mostly been “Oh look, it worked!”, so a 101 would be awesome! Are there specific resources you would recommend to help me get more familiar with the HA ecosystem? I feel like everything I’ve done so far in terms of proper yaml configuration has been a combination of trial/error and pulling snippets from the forum. I find when reading through the documentation that I am still missing pieces that are required.

Your Arduino Library sounds great. I would love to take a look at it to see how it compares to what I have been doing (and perhaps just use it). Is it on GitHub?

Cheers,
Robert

Hi Robert,

sorry for not replying immediately, the last week has been busy with work and kids, I haven’t had much time to devote to my hobbies…
Yes, my repository is on GitHub, it is in the ‘arduino-sketches’ repository, I saw you were following me, so you have probably discovered it already; the way I set it up is as follows:

  • The main sketches are the ones with the name ‘Domus_Mega_’, they are really only set up headers for the main sketch, which is under ‘libraries/Domus_by_PM/domus.h’, here all the code is gathered.
  • I use the ‘Domus_mega_*’ sketches to set up the configuration of the Arduino Mega i use as a standardized controller, and in this sketch I do the following:

I set up compiler directives to include the libraries I want to use, depending on which sensors and interfaces I connected to the Arduino, which can include the following:

  • I2C sensors like the BMP280 pressure/temperature sensor
  • DHT-11/22 sensors (temperature/humidity)
  • MQ-type gas sensors, with a special lbrary to include the MQ-7
  • DS18B20 1-wire temperature sensor
  • LDR sensor for light measurement

I set up the pins for the sensors in the following code blocks for each type sensor.

I set up the ID and ethernet Mac address for the Arduino Mega’s ethernet shield, and the ID’s for homeassistant and MQTT. as well as the main MQTT topic.

The, for each MQTT discovery item, I set up the separate pin settings and parameters:

  • relays
  • lights
  • covers
  • locks
  • binary sensors
  • buttons
  • sensors

Pulse relays are a separate thing, and are only used internally, for the locks and covers.

That’s it, the rest is dealt with in the main code in the domus.h file, which takes all of its configuration from here.

There is also a ‘secrets.h’ file, not published on Github, which holds three parameters:

#define SECRET_SSID "<mySSID>"
#define SECRET_PASS "<Wifipassword>"
#define MQTTSERVER "ip/hostname of MQTT server"

And for the rest I do nothing but set up home assistant so that it uses MQTT and uses MQTT discovery, all of the items show up by themselves in Home Assistant, and from there on you can build your automations. I find in the latest version of HA, the GUI is quite usable for setting up automations.
Right now my setup for Home assistant is almost empty, except for some experimental things, for the main body of the HA setup I use only the discovered items. For instance, an automation for a light button looks like this:

- id: knop_1_huiskamer
  alias: Knop 1 Huiskamer
  trigger:
  - device_id: 8aff222da27449a5869894917c6f00a9
    discovery_id: mk_button1
    domain: mqtt
    platform: device
    subtype: button_1
    type: button_short_release
  condition: []
  action:
  - entity_id: light.sierlamp_huiskamer
    service: homeassistant.toggle

And this is set up by the GUI, not my writing the YAML by hand.
Another automation, based on a PIR binary sensor:

- alias: PIR Keuken aan
  id: kitchen_on_pir
  trigger:
  - entity_id: binary_sensor.pir_keuken
    from: 'off'
    platform: state
    to: 'on'
  condition:
    condition: or
    conditions:
    - condition: state
      entity_id: sun.sun
      state: below_horizon
    - condition: template
      value_template: '{{ states.sensor.lichtsterkte_huiskamer.state < states.input_number.light_level.state
        }}'
  action:
  - data:
      entity_id: light.keuken
    service: homeassistant.turn_on

This one was made by hand, but just because I already made it before the GUI became available, I could just as easily set it up with the GUI.

Please let me know if all this is helpful.