So I spent like 3 days setting this up without an echo and with OpenHab + misquitto.
And finally works.
here are my steps.
- Using docker to create all openhab and mosquitto:
version: "3.7"
services:
mosquitto:
network_mode: host
image: eclipse-mosquitto
container_name: mosquitto
restart: unless-stopped
ports:
- "1883:1883"
- "9001:9001"
volumes:
- "<redacted>/configs/mosquitto/config:/mosquitto/config:rw"
- "<redacted>/configs/mosquitto/data:/mosquitto/data:rw"
- "<redacted>/configs/mosquitto/log:/mosquitto/log:rw"
version: '2.2'
services:
openhab:
image: "openhab/openhab:4.1.3"
restart: always
network_mode: host
volumes:
- "/etc/localtime:/etc/localtime:ro"
- "/etc/timezone:/etc/timezone:ro"
- "<redacted>/configs/openhab/addons:/openhab/addons"
- "<redacted>/configs/openhab/conf:/openhab/conf"
- "<redacted>/configs/openhab/userdata:/openhab/userdata"
environment:
CRYPTO_POLICY: "unlimited"
USER_ID: 999
GROUP_ID: 998
OPENHAB_HTTP_PORT: "11080"
OPENHAB_HTTPS_PORT: "11443"
setting up openhab:
I use Smarthome/J Amazon Echo Control
to connect to the AQN (amazon air quality monitor). You need to add https://download.smarthomej.org/addons.json
under Setting - > Json 3rd Party Add-on Service
to make it available.
With I created these two things:
This custom plugin recognizes all sensors on AQN. I created items
from the thing
ās channel
(crazy terminology).
With the MQTT binding I connected to the mosqitto MQTT server (I set it up all open as it can be only accessed from localhost). This way I only needed to set the ip address (no password is need. port is 1883, so no security layer), when I was configuring the MQTT binding.
Then I created this MQTT config script. This will tell HA that the sensors exists and where their values are. (you can do this in the HA config yaml too, but I wanted MQTT discovery to work):
/**
* Publishes commands and updates to the configured topic.
*/
var pubEvent = () => {
console.debug('Publishing an event bus event');
console.log(event);
var config_topic = 'homeassistant/sensor/aqmTemp/config';
var config = {
unique_id: "aqmTemp",
device_class: "temperature",
name: "Temperature",
state_topic: "homeassistant/sensor/aqm/state",
unit_of_measurement: "Ā°C",
value_template: '\{\{ value_json.Air_Quality_Monitor_Temperature}}',
device: {
name: "Amazon Smart Air Quality Monitor",
identifiers:["aqn"]
}
}
actions.get('mqtt', 'mqtt:broker:57645ab096').publishMQTT(config_topic, JSON.stringify(config));
var config_topic = 'homeassistant/sensor/aqmHum/config';
var config = {
unique_id: "aqmHum",
device_class: "Humidity",
name: "Humidity",
state_topic: "homeassistant/sensor/aqm/state",
unit_of_measurement: "%",
value_template: '\{\{ value_json.Air_Quality_Monitor_Humidity}}',
device: {
name: "Amazon Smart Air Quality Monitor",
identifiers:["aqn"]
}
}
actions.get('mqtt', 'mqtt:broker:57645ab096').publishMQTT(config_topic, JSON.stringify(config));
var config_topic = 'homeassistant/sensor/aqmCO/config';
var config = {
unique_id: "aqmCO",
device_class: "carbon_monoxide",
name: "Carbon Monoxide",
state_topic: "homeassistant/sensor/aqm/state",
unit_of_measurement: "ppm",
value_template: '\{\{ value_json.Air_Quality_Monitor_Carbon_Monoxide}}',
device: {
name: "Amazon Smart Air Quality Monitor",
identifiers:["aqn"]
}
}
actions.get('mqtt', 'mqtt:broker:57645ab096').publishMQTT(config_topic, JSON.stringify(config));
var config_topic = 'homeassistant/sensor/aqmVOC/config';
var config = {
unique_id: "aqmVOC",
device_class: "volatile_organic_compounds_parts",
name: "VOC",
state_topic: "homeassistant/sensor/aqm/state",
unit_of_measurement: "ppm",
value_template: '\{\{ value_json.Air_Quality_Monitor_VOC}}',
device: {
name: "Amazon Smart Air Quality Monitor",
identifiers:["aqn"]
}
}
actions.get('mqtt', 'mqtt:broker:57645ab096').publishMQTT(config_topic, JSON.stringify(config));
var config_topic = 'homeassistant/sensor/aqmPM25/config';
var config = {
unique_id: "aqmPM25",
device_class: "pm25",
name: "PM2.5",
state_topic: "homeassistant/sensor/aqm/state",
unit_of_measurement: " Āµg/mĀ³",
value_template: '\{\{ value_json.Air_Quality_Monitor_PM25}}',
device: {
name: "Amazon Smart Air Quality Monitor",
identifiers:["aqn"]
}
}
actions.get('mqtt', 'mqtt:broker:57645ab096').publishMQTT(config_topic, JSON.stringify(config));
var config_topic = 'homeassistant/sensor/aqmIAQ/config';
var config = {
unique_id: "aqmIAQ",
device_class: "aqi",
name: "Indoor Air Quality",
state_topic: "homeassistant/sensor/aqm/state",
value_template: '\{\{ value_json.Air_Quality_Monitor_Indoor_Air_Quality}}',
device: {
name: "Amazon Smart Air Quality Monitor",
identifiers:["aqn"]
}
}
actions.get('mqtt', 'mqtt:broker:57645ab096').publishMQTT(config_topic, JSON.stringify(config));
}
pubEvent();
This you basically only need to run it once, but I created a rule to send it when there is anew value on any of the senzors.
Then I have this scrip that runs when a senzor has a new value:
/**
* Publishes commands and updates to the configured topic.
*/
var pubEvent = () => {
console.debug('Publishing an event bus event');
var topic = 'homeassistant/sensor/aqm/state';
var multiple = 1;
switch(event.itemName){
case 'Air_Quality_Monitor_PM25':
multiple = 1000000000;
break;
case 'Air_Quality_Monitor_Carbon_Monoxide':
multiple = 1000000;
break;
}
var payload = {};
payload[event.itemName] = (parseFloat(event.itemState)*multiple);
var msg = JSON.stringify(payload);
console.debug('Publish - Topic: ' + topic + ' Message: ' + msg);
actions.get('mqtt', 'mqtt:broker:57645ab096').publishMQTT(topic, msg, true);
}
pubEvent();
These script assumes that you named your items (senzors) as Air_Quality_Monitor_XXX (see string in the script).
Then I installed MQTT binding to HA and after the config script run, Ha discovered the senzors (and it kinda ājustā works).
It is not a super low lever tutorial but I hope some part of them helps ppl.