Script that ask when a feed/input was last updated

since we dont have the feedtime widgit on emoncms.org i wonder if a python script could be hammered up that would ask when a given feed/input was last updated and if more than 5 mins ago i could use that to turn on a LED to signal that something is going on

https://emoncms.org/feed/timevalue.json?id=

gives me: {“time”:1522144019,“value”:“22”}

what time format is that? unix of some sorts?

It’s JSON and the timestamp is Unix (seconds since 00:00 01/01/1970)

The Python time module uses the same format so time.time() - 1522144019 = seconds since last update.

Look at the Python json module to decode the reply into a Python dict which you can the access the values using their names eg reply[‘time’] and reply[‘value’].

oki, will be on it

this is just an alternative to not having the feedtime widget :slight_smile:

first rough cut:

#!/usr/bin/env python

try:
    # For Python 3.0 and later
    from urllib.request import urlopen
except ImportError:
    # Fall back to Python 2's urllib2
    from urllib2 import urlopen

import json
import time

#this one is for the wiringpi lib

import wiringpi

# wiringpi numbers

wiringpi.wiringPiSetup()
wiringpi.pinMode(0, 1)  # sets pin 0 to output (GPIO 17, Actual hardware pin number is 11) (Relay)



def get_jsonparsed_data(url):
    """
    Receive the content of ``url``, parse it as JSON and return the object.

    Parameters
    ----------
    url : str

    Returns
    -------
    dict
    """
    response = urlopen(url)
    data = response.read().decode("utf-8")
    return json.loads(data)


url = ("https://emoncms.org/feed/timevalue.json?id=xxxx")


while True:


    dif = time.time() - (get_jsonparsed_data(url))['time']
    print dif
    if dif  >  60:
        print 'Last update is more than 5 mins ago'
        wiringpi.digitalWrite(0, 1) # sets port 0 to ON
    else:
        print 'Last update is less than 5 mins ago'
        wiringpi.digitalWrite(0, 0) # sets port 0 to OFF

    time.sleep(10)