Skip to content

Node.js JavaScript to send Nordic Thingy:52 environment sensor data to Home-Assistant using MQTT.

License

Notifications You must be signed in to change notification settings

joakimtoe/thingy52_hass_mqtt_sensor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nordic Thingy:52 Node.js Home-Assistant MQTT Sensor Script

This script will connect to Nordic's Thingy:52 IoT Sensor Kit using Bluetooth, and send the environment sensor data to Home-Assistant set up with the MQTT Sensor component.

Raspberry Pi

Prerequisites

  1. A Raspberry Pi with built in Bluetooth or a Raspberry Pi and a Bluetooth USB dongle.
  2. The Raspbian Jessie operating system image.
  3. A Nordic Thingy:52 IoT Sensor Kit
  4. Home-Assistant with MQTT Sensor component
  5. Git, Node.js, npm and thingy52 lib.

Install

  1. Update the package manager: sudo apt-get update.
  2. Add the latest version of Node.js to package manager: curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
  3. Install Node.js and thingy52 lib dependencies: sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev git nodejs
  4. Clone the repository: git clone https://github.com/joakimtoe/thingy52_hass_mqtt_sensor.git
  5. Go into the thingy52_hass_mqtt_sensor folder. cd thingy52_hass_mqtt_sensor
  6. Install the thingy52 lib: npm install thingy52

Configure script

  1. Find your Thingy:52's bluetooth device address using. hcitool
pi@raspberrypi:~ $ sudo hcitool lescan
LE Scan ...
E2:16:3A:FD:67:46 Thingy
  1. Open config.json with nano config.json
  2. Remove ':' from the device address and add it (with this format e2163afd6746) to the parameter thingy_white_list in the config.json file. Or remove thingy_white_list completly to connect to all discovered Thingy:52's.
  3. Change mqtt_url, mqtt_username and mqtt_password according to your MQTT Broker. See Home-Assitant configuration chapter if in doubt. If you are using Home-Assitant's embedded MQTT broker see this page.

Run

To run the script: sudo node thingy_hass_mqtt.js

Autorun

  1. Create a new systemd service sudo nano /etc/systemd/system/thingy-hass-mqtt.service
  2. Then copy/paste the following into the service file:
[Unit]
Description=Thingy Environment MQTT service
After=network.target ntpdate.service

[Service]
Environment=PATH=/usr/bin:/usr/local/sbin:/usr/local
ExecStart=/usr/bin/node /[path to thingy52_hass_mqtt_sensor]/thingy_hass_mqtt.js
StandardOutput=inherit
StandardError=inherit
Restart=always
User=root

[Install]
WantedBy=multi-user.target
Alias=thingy-hass-mqtt.service
  1. Change the path to your thingy52_hass_mqtt_sensor folder and save.
  2. Reload systemd: sudo systemctl --system daemon-reload
  3. To start automatically at boot, enable the service: sudo systemctl enable thingy-hass-mqtt. Use disable to turn off.
  4. To start it now use: sudo systemctl start thingy-hass-mqtt. Also supports stop, restart and status.
  5. To get the log use: sudo journalctl -f -u thingy-hass-mqtt

Home-Assistant

Configuration

  1. Setup MQTT and select MQTT Broker according to this page.
  2. Availible data are:
{
  "temp": "25",    // Temperature in Celsius
  "press": "1000", // Air Pressure in hPa
  "humid": "30",   // Relative Humidity in %
  "eco2": "500",   // Estimated CO2 in ppm
  "tvoc": "20",    // Total Volatile Organic Compound in ppb
  "batt": "85"     // Battery level in %
}
  1. Add sensors like in the following example.
# Example configuration.yaml entry
sensor:
  - platform: mqtt
    state_topic: "thingy/[lower case bluetooth device address without :]/environment"
    name: "Temperature"
    unit_of_measurement: "C"
    value_template: '{{ value_json.temp }}'

To find your Thingy:52's bluetooth device address use. sudo hcitool lescan

  1. Restart Home-Assitant and the added sensors will appear in the States view.

About

Node.js JavaScript to send Nordic Thingy:52 environment sensor data to Home-Assistant using MQTT.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published