Integrate a self written Arduino Sensor into Home Asssistant
In this post you learn how to integrate a self built sensor with an Arduino Board ( UNO/MEGA or an ESP82666 / ESP32) into Home Assistant. There are several examples which show the usage of MQTT or ESP Home. Here I will show how to integrate a sensor based on plain JSON via HTTP.
Pre requisits
Regarding Home assistant you will need:
- A running instance of Home Assistant (HA). In my case I'm running HA on a Raspberry Pi5.
- In HA you have to install file File Editor (Install via Settings / Add ons / ADD-ON STORE called "File editor").
- You know how to add new entities (sensors) to your dashboard.
Obvious you will need as device
- A microcontroller with a sensor and a working network setup (for example an Arduino UNO, or an ESP8266, ESP32...)
- A running webserver on that microcontroller which provides the sensor
data in plain text or as JSON.
This example will be based on my Generic ESP Webserver but any other webserver should be usable.
Arduino: UNO Sensor Board with JSON/HTTP
In this example I'm using an Arduino UNO with a W5100 Ethernet Board. This UNO has several sensors: a DHT21 (outdoor temperature and humidity) an indoor BMP (temperature and pressure) and a MQ12 (CO2) Sensor. The Arduino webserver is used to display a webpage and this webpage is updated with AJAX JavaScript. The JSON on that Arduino looks like:
{"ss":1102003,"t":12.30,"h":1.00,"dl":5.2,"dh":12.6,"t1":20.30,"p":1011.3,"co":1051}
The tag value pairs are quite simple ss is the runtime in seconds, t is the temperature, h is the humidity and so on.
Two Options for Sensor Data: RESTful and Sensor (REST)
To integrate a new entity (sensor value) you can either use the RESTful or Sensor integration
- Settings / Devices & Servies / ADD INTEGRATION / RESTful (for multiple sensors using the same endpoint)
- Settings / Devices & Servies / ADD INTEGRATION / Sensor (sometimes also called REST) (supports only one sensor per request)
As the RESTful integration has the possibility to generate several sensors from one endpoint, I propose to use RESTful (instead of sensor).
HA: The configuration.yaml
You could add your RESTful integration directly in your configuration.yaml. I propose to put all RESTful integrations in a separate file and just include that file in configuration.yaml. In my example the restful file will be called myrestful.yaml.
# Loads default set of integrations. Do not remove. default_config: # Load frontend themes from the themes folder frontend: themes: !include_dir_merge_named themes automation: !include automations.yaml script: !include scripts.yaml scene: !include scenes.yaml # My entries rest: !include myrestful.yaml
HA: My myrestful.yaml
Create a new file called "myrestful.yaml". In myrestufl.yaml you define the entities of an endpoint (resource). This example is based on the mentioned JSON. It creates one sensor with several entities:
# myrestful.yaml # Tip: If you want to create multiple sensors using the same endpoint, use the RESTful configuration instructions. #rest: - resource: http://172.18.67.96/ajax scan_interval: 300 sensor: - name: "96 Runtime" value_template: "{{ (value_json.ss / 60 / 60) | round(0)}}" unit_of_measurement: "h" - name: "96 Temp out" value_template: "{{ value_json.t }}" unit_of_measurement: "°C" - name: "96 Humidity" value_template: "{{ value_json.h }}" unit_of_measurement: "%" - name: "96 Temp low" value_template: "{{ value_json.dl }}" unit_of_measurement: "°C" - name: "96 Temp High" value_template: "{{ value_json.dh }}" unit_of_measurement: "°C" - name: "96 Temp in" value_template: "{{ value_json.t1 }}" unit_of_measurement: "°C" - name: "96 Pressure" value_template: "{{ value_json.p }}" unit_of_measurement: "hP" - name: "96 CO2" value_template: "{{ value_json.co }}" unit_of_measurement: "ppm"
- #rest
- The first line #rest: is commented as this is already in the configuration.yaml before the include command.
- resource
- In resource you define your web resource for the JSON output. In the example it is http://172.18.67.96/ajax
- scan_interval
- The scan interval is in seconds.
- sensor
- sensor starts a new section. One Endpoint can provide several sensors. One Sensor can have several entities.
- name
- the name for the entity
- value_template
- The value_template is used to extract the data from the JSON
- unit_of_measurement
- unit_of_measurement: will also be used to determine the kind of chart you see for that entity in a dashboard.
Restart HA Configuration
When you have modified (added) a plattform in the configuration.yaml you must RESTART your configuration. Usually this is only needed once. Use
Developer tools / Check and Restart / Restart
Later, when you just do adoptions in the myrestful.yaml it is sufficent if you just do a YAML configuration reloading.
Check Configuration
After you changed files you can do a quick check of your files in Developer Tools / YAML / CHECK CONFIGURATION.
Reload YAML configuration
You will find it in the Developer tools also:
Developer tools / YAML configuration reloading / All YAML configuration
HA: Add New Sensor to your Dashboard
Now you can add your new sensor data into a dashboard. For example:
- Add a new card by Entity
- chose your entity (in this example xxxx) and press CONTINUE
- Home Assistant will notify "We created a suggestion for your" and leave the card with ADD TO DASHBOARD
Summary
As you see it is not complicated to integrate own sensor data into home assistant without MQTT and without ESP NOW. A plain HTTP/JSON interface is enough and you can integrate all your existing sensors into Home Assistant. If you have already sensors with a webinterface available, just let Home Assistant collect all data.