set timeout for urllib.urlopen

Posted on
Wed Oct 14, 2020 1:47 pm
Hans Kiesewetter offline
Posts: 8
Joined: Nov 19, 2014

set timeout for urllib.urlopen

I have several energy meters, (connected with my LAN) which are not supported by Indigo. However with a python script I'm able to retrieve the values and store these in variables for further use (graphs, reports)
The script is included as "Embedded Python" in a Schedule that runs every minute. Sometimes I see a time-out warning in my log file.
I want to move the python script to an external file, but have to make it robust to avoid a crash off the computer if the connection cannot be made.

Here is (a part of) the script (Total script is much longer, but my problem is in this part)
----
#! /usr/bin/python
# --------------------------------------------------
# PREPARATION & SETTINGS
# --------------------------------------------------
import urllib2
import urllib
import base64
import json
import sys

# --------------------------------------------------
# READING YOULESS PV panels
# --------------------------------------------------

URL = 'http://192.168.178.71/a'
response = urllib.urlopen(URL).read()

# typical output consists of four lines
#
# 1195,497 kWh
# 93 Watt
# 123
# OK (260)

lines = response.split("\n") # split the 4 lines into elements
lines[0]=lines[0].lstrip(" ")
Watt_zon = lines[1].split(" ")[0]
kWhText= lines[0].split(" ")[0]
kWhdeel=kWhText.split(",")
kWhzon=kWhdeel[0] + "." + kWhdeel[1]

indigo.variable.updateValue("kWh_zon" , value = kWhzon)
indigo.variable.updateValue("Watt_zon" , value = Watt_zon)

# --------------------------------------------------
# last line off script
# --------------------------------------------------

Question: How to force a timeout in this script, if the connection with the meter fails?
response = urllib.urlopen(URL, timeout=1).read() does not work.

PS: the non-supported meters are a Youless ( https://www.youless.nl/downloads-ls110.html) and a Beeclear (https://beeclear.nl) (Sorry, both sites in Dutch :wink: )

Posted on
Wed Oct 14, 2020 2:57 pm
jay (support) offline
Site Admin
User avatar
Posts: 16379
Joined: Mar 19, 2008
Location: Austin, Texas

Re: set timeout for urllib.urlopen

I'd recommend using the requests library instead (which Indigo includes), it's just easier and more reliable (IMO):

Code: Select all
import requests
from requests.exceptions import Timeout

URL = 'http://192.168.178.71/a'
try:
    reply = requests.get(URL, timeout=1) # a one second timeout is probably too small, but that's up to you
    # your code here using the reply's text property
    lines = reply.text.split("\n") # split the 4 lines into elements
    lines[0]=lines[0].lstrip(" ")
    Watt_zon = lines[1].split(" ")[0]
    kWhText= lines[0].split(" ")[0]
    kWhdeel=kWhText.split(",")
    kWhzon=kWhdeel[0] + "." + kWhdeel[1]

    indigo.variable.updateValue("kWh_zon" , value = kWhzon)
    indigo.variable.updateValue("Watt_zon" , value = Watt_zon)
except Timeout:
    indigo.server.log("Request timed out")


Not tested of course, but it should be close.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Oct 15, 2020 11:59 am
Hans Kiesewetter offline
Posts: 8
Joined: Nov 19, 2014

Re: set timeout for urllib.urlopen

Thanks Jay,

This worked.
(including a nice warning in the log when I unplug the YouLess meter.)

:)

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests