Simple thermostat in python

For heating/cooling related questions in Domoticz

Moderator: leecollings

Post Reply
HvdW
Posts: 615
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Simple thermostat in python

Post by HvdW »

Hi,
At home we have a 20 year old Nefit Ecomline HR since 1999.
A few weeks ago it had the annual checkup and the technician convinced us not to have a new boiler installed and continue using this reliable boiler.
Problem is that it isn't smart.
That is to say, it is connected to a modulating thermostat, but I cannot influence the thermostat while being away.

What I want is simple:
Being away from home the thermostat has to be set at 12 or 14 degrees and before getting home it has to make the house comfortable.
Up till recently we let the heating on when not too long away and in case we were away for more than a day we came home in a cold house.

In comes Domoticz!
First experiment was Smart Virtual Thermostat from @Logread.
This is the easy one. Installed with a mouseclick easy handling.
Problem however (in my case) is that Smart Virtual Thermostat isn't smart.
It ignites the boiler and keeps on heating until the desired temperature is reached, only tempered by the max water temperature that I have set to 60 degrees on the Nefit Ecomline HR. It acts as an on/off thermostat.
Maybe I don't understand something there.

Second thing I encountered was the voyage undertaken by @domosapiens with his Nefit Ecomline HR.
It learned me not to be afraid to connect a relay to my boiler and it learned me to build in some safetey with a delay-switch-turn-off-module.
Great.

Next step was LUA and the discovery of dzVents. Great work from @dannybloe. Watch his dzVents wiki and some examples.
I used his script as can be found on GitHub, but that didn't work. This post cleared things up for me, to be precise @dannybloe's script in that thread.
The script runs fine, however I encountered another problem.
Domoticz is polling the thermometer every 45 seconds. So the average is very soon equal to the room temperature and then the smartness of the script disappears. I wasn't able to change the script to wait some 10 minutes before the next execution.

So I reverted to python.
What I had discovered on Domoticz was Virtual Switches and Thermostat setpoint. Adding the information from a thermometer it was straightforward to write a python script.
Of course; I can set setpoints anytime, anywhere, start heating the house from the other side of the world. That could be achieved with the formentioned solutions as well.
New in this script is the time.sleep() function.
When the radiators are getting hot, the rise of the room temperature will be lagging a while.
What this script does is just wait, poll the temperature after 5 or 10 minutes and decide what it has to do next.
Instead of burning the temperature up all the way and overshooting it gives the same result as if a smart calculating procedure has been applied.
The result is less gas consumption and that is what I made this script for, besides long distance switching of course.

Code: Select all

# experiment to turn a Nefit Ecomline HR into a smart boiler
# acessories needed:
# raspberry pi running Domoticz and python 2.7
# an ESP8266 (mine is flashed with ESPEasy)
# connect ESPEasy with a cheap relay, in this script connected on D1 (GPIO5)
# the relay needs 5V
# the command inside Domoticz is http://<IP from ESP8266>/control?cmd=GPIO,5,0
# a thermometer wifi or 433 mHz

import urllib, json, datetime, time

# for the logfile:
now = datetime.datetime.now()
#print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))

#define a function to squeeze a number from a string
def get_num(x):
   return int(''.join(ele for ele in x if ele.isdigit()))

# ask current room temperature from domoticz
url = "http://192.168.2.1:8080/json.htm?type=devices&rid=3"
response = urllib.urlopen(url)
str = json.loads(response.read())
for i in str["result"]:
  datastring = i["Data"]
  temperature = float(datastring[0:5])
  print "temperature = ",temperature
# ask current thermostat setpoint from domoticz
url = "http://192.168.2.1:8080/json.htm?type=devices&rid=184"
response = urllib.urlopen(url)
str = json.loads(response.read())
for i in str["result"]:
  datastring = i["Data"]
  setpoint = float(datastring[0:5])
  print "setpoint    = ", setpoint
  print "setpoint - temperature = : ", setpoint-temperature

if temperature >= setpoint:
  # relax, don't do anything
  print "roomtemperature higher than setpoint"
  # switch boiler off to be certain that the boiler is switched off, it could have been manually activated
  urllib.urlopen("http://192.168.2.1:8080/json.htm?type=command&param=switchlight&idx=158&switchcmd=Off")

if setpoint-temperature < 1.0 and setpoint-temperature >= 0:
  # switch boiler on
  print  "boiler will be switched on during 5 minutes"
  urllib.urlopen("http://192.168.2.1:8080/json.htm?type=command&param=switchlight&idx=158&switchcmd=On")
  time.sleep(300)  # wait during 5 miutes heating
  # switch boiler off
  urllib.urlopen("http://192.168.2.1:8080/json.htm?type=command&param=switchlight&idx=158&switchcmd=Off")

if setpoint-temperature >= 1.0 and setpoint-temperature >= 0:
  # switch boiler on
  print "boiler will be switched on during 10 minutes"
  urllib.urlopen("http://192.168.2.1:8080/json.htm?type=command&param=switchlight&idx=158&switchcmd=On")
  time.sleep(600)  # wait during 10 minutes heating
  # switch boiler off
  urllib.urlopen("http://192.168.2.1:8080/json.htm?type=command&param=switchlight&idx=158&switchcmd=Off")

# The program is being activated every 15 minutes with a crontab command
# a log file is maintained in the /home/pi directory
# */15 * * * * python /home/pi/test/simple-thermostat.py >> /home/pi/heating.log 2>&1
# for testing purposes you can run the script every 1 minute like
# */1 * * * * python /home/pi/test/simple-thermostat.py >> /home/pi/heating.log 2>&1
BTW I didn't fiddle with hysteresis. Not necessary. The temperature will be reached anyway.
What many people forget is that temperature is a relative value. The thermometers we use in our houses are not at all accurate.
Human beings adapt easily to any gauge.
So in my house 19 may be comfortable and in your house 21 is. And maybe we have about the same room temperature.

ENJOY!
Attachments
Domoticz for Android
Domoticz for Android
Screenshot_20190518-220141.png (116.63 KiB) Viewed 3597 times
Domoticz for Android
Domoticz for Android
Screenshot_20190518-220105.png (140.79 KiB) Viewed 3597 times
Bugs bug me.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest