DAL

From Domoticz
Jump to: navigation, search

This wiki page descript how to retrieve data from the netduino-DAL [1] to Domoticz using Python. The DAL itself does have a nice UI, but off course we want everything on one place (Domoticz)

The values can be requested by the command : http://IP:port/requestData!allContentFields! This will give you a comma separated line with values. Nice to know that http://IP:port/requestData!allContentNames! will give you all the fieldnames.

HowTo Create 4 dummy devices (1 Gas / 3 Energy) Change the Settings in the script - IP:port of the domoticz and netduino and the idx of the devices.

Because I want to update the values every 20 seconds (and the crontab only runs every 1 minute) I created a loop. (So, if you think ones every minute is enough, remove the loop)

Do not forget to edit you crontab

#!/usr/bin/pytho
import time
from urllib2 import urlopen

# change these values
netduino = "http://IP:Port"  
domoticz = "http://IP:8080"
idx_gas = #
idx_SolarPanel = #
idx_CurrentElekctraConsumption = #
idx_ConsumptionReal = #
# End 

uur=int(time.strftime("%H", time.localtime()))
min=int(time.strftime("%M", time.localtime()))

for x in range (0, 3):

  response = urlopen(netduino+'/requestData!allContentFields!')
  html=response.read()
  values=html.split(',')

 #                  Solar Generate   + Consumed         -  Solar Delivered  
  ConsumptionReal = float(values[14]) + float(values[5]) - float(values[4])
  Solar = 1000 * float(values[14])
  ElectraConsum = 1000 * float(values[5])

  # quick&dirty fix for time-problem between the 2 devices
  if uur==0 and min<7:
    ConsumptionReal=0
    Solar=0
    ElectraConsum=0

  if uur==23 and min>58:
    ConsumptionReal=0
    Solar=0
    ElectraConsum=0


  # Gas Total
  response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_gas)+'&svalue='+str(1000*float(values[12])))
  response.read()
	
  # Solar Panels (Watt)
  response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_SolarPanel)+'&nvalue=0&svalue='+values[13]+';'+str(Solar))
  response.read()

  # Current Electra Consumption 
  response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_CurrentElekctraConsumption)+'&nvalue=0&svalue='+values[1]+';'+str(ElectraConsum))
  response.read()

  # Consumption Real
  response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_ConsumptionReal)+'&nvalue=0&svalue=0;'+str(1000*float(ConsumptionReal)))
  response.read()
  
  time.sleep(15)


print('end')

If you need more info please contact MarFan on the tweakers forum