Gazpar data retrieval example

Hello,

Following discussion here: [SOLVED] Jsonpath or JS transform for MQTT transform, I figured out it would probably be better to start a new thread to describe how to retrieve Gazpar data.

Gazpar is a gas counter used in France, and deployed by GRDF.
Just like Linky, there is virtually no API. Only method is to log into website a retrieve data manually.
Some clever people (not me :smiley:) have made up a script to parse web data and export to JSON.
I use the script located here: https://github.com/empierre/domoticz_gaspar
It is originally made for domoticz, and once Json is retrieve, just need to add a few lines to publish to MQTT.

I have modified the file gaspar_json.py like this:

Add this at the beginning:
import paho.mqtt.client as mqtt

Then:

def main():
    logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)

########### Add MQTT connection
    client = mqtt.Client()
    client.connect('192.168.0.114',1883,60)

I only export days values:

        res_day = gaspar.get_data_per_day(token, dtostr(today - relativedelta(days=1, months=1)), \
                                         dtostr(today - relativedelta(days=1)))

then publish to MQTT:

        try:
            export_days_values(res_day)
### PUBLISH to MQTT
            client.publish('gazpar/json', json.dumps(res_day), 1)

Add shell script to crontab (website is not reliable, so questing several times per day):

00 11,13,15,17,19,21,23 * * * ( /etc/openhab2/scripts/gazpar/domoticz_gaspar/domoticz_gaspar.sh  >> /etc/openhab2/scripts/gazpar/domoticz_gaspar/domoticz_gaspar_cron.log 2>&1 )

.things file:

        Thing mqtt:topic:mosquitto:gazpar "Gazpar" (mqtt:broker:mosquitto) @ "Salon" {
                Channels:
                Type string : gazpar-consommation-str "Consommation Gaz" [ stateTopic="gazpar/json", transformationPattern="JSONPATH:$.[-1].conso" ]
                Type string : gazpar-json "Consommation Gaz" [ stateTopic="gazpar/json" ]
	}

.items file:

// Gazpar :
String Gazpar_Consommation_str "Consommation gaz [%s kWh]" <energy>
    { channel="mqtt:topic:mosquitto:gazpar:gazpar-consommation-str" }

String Gazpar_json "Consommation gaz json"
    { channel="mqtt:topic:mosquitto:gazpar:gazpar-json" }

Number Gazpar_Consommation "Consommation gaz [%.2f kWh]" <energy>

.rules file: I am trigerring on json items change, this way rule does not fire when json stays the same.

rule "Conversion Gazpar str to num"
when
    Item Gazpar_json changed
then
    Gazpar_Consommation.postUpdate(Float::parseFloat(String::format("%s",Gazpar_Consommation_str.state).replace(',','.')))
    logInfo("gazpar", "cons gaz: " + Gazpar_Consommation)
end 

Works pretty well. Not really sure why sometimes I am getting very high values (as can be seen on the grafana snapshot). Have added these 2 items to persistance:

// Gazpar
	Gazpar_Consommation: strategy = everyChange, everyUpdate
	Gazpar_json: strategy = everyChange

Grafana config:

SELECT mean("value") FROM "Gazpar_Consommation" WHERE time > now() - 15d GROUP BY time(1d)

Hope this helps

2 Likes

Thanks! :+1:

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.