[Solved]Python scripting: Get temprature and Hum value

Python and python framework

Moderator: leecollings

Post Reply
vleud101
Posts: 3
Joined: Friday 13 January 2017 15:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 15b013c
Location: The Netherlands
Contact:

[Solved]Python scripting: Get temprature and Hum value

Post by vleud101 »

Dear Domoticz forum people,

I have a PLC connected to my raspberry pi and i can successfully send values to the PLC Datablocks with Python scripting (Snap7).

My final goal is to use the PLC to control the heat in the house with several floorheaters and those are controlled by several valves to control each room.
Now i bought 6 temparature sensors (WH-5) to send from each room trough RFXCOMe the data to domoticz on a RPI.

My problem is that I am not a Python scripting hero, so i hope some can help me with the scripting.

Code: Select all

import sys
import datetime
import time
import os
import subprocess
import urllib2
import json
import base64

#-----------------------------------
domoticzserver="192.168.1.xxx:8080"
domoticzusername = "xxxx"
domoticzpassword = "xxxxx"
device = "45"
log_to_file = True
#-----------------------------------

base64string = base64.encodestring('%s:%s' % (domoticzusername, domoticzpassword)).replace('\n', '')
domoticzurl = 'http://'+ domoticzserver +'/json.htm?type=command&param=udevice&idx=' + device +'&nvalue=0&svalue=TEMP;HUM;HUM_STAT'

def log(message):
   print message
   if log_to_file == True:
      logfile = open(sys.argv[0] + '.log', "a")
      logfile.write(message + "\n")
      logfile.close()

def domoticzrequest (url):
  request = urllib2.Request(url)
  request.add_header("Authorization", "Basic %s" % base64string)
  print(request)
  response = urllib2.urlopen(request)
  return response.read()

def domoticzstatus():
   json_object = json.loads(domoticzrequest(domoticzurl))
   getvalue = 0

   # Search the device in JSON list
   if json_object["status"] == "OK":
      getvalue = json_object["result"][0]["Data"]
   else:
      print (datetime.datetime.now().strftime("%H:%M:%S") + "- Error. Could not find device idx in Domoticz response.")

   status = format(float(getvalue[:-1]), '.2f')
   return status

log (datetime.datetime.now().strftime("%H:%M:%S") + "- script started.")
  
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + device + '&nvalue=0&svalue=TEMP;HUM;HUM_STAT')  

Now i know if i run the url at my webbrowser i get:
http://xxxx:[email protected]:8080/js ... M;HUM_STAT

Code: Select all

{
status: "OK",
title: "Update Device"
}
So i know my password and link is OK (Also IDX).

If i run my python scripts i get as output:

Code: Select all

16:03:29- script started.
<urllib2.Request instance at 0x767f1c10>

I want to use the output a variable as float for Temp and seperat for Hum also as float.

So Temp =
and Hum =

So i can use those two variables to send to my PLC.
Last edited by vleud101 on Monday 16 January 2017 20:13, edited 1 time in total.
vleud101
Posts: 3
Joined: Friday 13 January 2017 15:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 15b013c
Location: The Netherlands
Contact:

Re: [Help]Python scripting: Get temprature and Hum value

Post by vleud101 »

Update:

Ok I made a mistake by defining the url. Now it works:

Code: Select all

#---------------Benodigheden------------------
import sys
import datetime
import time
import os
import subprocess
import urllib2
import json
import base64

#---------------Settings---------------------
domoticzserver="192.168.1.210:8080"
domoticzusername = "xxx"
domoticzpassword = "xxxx"
device = "45"
log_to_file = True
#---------------Variabelen--------------------
degrees = "Test"
temp = 0
humi = 0
hum_stat = 0
#---------------Wachtwoordenzooi--------------
base64string = base64.encodestring('%s:%s' % (domoticzusername, domoticzpassword)).replace('\n', '')
#domoticzurl = 'http://'+ domoticzserver +'/json.htm?type=command&param=udevice&idx=' + device +'&nvalue=0&svalue=TEMP;HUM;HUM_STAT'
domoticzurl = 'http://192.168.1.210:8080/json.htm?type=devices&rid=45'

#---------------Logging-----------------------
def log(message):
   print message
   if log_to_file == True:
      logfile = open(sys.argv[0] + '.log', "a")
      logfile.write(message + "\n")
      logfile.close()
#---------------Info ophalen------------------
def domoticzrequest (url):
  request = urllib2.Request(url)
  request.add_header("Authorization", "Basic %s" % base64string)
  print(request)
  response = urllib2.urlopen(request)
  return response.read()
#--------------Status-------------------------

json_object = json.loads(domoticzrequest(domoticzurl))
if json_object["status"] == "OK":
	temp = json_object["result"][0]['Temp']
# getvalue = json_object['result'][0]['Data']
# temp_stat = json_object["result"][0]["TEMP"]
#	hum_stat = json_object["result"][0]["HUM_STAT"]
#return hum_stat
	# #return getvalue
# #return 0
print(temp)


#log (datetime.datetime.now().strftime("%H:%M:%S") + "- script started.")
  
#domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=udevice&idx=" + device + #'&nvalue=0&svalue=TEMP;HUM;HUM_STAT')  

#print(domoticzrequest)

  

I hope some people have some use for it. Most of the time there are only examples to get information IN domoticz and not OUT :mrgreen:
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest