MqttDasherService

Create an mqtt-dasher service. (Debian/Ubuntu distros).

mqtt-dasher : https://github.com/stjohnjohnson/mqtt-dasher

Limits permissions for promiscuous net socket access via libpcap.

This assumes npm install without the global flag so uses /opt/node_modules instead of /usr/lib.

Install

# install nodejs from debsource  
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -  
sudo apt-get install -y nodejs  
  
# install mqtt-dasher and libpcap dependency  
sudo apt-get install libpcap-dev  
sudo npm install mqtt-dasher  
  
# add user to run the service, and group to limit access to setcap files  
sudo useradd mqtt-dasher -d /opt/node_modules/mqtt-dasher -U  
  
# make a copy of nodejs to set privileged net capabilities on  
sudo cp `which nodejs` /opt/node_modules/mqtt-dasher/nodejs.setcap  
  
# set user and group for mqtt-dasher first  
sudo chgrp mqtt-dasher /opt/node_modules/mqtt-dasher/nodejs.setcap  
sudo chmod 750 /opt/node_modules/mqtt-dasher/nodejs.setcap  
  
# allow raw sockets and net admin for promiscuous mode  
sudo setcap 'CAP\_NET\_RAW+eip CAP\_NET\_ADMIN+eip' /opt/node_modules/mqtt-dasher/nodejs.setcap

Create the Service

/etc/systemd/system/mqtt-dasher.service

[Unit]
Description=mqtt-dasher service

[Service]
ExecStart=/opt/node_modules/mqtt-dasher/nodejs.setcap /opt/node_modules/mqtt-dasher/server.js
# WorkingDirectory=/opt/node_modules/mqtt-dasher   # Required on some systems
Restart=always
RestartSec=10             # Restart service after 10 seconds if node service crashes
StandardOutput=syslog     # Output to syslog
StandardError=syslog      # Output to syslog
SyslogIdentifier=mqtt-dasher
User=mqtt-dasher
Group=mqtt-dasher
Environment=CONFIG_DIR=/etc/mqtt-dasher NODE_ENV=production

[Install]
WantedBy=multi-user.target

Configure and start

sudo mkdir /etc/mqtt-dasher

/etc/mqtt-dasher/config.yml

mqtt:
  host: localhost
  ## Preface for the topics $PREFACE/$TOPIC
  ## (not in mqtt-dasher 0.1.0)
  #preface: ha/dash

buttons:
  abcdef:12:34:56: ha/dash/listerine
sudo systemctl enable mqtt-dasher  
sudo systemctl start mqtt-dasher

Home Assistant Switch

An mqtt switch which:

  • turns on when the dash button is activated
  • turns off when the dash button deactivates
  • can be activated and deactivated from HA
switch:  
- name: Listerine  
  platform: mqtt  
  command_topic: ha/dash/listerine  
  state_topic: ha/dash/listerine  
  payload_on: 'active'  
  payload_off: 'inactive'  
  retain: false

The switch isn't intended to be used in the HA UI, it should be mapped to a script instead.

To Do

Compile mqtt-dasher with nexe so that nodejs.setcap can't be so easily abused.