Push sensor vales to luftdaten

I have been looking into a way to push sensor values to luftdaten.info. Up until now the only option I have found is to install the luftdaten firmware on my wemos or ESP32 devices and then get the data back into Homeassistant via the Luftdaten sensor. This however conflicts a little bit with my, and the homeassistant, philosophy that Homeassistant should not have to rely on cloud services and operate locally as much as possible. I would therefore ideally like to rely on the robust esphome integration and afterwards push values to Luftdaten from within Homeassistant.

There is an API available for pushing data to Luftdaten http://api.luftdaten.info/v1/push-sensor-data/ but I have next to zero scripting experience.

Does anyone have a script or something available to achieve this in an easy fashion?

I was thinking about making the same change to my current setup just today. I am currently pushing the data from my Luftdaten device into a local Influxdb and from there pull the information into Home Assistant. That approach avoids using the detour through the cloud.

When moving to ESPHome to drive the device the high level approach would be:

  • Configure device to present sensors of PM2.5 and PM10 to Home Assistant (standard ESPHome functionality).
  • Create rest_command that points to the Luftdaten API URL you have already found. The payload of the command will be configured as a template and contains some fixed data (like your sensor ID) as well as some variable data (like the current sensor values).
  • Create automation that gets triggered every time the PM2.5 and PM10 are both updated, and in the action call the rest_command.

I just captured the payload that my sensor has sent:

{
  "software_version": "NRZ-2020-129", 
  "sensordatavalues":[
    {"value_type":"P1","value":"2.93"},
    {"value_type":"P2","value":"1.83"}
  ]
}

My sensor ID was sent as part of the User-Agent HTTP header, but this could also be added to the payload.

Apparently someone already did most of the hard work :slight_smile: now we just need to transform it into something usable for Homeassistantā€¦

Did any of you get around this? I just built a nice sensor with my oldest son, found the luftdaten ā€˜cloud detourā€™ to get data into HA too cumbersome, so I set up a ESPHome device and am able to get all data in HA. Iā€™d still like to push the data to Luftdaten though.

I havenā€™t due to vacation and extremely rusty coding skillsā€¦

Found an even simpler script, but now need some help in getting the sensor values from Homeassistant into itā€¦

EDIT: some progressā€¦

  1. Find your sensebox ID (go to: https://opensensemap.org/account and copy the ID)
  2. Find the ID of the sensor you would like to update (go to https://opensensemap.org/account click under the right sensebox ā€œEDITā€, Sensors and find the long sensor ID)
  3. Create a rest_command like below:
rest_command:
  post_opensensebox:
    url: https://api.opensensemap.org/boxes/SENSBOXID/SENSORID
    method: post
    headers:
      Content-Type: application/json
    payload: '{"value": "{{states.sensor.PM10.state }}" }'

Use this in an automation to push the values.

Thanks, you put me on the right track. Iā€™ve got it all implemented right now.

So, assuming that you already have the data available in Home Assistant, one could use the following automation to push:

As you can see, the automation calls for 4 rest_commands. These are defined as follows:

As the rest_commands cannot process !secrets, I used a workaround via input_text:

Secrets explained:

!secret luftdaten_x_sensor: contains Luftdaten sensor ID (typically something like esp8266-123456)
!secret opensensebox_url: contains API endpoint for this sensor box (something like https://api.opensensemap.org/boxes/<<yourSenseBoxID>>/data
3 Likes

I donā€™t know how my minor brain waves caused you to create this whole script, but judging by reading it it entirely fulfills my needs too :smiley:

It was a learning opportunity, I built this sensor together with my son (10) and he wanted to upload the data too. So we made it happen.

And kudos to Frenck for helping me tweaking the scripts too. Had an initial proof-of-concept, but that had a lot of duplicate code. Thanks to his input i was able to simplify the script considerably.

And yes - your brainwave put me on the right track :smiley:

Aha: you got help from the master himself :smiley:

Itā€™s now up and running fine at my end! There were 2 things I had to change in your script:

  1. You multiplied the pressure with 100 before sending it out; I removed that as a pressure of 101540 Pa seems a bit high

  2. I edited the payload for the opensensemap so it reflected my sensors (the part in bold below)

    {
    ā€œ5f2XXXXXXXXXXXXā€: ā€œ{{ states(ā€˜sensor.bme280_temperatureā€™) }}ā€,
    ā€œ5f2XXXXXXXXXXXXXXXXXXā€: ā€œ{{ states(ā€˜sensor.bme280_pressureā€™) | float * 100 }}ā€,
    ā€œ5f2XXXXXXXXXXXXXXXXXXā€: ā€œ{{ states(ā€˜sensor.bme280_humidityā€™) }}ā€,
    ā€œ5f2XXXXXXXXXXXXXXXXXXā€: ā€œ{{ states(ā€˜sensor.particulate_matter_2_5um_concentrationā€™) }}ā€,
    ā€œ5f2XXXXXXXXXXXXXXXXXXā€: ā€œ{{ states(ā€˜sensor.particulate_matter_10_0um_concentrationā€™) }}ā€
    }

EDIT: still figuring out point 1 as your multiplication appears to be correct, but isnā€™t correctly reflected in the Opensensemap dashboardā€¦

1 Like

Yeah, I had to do the multiplication as the value in HA is in hPa, while the APIs are expecting Pa. And you are right, you need to change those values in 2) to your own sensor IDs. Iā€™ll update the file on Github with a note (seems I canā€™t use !secret there?)

edit: fixed it in a new upload, using input_text with !secret now :slight_smile:
Updated my result above.

Made a small write-up: https://hmmbob.tweakblogs.net/blog/18950/push-data-to-luftdaten-and-opensensemap-with-home-assistant

5 Likes

Hi Bob,
I followed your instructions on Tweakblogs. I get all the information in Home Assistant but i canā€™t push it to opensensemap. Here is my error in HA:


Can you please help me?

Regards, Edwin.

Is it persistant? I sometimes have these errors too, and either of these two options is happening:

  1. the data I send is invalid (i.e. one of the sensors is listing ā€œunavailableā€ instead of a normal value)
  2. the API endpoint times out (but this typically doesnā€™t give you the 400 error.

So, have a good look at your sensor values and see if anything is wrong with them.

Edit: if you want you can shoot me the full error in DM - using the same nickname all over the place (here, discord, Tweakers)

Hi, i built the sensor last year and just migrated it from the standard luftdaten firmware to esphome - followed the blog and itā€™s working; however, my SDS updates every couple of seconds as opposed to the 30min I put in the script; any thoughts what might going wrong here?

The dht does update every 5minutes.

sensor:
  - platform: sds011
    pm_2_5:
      name: "Particulate Matter <2.5Āµm Concentration"
      accuracy_decimals: 2
    pm_10_0:
      name: "Particulate Matter <10.0Āµm Concentration"
      accuracy_decimals: 2
    update_interval: 30min
  - platform: dht
    pin: D7
    temperature:
      name: "Temperature"
      accuracy_decimals: 2
    humidity:
      name: "Humidity"
      accuracy_decimals: 2
    update_interval: 5min

apologies, found the solution elsewhere:

https://community.home-assistant.io/t/sds011-not-updating-at-specified-interval/338892/2?u=rt1080

Great, I was just about to reply that I couldnā€™t see anything wrong with your code :rofl:

Can you confirm this is still working?
Iā€™m getting the following error:
2022-07-26 19:51:36 WARNING (MainThread) [homeassistant.components.rest_command] Error. Url: https://api.opensensemap.org/boxes/xxxxxxxxxxxxx/data. Status code 422. Payload: b'{\n "unknown": "2.2",\n "unknown": "9.2",\n "unknown": "6.91",\n "unknown": "100527",\n "unknown": "80.79"\n}'

Verify your sensor IDā€™s (basically, anything that saus unknown here)

1 Like

Cheers!
I just realized, that my input_text: !include_dir_merge_named was misconfigured, and that caused a chain of errors.

1 Like