Community
OpenEnergyMonitor

Community

Get values from emoncms feeder with python script

Tags: #<Tag:0x00007fc3408c31c8> #<Tag:0x00007fc3408c2e08>

Hello everyone,

I’m a master student and I’m working on a project using emoncms.org.

I have multiples feeder that monitor energy from a lab of my university for example. It is perfectly set up and every ~20 second I have a new value.

I need to write a python script that get the value from the website API.

So I tried some stuff but it’s not working:
import requests
incoming_data = requests.get(“https://emoncms.org/feed/value.json?id=369348”)
print(incoming_data)
parsed_data = incoming_data.json()
print(parsed_data)

incoming data return:
<Response [406]>
and parsed_data return:
URI not acceptable. No controller 'feed'. (value/)

Can someone help me? Thanks in advance.
Alban

Doesn’t look like you are authenticating the request with an emoncms.org API key.

From https://emoncms.org/site/api#feed:

Authentication

If you want to call any of the following actions when you’re not logged in you can authenticate with an API key:

  • Append on the URL of your request: &apikey=APIKEY
  • Use POST parameter: “apikey=APIKEY”
  • Add the HTTP header: “Authorization: Bearer APIKEY”

API keys

There are two types of api key the write apikey and read apikey giving read & write access or read-only access. Login or create an account to obtain these keys.

Yes, I managed to do it thank you for your answer :slight_smile:

I have a new question now, so I can read the value from the website with my python script.

So basically, I need get this value and wait for the next update, but I would like to do it without using a timer in my python script.

Is there a method that can ensure me that I get the value each time the value is updated?

Thanks

@AlbanGimli

Using a programatical loop you can read (via API) the [Interval] time for a given feed and and test that against the [current] system time. This will tell you when the data has been updated (and will also inform when the feed update is late for use as a timing alarm.)

Example API call for all feeds: http://emonpi.local/emoncms/feed/list.json&apikey=YOUR_APIKEY

I think another approach might be to subscribe to the values via MQTT rather than using the web API.

Hello @djh

I don’t want to sidetrack the current post but, Is it possible to read the feed update interval via mqtt? I use the API for monitoring feeds that miss updates, but mqtt might be more efficient.

Sorry, dunno.

I don’t think so but you can via the API IIRC.