Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grab data from JSON and format correct (and send to influxDB) #479

Closed
Fux2 opened this issue Jan 16, 2021 · 2 comments
Closed

Grab data from JSON and format correct (and send to influxDB) #479

Fux2 opened this issue Jan 16, 2021 · 2 comments

Comments

@Fux2
Copy link

Fux2 commented Jan 16, 2021

Hi,
is it possible, to grab the "Temperature" and "Humidity" of the following JSON String?

{"Time":"2021-01-16T21:44:19","HTU21":{"Temperature":3.2,"Humidity":35.4,"DewPoint":-10.7},"TempUnit":"C"}

It should be forwarded to an influxDB:

[tele/sonoff4ch/SENSOR]
topic = tele/sonoff4ch/SENSOR
targets = log:info, influxdb:garageHumidity
format = value={Humidity}   # how to format correctly ?

Another issue is, that I can not use the topic a second time, to get humidity and temperature separately.

[tele/sonoff4ch/SENSOR]
topic = tele/sonoff4ch/SENSOR
targets = log:info, influxdb:garageTemp
format = value={Temperature}
    ...
configparser.DuplicateSectionError: While reading from 'mqttwarn.ini' [line 211]: section 'tele/sonoff4ch/SENSOR' already exists

So both values have to be transfered at once - is this possible?

Thanks for your help!

@jpmens
Copy link
Collaborator

jpmens commented Jan 16, 2021 via email

@Fux2
Copy link
Author

Fux2 commented Jan 16, 2021

Ok, I got it by help of this issue: #294
Here my solution as example:

#mqttwarn.ini
   ...
[garageTemp]
topic = tele/sonoff4ch/SENSOR
targets = log:info, influxdb:garageTemp
alldata = getJsonNestedTemp()
format = {Temperature}

[garageHumi]
topic = tele/sonoff4ch/SENSOR
targets = log:info, influxdb:garageHumi
alldata = getJsonNestedHumidity()
format = {Humidity}
#samplefuncs.py
   ...
import ast
   ...
def getJsonNestedHumidity(topic, data, srv=None):
    payload = ast.literal_eval(data["payload"])
    humi = payload["HTU21"]["Humidity"]
    d = dict( Topic = topic, Humidity = humi )
    return d

def getJsonNestedTemp(topic, data, srv=None):
    payload = ast.literal_eval(data["payload"])
    temp = payload["HTU21"]["Temperature"]
    d = dict( Topic = topic, Temperature = temp )
    return d

@Fux2 Fux2 closed this as completed Jan 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants