Category Archives: Domotica

Control a StorkAir/Zehnder WHR 930 ventilation unit using mqtt

Our house is equipped with a WHR 930 ventilation system, in Dutch a ‘warmte terugwin systeem (wtw)’ and since we have the ‘basic’ version, we have to control it using switch in the bathroom. There is a RF module available for the WHR 930, but that’s a pretty expensive option and i could not figure out how open it was. Since i want to control it from Home Assistant, it must be open or should have some sort of API.

Searching the internet for possibilities i learned that the WHR 930 has a serial interface on it’s mainboard and that the protocol is fully reverse engineered! The picture below is one of the mainboard, the red arrow points at the serial interface (RJ45)

I modified a UTP cable to get the right pin layout for a Serial<>USB converter and attached it to a Raspberry Pi and wrote some python code to interface with the WHR 930 via the serial connection. The code reads the various temperature values and fan status and publishes the results on a mqtt topic. The python code subscribes to a specific mqtt topic (house/2/attic/wtw/set_ventilation_level) for messages (0, 1, 2 or 3) to set the ventilation level. Level 0 stops the ventilation, i did not even know that the WHR 930 could do that 🙂

For now i am happy, but the serial protocol description shows that there is much more possible. Maybe i will look into that later, but do not hesitate to contact me (see my Github page for contact info) if you found out nice additions!

For integration with Home Assistant, see the README file on my Github channel.

Push Hue switch status on a MQTT topic

Since a few weeks we have a Hue switch which can be used to set the Hue lights on and off and also dim or brighten them. Of course the Hue switch only triggers Hue devices and nothing else. But i wanted to trigger other devices also, like a wifi tp-link switch that’s connected to a normal light  when someone pushes the ‘on’ button. And when the ‘off’ button is pressed, all lights in the livingroom should go off, but also that light that’s connected to the tp-link switch and even the TV, radio and the tp-link switch that switches a few devices behind the tv-set that’s not needed at night.

Sadly the switch does not have a webhook or something like that, but it is possible to get the state of the switch from the Hue bridge. With a simple curl command. we can get the state of our switch (our’s has id 2): curl -s -XGET http://<ip address>/api/<user>/sensors/2

{
 "state": {
 "buttonevent": 1002,
 "lastupdated": "2017-01-22T11:34:05"
 },
 "config": {
 "on": true,
 "battery": 100,
 "reachable": true,
 "pending": []
 },
 "name": "Hue switch",
 "type": "ZLLSwitch",
 "modelid": "RWL021",
 "manufacturername": "Philips",
 "swversion": "5.45.1.16265",
 "uniqueid": "00:aa:bb:cc:00:ff:00:ff-00-ff00"
}

Now it is pretty simple to poll the Hue bridge and when someone pushes a button, send the id of the button on a MQTT topic. I use Home Assistant to catch that event and trigger an action depending on the button pressed. Btw, button 1 is ‘on’, button 2 is ‘brighten, button 3 is dim and button 4 is off.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import json
import requests
import mosquitto

######## CONFIG ########
urlhuebase = "http://<ip address>/api/<user>"
port = 8123
use_ssl = True
sensorid = '2'
pollingtime = 1
########################

def publish_message(msg, mqtt_path):
  mqttc.publish(mqtt_path, msg, 1)

def SensorState(sensorid):
  urlhue = urlhuebase + "/sensors/" + sensorid
  hueresponse = requests.get(urlhue)
  state = hueresponse.json().get('state')
  data = [ state['buttonevent'], state['lastupdated'] ]
  return data

# Connect to the MQTT broker
mqttc = mosquitto.Mosquitto("publish_hue_switch")
mqttc.connect("127.0.0.1")

sensordata = SensorState(sensorid)
lastupdated = sensordata[1]
print "lastupdated: {0}".format(lastupdated)

while True:
  try:
    sensordata = SensorState(sensorid)

    if (sensordata[1] != lastupdated):
      button = str(sensordata[0])[0:1]
      publish_message(msg=button, mqtt_path='house/0/hallway/hueswitch/button_pressed')
      print 'Button pressed: {0}'.format(button)
    else:
      time.sleep(pollingtime)
  except KeyboardInterrupt:
    print 'Exiting on request'
    break

Gas meter update

Yesterday I blogged about getting information from our gas meter with an Arduino that reads the reed switch. After using this setup for many hours, we noticed that our counter was not accurate. My first suspect was the IN-Z31 reed switch, it looked like the switch sometimes stayed closed for a very long time. To get more information, I changed the code so that when the sensor port changes to HIGH (1), I start an other function that checks for how long (in milliseconds) the switch stays closed.

Luckily, soon after changing the code and monitoring the serial (debugging) output, the gas meter stopped exactly at the digit 9 and the CountStateDuration() function showed me that the switch stayed closed. Yesterday we already noticed that we got a pulse every time when the digit 9 was shown, so that the switch stayed closed at 9, was not a very big surprise.

Now we learned that, I modified the code. In the loop() it calls the function CheckSwitch() which returns a 0 if the switch is open (not making contact). If the switch is closed, thus making contact, the function CountStateDuration() is called to check how long the switch is closed. This function returns when the switch is open again, resulting in a counter update.

The LCD screen now shows the real counter, but to get that right, you need to set the current reading before starting the Arduino. The new code can be downloaded here.

arduino_01012013

Reading our gasmeter with an Arduino

In our temporary home(*), we have a gas meter with a IN-Z31 pulse transmitter.I had never seen one before, so after some Googling, I found out that it is a reed switch, generating pulses when an event happens. This model have 2 switches, one for an alarm event, that is triggered when someone tries to tamper the meter and the other one that will be triggered when an amount off gas is used, the ‘Totalizing contact’.

A cable with 4 wires coming from the IN-Z31 was not attached to anything, so we would not brake or interrupt something when we are trying to read information from it. The datasheet (IN-Z61 is a newer version, but working the same) showed me that the inner 2 wires of the 4 are used for the ‘Totalizing contact’, the one we need.

 

gasmeter

 

Two months ago I bought an Arduino Uno, because in our new home we want to do some domotica. Since we have the Arduino, our son Sven (14) had a lot off air time with it, so he helped me a lot to attach the IN-Z31 to the Arduino. Mostly, he build the board and I did the logic.

Board setup

The setup on the board started simple, connect the 5v from the Arduino to one wire and connect the minus of the Arduino to the other wire. Between the wire from the IN-Z31 and the minus, we put a 10K resistor and behind the resistor we attach a wire to port 6 on the Arduino. Port 6 is a digital port, that means we are going to get a 0 (LOW) or a 1 (HIGH) from it. The part of the switch we are reading is normally open and will be close (making contact) when generating the pulse.

The datasheet mentioned that it will be 250ms closed. After connecting the IN-Z31 to the Arduino, we noticed that it triggered an event at every 0,001 m3. But after a lot of fiddling with the timing, we could not get a counter that was exact keeping up with the counters on the meter self. Eventually we decided not to measure the 0,001 m3, but 1 step further, so we count every 0,010 m3. What we did is the following, when we see an event from the reed switch, we append 1 to our counter and wait for 3500 ms (3.5 seconds). Why 3500?, 2500 (10 times 250 ms) was to low (the Arduino is so fast that we sometimes counted 2 extra loops in that 2500 ms ) and 3500 turned out to be a very good number 🙂

arduino

The code

After getting stable counters, Sven attached and LCD screen to the Arduino. That way we can leave it in the cabinet where the meter is, without a laptop attached to it, and still read out the information we want. When that worked, Sven did not liked it that the LCD was always on, so he also put a button switch between the LCD screen and the 5v, so that you need to press the button to read the screen, very nice!. The LCD part added some code and cables, but the base is very simple… You can download the code here

The future

Soon a Raspberry PI will arrive, then we will try to get the counters somewhere on the internet

*)  We sold our home and are building a new one, so we now are renting a house on a holiday park in the neighbourhood of our new house