Search | Sailfish OS | Running | PineTime | All Posts

Domoticz and Velux KLF-200

May 14, 2021 — Nico Cartron

Last article in this series: let's integrate our Velux KLF-200 with Domoticz!


If you've followed my last article explaining how to add your roller shutters in a Velux KLF-200 io-homecontrol gateway, you've seen that it comes with an API, meaning we can control it from an home automation system - Domoticz in my case.

Initial thoughts

I first wanted to implement basic API calls to open/close my roller shutters, but when I looked at the Velux API, I realised that I'd better find existing implementations (e.g. on GitHub), since Velux provides you with the ability to interact with the KLF-200, but you have to implement a lot of things by yourself - basically, it's pretty low level, see below:

Finding alternatives

A quick search on GitHub on the keyword "KLF-200" gave me a few results:

Both seemed interesting, so I started playing a bit with them, but in parallel I looked at the Domoticz forums to see whether some folks had the same requirements as me, and indeed that was the case!

So what do we use?

The post in question is from French user "ProfBoc75" and available on the French Domoticz forum, but he also made a post in English on the Domoticz Forum.

What do we want to achieve?

We want to be able to fully control our roller shutters, i.e.:

  • use Domoticz to close and open them,
  • but also get their status displayed if we use the physical remote controller to open/close them.

Approach and Components

The idea is to use a component in between Domoticz and the Velux KLF-200 to get status from one side and send it to the other side, e.g.:

  • we close one roller shutter with its remote controller:
    • this is read from the KLF-200 API,
    • then the roller shutter's status is updated in Domoticz,
  • or we use Domoticz to open a roller shutter:
    • this is read from Domoticz,
    • then the command is sent to the KLF-200 API, which closes it.

We will need three components:

  1. a MQTT Broker: this is a lightweight messaging protocol with a subscribe/publish model, ideal for IoT and to make devices communicate with each other (in our case Domoticz and the KLF-200).
  2. Node-RED: a browser-based editor to wire together hardware devices and APIs.
  3. node-red-contrib-velux: specific Node-RED module for Velux KLF-200

Setting that up!

ProfBoc75's post is super well detailed, so I'm just listing the main steps below:

Prerequisites

  1. Install and setup a MQTT broker
    • I am using Mosquitto
    • Explanations on how to set it up in Domoticz are here
    • You can install Mosquitto on the same server as Domoticz, or on a dedicated one - I have installed it on a dedicated Raspberry Pi
  2. Install Node-RED
    • Very straightforward
    • Multiple platforms are supported, see here - e.g. RPi, Docker, Linux, ...
    • I have installed it on the same RPi as Mosquitto, following this doc
  3. Install node-red-contrib-velux using the CLI
    • You must install version 0.0.4 with the below command, as using the node-red CLI installs version 0.0.3, which doesn't work
    • sudo node-red-stop
      cd ~/.node-red/
      npm install node-red-contrib-velux@0.0.4
      sudo node-red-start
  4. Of course the KLF-200 must be setup

Domoticz part

  • Create a "dummy hardware" in Setup > Hardware
  • Then create as many "virtual sensors" as roller shutters you have in your KLF-200
    • Sensor type is "Switch"
  • Then edit it the newly creates devices in "Switches" and set the Switch Type to "Blinds Percentage":

Node-RED: KLF-200 to Domoticz

We are creating a Node-RED flow to read status from the KLF-200, and update the switches (roller shutters) in Domoticz.

  • In Node-RED UI (http://YOUR IP:1880), import this code
    • using the "burger menu" from top right, then Import:
    • Paste the code in the pink part, and:
    • Put your KLF-200 IP address in the line "host": "192.168.xx.xx",
    • and the KLF-200 WiFi password (which you can find on the KLF-200 itself) in the line "password": "YourWifiPassword",
    • You can check the MQTT configuration in the purple "domoticz/in" icon, then click on the pencil button on the "Server : MQTTBroker" line:
    • And the KLF-200 settings can be checked in the "Velux Nodes" blue icon, then pencil next to "Datasource"
    • Then click on the red "Deploy" icon at the top right
    • The "domoticz/in" purple icon should show a green "connected" dot
  • Edit the "UpdatePosition" and update the index numbers (IDX) with the ones from Domoticz (which you get from the Switches properties).
    • You get the Velux nodeID from this URL: http://YOUR IP:1880/velux/nodes
    • This should return something like:
      [{"id":3,"name":"Velux1","order":1},{"id":2,"name":"Velux2","order":0}]
    • Make the connection between your Domoticz Switch IDX and the Velux nodeID:
  • Deploy the flow again (with the red "Deploy" button top right of the screen).
  • You can now check that it works by closing a roller shutter with your remote, and this should be updated in Domoticz:
    =>

All done, now Domoticz knows the status of our roller shutters and displays them.

Let's do the other way around, i.e. having Domoticz control those shutters!

Node-RED: Domoticz to KLF-200

  • Still from the Node-RED UI, import this code
    into the current flow.
    • Select "import copy" when a pop-up appears, since few nodes already exist
    • Check the "MQTTBroker" and "VeluxAPI" - same as in previous step
  • Edit the "VeluxMsg" green-ish icon:
    • /!\ The name must match the name that you defined in your KLF-200 configuration /!\
    • If you have more devices to add, just use the Node-RED UI:
    • Add an item in "VeluxMsg" pointing to the next number (e.g. 5 if the last one was 4)
    • Add a "Change" and fill in the correct Velux nodeID, and connect it to "VeluxMsg" on one side, and to "BougePosition" on the other side
  • Deploy the flow
  • You should now be able to control your shutters from Domoticz

Troubleshooting

  • The Node-RED logs are super useful, and can be seen in the UI by clicking on the "bug" icon:
  • Be sure to use the Name of your Velux node when updating the "VeluxMsg" part.
  • The Domoticz index number needs of course to be correct for the reading shutters state from Domoticz part.

Wrap-up

Many thanks to ProfBoc75 for his work, this post is just a more detailed walk-through.

I hope you found it useful, and as usual, hit me up on Twitter for any question/feedback!


Tags: Automation


I don't have any commenting system, but email me (nicolas at ncartron dot org) your comments!
If you like my work, you can buy me a coffee!