Just a little bit of code so you can remotely get values from these sensors. I have them connected together using the Breakout Garden (from Pimoroni) on a Pi Zero.
You’ll need flask installed, sudo apt install python-flask
if you haven’t. Otherwise it just uses the python modules you have already from installing the devices.
You can test it with:
curl -s [IP of the Pi with the sensors]:5000/sensor/co2_voc
and
curl -s [IP of the Pi with the sensors]:5000/sensor/uv_raw
curl -s [IP of the Pi with the sensors]:5000/sensor/uv_index
#!/usr/bin/env python
from flask import Flask, jsonify, make_response
import veml6075
from sgp30 import SGP30
import smbus
import sys
bus = smbus.SMBus(1)
sgp30 = SGP30()
# Create VEML6075 instance and set up
uv_sensor = veml6075.VEML6075(i2c_dev=bus)
uv_sensor.set_shutdown(False)
uv_sensor.set_high_dynamic_range(False)
uv_sensor.set_integration_time('100ms')
def crude_progress_bar():
sys.stdout.write('.')
sys.stdout.flush()
sgp30.start_measurement(crude_progress_bar)
app = Flask(__name__)
@app.route('/sensor/uv_raw', methods=['GET'])
def uv_raw_readings():
uva, uvb = uv_sensor.get_measurements()
return jsonify({'uva_raw' : uva, 'uvb_raw' : uvb})
@app.route('/sensor/uv_index', methods=['GET'])
def uv_index_readings():
uva, uvb = uv_sensor.get_measurements()
uv_comp1, uv_comp2 = uv_sensor.get_comparitor_readings()
uv_indices = uv_sensor.convert_to_index(uva, uvb, uv_comp1, uv_comp2)
return jsonify({'uva_index' : float(round(uv_indices[0],3)), 'uvb_index' : float(round(uv_indices[1], 3))})
@app.route('/sensor/co2_voc', methods=['GET'])
def co2_voc():
split_result = str(sgp30.get_air_quality()).split(' ')
first = 0
co2 = 0
voc = 0
for i in split_result:
if i.isdigit() and first == 0:
co2 = i
first = 1
if i.isdigit() and first == 1:
voc = i
return jsonify({'CO2' : co2, 'VOC' : voc})
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=False)
And the HA bits for your sensors.yaml
## CO2 & VOC Sensor - Raspberry Pi Breakout
- platform: rest
name: CO2 and VOC levels
json_attributes:
- CO2
- VOC
resource: http://192.xxx.xxx.xxx:5000/sensor/co2_voc
- platform: template
sensors:
co2_levels:
friendly_name: CO2 Levels
value_template: '{{ states.sensor.co2_and_voc_levels.attributes["CO2"] }}'
unit_of_measurement: ppm
- platform: template
sensors:
voc_levels:
friendly_name: VOC Levels
value_template: '{{ states.sensor.co2_and_voc_levels.attributes["VOC"] }}'
unit_of_measurement: ppb