Community
OpenEnergyMonitor

OpenEnergyMonitor Community

Get values from emoncms feeder with python script

Tags: #<Tag:0x00007f1be27dfeb0> #<Tag:0x00007f1be27df898>

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.

Hello,

Sorry for the long reply.

So the feeder that I’m looking give this answer:
{‘time’: 1587153369, ‘value’: ‘224.8’}
So there is the ‘time’ value, when this value change it’s mean that the data as been updated, but in my python script I don’t know how to proceed:

import requests
APIKEY = '5a8fxxxxxxxxxxxxxxxxxxxxxxx6cc6'
url = 'https://emoncms.org/feed/timevalue.json?id=369348&apikey=5a8fxxxxxxxxxxxxxxxxxxxxxxx6cc6'

inc_value = requests.get(url, headers = {'Authorization': 'Bearer 5a8fxxxxxxxxxxxxxxxxxxxxxxx6cc6'})

incoming_value = inc_value.json()
previous_value = incoming_value
while (previous_value['time'] == incoming_value['time']):
        	previous_value = incoming_value
        	incoming_value = inc_value.json()
        	print(incoming_value)

But incoming_data variable stay the same, so how can I correct this?

Many thanks,
Alban

Edit - munged API key. BT, moderator.

Try swapping those 2 lines around so that you save the old value before it is updated to the new value

[Edit]
Actually now I look closer there are some other issues too.

First the api call isn’t in the loop so yes it will only get fresh data once when the script is called.

Also the loop seems to repeat what is done immediately before entering the loop so I don’t think it’s doing what you intended.

1 Like

Oh thanks, I modify the loop like this and it work:
inc_value = requests.get(url, headers = {‘Authorization’: ‘Bearer 5a8fc3b42672113c669318f6a0356cc6’})

inc_value = requests.get(url, headers = {‘Authorization’: ‘Bearer 5a8fc3b42672113c669318f6a0356cc6’})

incoming_value = inc_value.json()
previous_value = incoming_value
while (previous_value[‘time’] == incoming_value[‘time’]):

inc_value = requests.get(url, headers = {‘Authorization’: ‘Bearer 5a8fc3b42672113c669318f6a0356cc6’})

incoming_value = inc_value.json()
previous_value = incoming_value
print(incoming_value)
1 Like