Python plugin: Air quality monitoring (for France only)

Python and python framework

Moderator: leecollings

Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Python plugin: Air quality monitoring (for France only)

Post by Logread »

Another plugin (after BatteryLevel) I wrote using the excellent python plugin framework (again many thanks to its active developers), again not to drive a specific hardware but rather to conveniently automate many things that before needed to be done manually (device creation) and with custom scripting...

This plugin reads air quality data from the French PrevAir air quality network of stations across the country and exposes the air quality data from the nearest station (based on the location data of domoticz) or a specific station (if its code is provided in the plugin setup).

The devices exposed by the plugin in domoticz are:
- overall air quality index for today
- overall air quality index for tomorrow
- Detailed level for up to 6 pollutants (SO2, NO2, CO, O3, PM 10 and PM 2.5), depending on the selected station.

You can select the polling cycles for the data on the hardware page (between 1 hour and 24 hours... I recommend 1 hour as it also helps preventing the sensor devices from turning red with the default domoticz setup).

In addition, the device icons will have a different color (green, orange, red) depending on the level of the index or pollutant level (the scale might need some tweaking, bear with me on this for now)

A sample screen capture to illustrate:
Capture.PNG
Capture.PNG (194.54 KiB) Viewed 4072 times
The code is on GitHub at https://github.com/999LV/PrevAir. I have yet to write an installation/instructions document, but I am looking for testers feedback for possible bugs or improvement suggestions, so below some basic installation instructions (on a Raspberry... you need to tailor depending on your platform/setup):
PREREQUISITE: Domoticz beta version no less than 3.7418
1. in a shell terminal, navigate to the domoticz installation folder (e.g. ~/domoticz), and:

Code: Select all

cd plugins
mkdir PrevAir
sudo apt-get update
sudo apt-get install git
git clone https://github.com/999LV/PrevAir.git PrevAir
cd PrevAir
sudo chmod +x plugin.py
sudo /etc/init.d/domoticz.sh restart
2. Now connect to Domoticz with your browser and go to the “Setup/Hardware” page and select the “PrevAir France Air Quality Monitoring” type from the drop-down list
3. Enter the polling interval (1 hour recommended) and if relevant to you (see below) a specific station code (if no code is entered, the plugin will calculate the nearest station based on the atitude/longitude entered in your domoticz location settings)
4. Add” the new hardware…

note that only three devices are automatically created in the utilities tab of your domoticz (Index for today and tomorrow, name of the selected air quality station). Others (if data exists for your station) will be created automatically but marked "unused" until you manually change that setting in the devices section of domoticz.

If the default air quality station calculated is not your preferred (check www2.prevair.org for station location and pollutants surveyed), you can use a small utility I wrote that is also in the plugin installation folder to display all available station and obtain the code for an alternate station of your choice (you then need to enter this code in the plugin hardware page and hit "update" to restart the plugin with the new parameters):

Code: Select all

cd ~/domoticz/plugins/PrevAir
sudo chmod +x ListeStations.py
python3 ListeStations.py
Please let me know of any feedback. I am not a professional developer but I really enjoy using this python plugin framework to simplify my systems and possibly others will find this useful as well...
deennoo
Posts: 784
Joined: Wednesday 10 December 2014 13:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by deennoo »

Thanx ! Merci !

Works like a charme !
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / Tasmota / MQTT / BLE / Zigate
http://domo-attitude.fr
nmaster
Posts: 54
Joined: Saturday 16 August 2014 17:17
Target OS: Linux
Domoticz version: 3.6304
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by nmaster »

Hi Logread,

I installed your plugin in a domoticz test it before adding it on my main domoticz.

I first wanted to select a station, so I used the ListeStation.py.

The wanted station was:

'18042', 'P. Roth St Quentin', 'ATMO PICARDIE', '02691', 'SAINT-QUENTIN', '49.85133', '3.28400', 'Urbain']

But entering the station code 18042 I got in the logs:

Code: Select all

2017-07-09 15:59:29.189 (PrevAir) Initialized version 0.0.3, author 'logread'
2017-07-09 15:59:29.190 (PrevAir) Debug log level set to: 'false'.
2017-07-09 15:59:29.195 (PrevAir) Using polling interval of 1 hour(s)
2017-07-09 15:59:29.195 Error: (PrevAir) 'onStart' failed 'KeyError'.
2017-07-09 15:59:29.195 Error: (PrevAir) ----> Line 181 in /home/nicolas/Applis/domoticz/plugins/prevAir/plugin.py, function onStart
2017-07-09 15:59:29.195 Error: (PrevAir) ----> Line 108 in /home/nicolas/Applis/domoticz/plugins/prevAir/plugin.py, function onStart 
I removed the station code to let calculate the station automaticaly.
This worked, I got a Text sensor with value "OMISSY - P. Bert St Quentin (18008/02571) at 4km"

I edited the PrevAir Hardware to change to station 18042 and this time it worked.

It's definitively a nice plugin.

Great work.
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by Logread »

@nmaster,

Thanks for reporting your experience with the plugin. Actually found a small bug that explains why the plugin logged some errors when you first started it. I will fix that in a new version.

Can you please tell me which version of domoticz you are running ?

Thanks !
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by Logread »

@nmaster,

Thanks for reporting your experience with the plugin. Actually found a small bug that explains why the plugin logged some errors when you first started it. I will fix that in a new version.

Can you please tell me which version of domoticz you are running ?

Thanks !
nmaster
Posts: 54
Joined: Saturday 16 August 2014 17:17
Target OS: Linux
Domoticz version: 3.6304
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by nmaster »

@Logread

I'm using domoticz v 3.8070 built by myself.
nmaster
Posts: 54
Joined: Saturday 16 August 2014 17:17
Target OS: Linux
Domoticz version: 3.6304
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by nmaster »

Hi,

I wanted to install the plugin on a friend's domoticz v 3.8.067 but there is issues.

I added a hardware without selecting station code.

My friend is in Paris 12e, the nearest station has been automatically selected but some devices couldn't be created.

Here is the logs:

Code: Select all

 2017-07-09 18:57:36.816 (PREVAIR) Pollutant: Indice Global Jour = 4 (max = 0)
2017-07-09 18:57:36.816 (PREVAIR - Indice Global Jour) Updating device from 0:'4' to have values 0:'4'.
2017-07-09 18:57:36.827 (PREVAIR) Pollutant: Indice Global Demain = 3 (max = 0)
2017-07-09 18:57:36.827 Error: CPlugin:CDevice_init, unable to find module for current interpreter.
2017-07-09 18:57:36.827 Error: Device creation failed, Device object is not associated with a plugin.
2017-07-09 18:57:36.827 (PREVAIR) Pollutant: SO2 = False (max = False)
2017-07-09 18:57:36.827 (PREVAIR) Pollutant: NO2 = 13 (max = 18)
2017-07-09 18:57:36.827 Error: CPlugin:CDevice_init, unable to find module for current interpreter.
2017-07-09 18:57:36.827 Error: Device creation failed, Device object is not associated with a plugin.
2017-07-09 18:57:36.827 (PREVAIR) Pollutant: CO = False (max = False)
2017-07-09 18:57:36.827 (PREVAIR) Pollutant: O3 = False (max = False)
2017-07-09 18:57:36.827 (PREVAIR) Pollutant: PM10 = False (max = False)
2017-07-09 18:57:36.827 (PREVAIR) Pollutant: PM25 = False (max = False) 
My friend uses domoticz prebuilt binaries, not own built ones like me.
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by Logread »

nmaster wrote:Hi,
I wanted to install the plugin on a friend's domoticz v 3.8.067 but there is issues.
I added a hardware without selecting station code.
My friend is in Paris 12e, the nearest station has been automatically selected but some devices couldn't be created.

My friend uses domoticz prebuilt binaries, not own built ones like me.
I am seeing the same issue with several plugins and have been banging my head on the wall for several days about this issue with device creation in recent domoticz releases (seems updating a newly created device in the same callback function creates an error)... Your post prompted me to test my plugins on version 3.8072 that I compiled myself rather than using the linux/RPi pre-builds that I usually use...
Et voilà... Issue solved !

Seems the pre-built packages have a bug within the python framework. I'll drop a note to Gizmocuz or the python framework developers to see if they can fix this...
nmaster
Posts: 54
Joined: Saturday 16 August 2014 17:17
Target OS: Linux
Domoticz version: 3.6304
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by nmaster »

Yes I think you are right about prebuilt binaries.

I built a domoticz for my friend and there is no more issue.
gysmo38
Posts: 50
Joined: Saturday 01 February 2014 13:42
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by gysmo38 »

I've got same issue with my two plugins.

It work well under Ubuntu where I use source code and not from my Raspberry where I use package.
chrisl
Posts: 3
Joined: Monday 17 July 2017 14:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by chrisl »

Hi, I managed to install and configure the plugin. Thank you for your tutorial.
I have a question though. Would it be possible to connect to 2 different air stations?
Indeed, the closest station from my place (which I'd like to keep as it should provide accurate values for my place) only provides a few airborne pollutant, however, it does not provide daily air quality and prevision. A farther station shows these 2.
Do you have a solution?
Thank you,
Chris.
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by Logread »

chrisl wrote:Hi, I managed to install and configure the plugin. Thank you for your tutorial.
I have a question though. Would it be possible to connect to 2 different air stations?
Indeed, the closest station from my place (which I'd like to keep as it should provide accurate values for my place) only provides a few airborne pollutant, however, it does not provide daily air quality and prevision. A farther station shows these 2.
Do you have a solution?
Thank you,
Chris.
I did not test it, but you should be able to install two instances of the plugin (add the "hardware" twice) with each a different station. You can then decide which "devices" you want displayed for each station (mark them as "used" or "unused" as you need).
This should work but again I did not test it, so please report your feedback.
chrisl
Posts: 3
Joined: Monday 17 July 2017 14:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by chrisl »

Hello,
I tried to add a new hardware of the same type (PrevAir France ...) but it did not work. "Problem adding hardware" is reported.
I tried specifying 2 different station codes and without specifying any. No success.
Cheers,
Christophe
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by Logread »

chrisl wrote:Hello,
I tried to add a new hardware of the same type (PrevAir France ...) but it did not work. "Problem adding hardware" is reported.
I tried specifying 2 different station codes and without specifying any. No success.
Cheers,
Christophe
Thanks for reporting. I'll try to understand why two instances of the same plugin cannot work.
chrisl
Posts: 3
Joined: Monday 17 July 2017 14:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by chrisl »

Hi,
I'll make a try by duplicating the PrevAir folder to a PrevAir2. I'll check the occurrences of PrevAir in the folder files and update these.
I'll let you know.
Chris.
deennoo
Posts: 784
Joined: Wednesday 10 December 2014 13:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by deennoo »

hi there, have you update your plugn to the last element requiered ?
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / Tasmota / MQTT / BLE / Zigate
http://domo-attitude.fr
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by Logread »

deennoo wrote: Saturday 12 August 2017 13:37 hi there, have you update your plugn to the last element requiered ?
Not sure what you mean... but if it is about the reçent breaking changes in the python framework, no worries about it as the plugin does not use the Connection object nor the onConnect / onMessage functions, instead relying on the urllib library.

On the other hand, due to a bug introduced in the python plugin framework around domotiçz version 8100 (creating/updating multiple devices at once in the same function pass throw errors), you may initially see some error messages about devices not updated... after a few plugin restarts (i.e. "Update" in the hardware tab), all relevant devices end up being created and no more errors appear... I need to spend some time on this to understand the root cause, but it is not on my critical "to do" list for now due to vacations.
deennoo
Posts: 784
Joined: Wednesday 10 December 2014 13:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by deennoo »

Logread wrote:
deennoo wrote: Saturday 12 August 2017 13:37 hi there, have you update your plugn to the last element requiered ?
Not sure what you mean... but if it is about the reçent breaking changes in the python framework, no worries about it as the plugin does not use the Connection object nor the onConnect / onMessage functions, instead relying on the urllib library.

On the other hand, due to a bug introduced in the python plugin framework around domotiçz version 8100 (creating/updating multiple devices at once in the same function pass throw errors), you may initially see some error messages about devices not updated... after a few plugin restarts (i.e. "Update" in the hardware tab), all relevant devices end up being created and no more errors appear... I need to spend some time on this to understand the root cause, but it is not on my critical "to do" list for now due to vacations.
Team that about new framework...
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / Tasmota / MQTT / BLE / Zigate
http://domo-attitude.fr
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by papoo »

Thanks
works great!
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: Python plugin: Air quality monitoring (for France only)

Post by papoo »

this plugin don't work with the 3.8360 beta version
it crash domoticz on my pi test
urllib.request compatibility surely
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest