Zehnder ComfoAir CA350 integration via serial connection (RS232) and MQTT

Hello,

I’m migrating my devices from Jeedom to HA (so quite new in HA), but i’m facing some issues to integrate this.

My HA is on a HA OS on a ESXI VM, and my Zehnder WHR 930 (which normally connects like the CA 350) has a Raspberry Pi connected via the RS232 / Usb, which is actually working fine on Jeedom:
image

So here are the steps followed regarding the instructions (connected in SSH to the raspberry):

  1. The following packages are needed:
    sudo pkg install python3-pip python3-yaml
    ==> I had to use: sudo apt install python3-pip python3-yaml
  2. Create directory for the application /usr/local/share/ca350/bin/ and copy src/ca350 script to it
    ==>
sudo mkdir /usr/local/share/mytmp
sudo mkdir /usr/local/share/ca350
sudo git clone https://github.com/adorobis/hacomfoairmqtt/ /usr/local/share/mytmp/
sudo mkdir /usr/local/share/ca350/bin
sudo cp /usr/local/share/mytmp/src/ca350 /usr/local/share/ca350/bin/
  1. Update the script as required (serial port and MQTT server mainly)
    ==>
    sudo nano /usr/local/share/ca350/bin/ca350
    Then i changed the server (HA ip adress), user, password and serial port (which are in my mosquitto broker in HA)
  2. Create virtual environment:
    python3 -m venv /usr/local/share/ca350/bin/
    ==>
    sudo python3 -m venv /usr/local/share/ca350/
  3. install packages in the venv:
source /usr/local/share/ca350/bin/activate.csh
sudo pip3 install paho-mqtt pyserial
deactivate

==> Here is where i’m blocked:

pi@RPI-VMC:/usr/local/share/ca350 $ source /usr/local/share/ca350/bin/activate.csh
-bash: alias: deactivate : non trouvé
-bash: alias: « test $?_OLD_VIRTUAL_PATH ! » : nom d’alias non valable
-bash: deactivate : commande introuvable
-bash: setenv : commande introuvable
-bash: setenv : commande introuvable
-bash: /usr/local/share/ca350/bin/activate.csh: ligne 38: erreur de syntaxe : fin de fichier prématurée

Did i missed a step for this venv part?

Sorry if theses are beginner questions :).

Have a nice day,
Dapolux.

From what I can see you have created the venv in the /usr/local/share/ca350/ directory but you are trying to activate it in the /usr/local/share/ca350/bin one. I’m not an expert but the venv directory should be the one where the binaries are so rather in the bin subfolder. So try to create the venv in it again:
sudo python3 -m venv /usr/local/share/ca350/bin/
activate it with
source /usr/local/share/ca350/bin/activate.csh
(or without the .csh - not sure which option should work for linux) and install the required components there.

Hi :slightly_smiling_face:
as you have an raspberry, you probably have raspian or ubuntu installed? With Ubuntu 20.04 following should work (other Debian/Raspberry/Ubuntu versions should be close to that) :

sudo apt-get install python3-pip python3-yaml python3-serial
sudo pip3 install paho-mqtt python-etcd

Please keep in mind, that the first 36 Lines are configuration. You will need to find the right Serial Port, mine is for example /dev/ttyUSB0.

Test try after that should work in your case with:

sudo python3.8 /usr/local/share/ca350/bin/ca350

As soon as everything works, you could create a service (which you can enable to start automatically on boot).

/etc/systemd/system/comfoair2mqtt.service

[Unit]
Description=comfoair2mqtt
After=network.target

[Service]
ExecStart=python3.8 ca350
WorkingDirectory=/opt/comfoair
StandardOutput=inherit
StandardError=inherit
Restart=always
User=root

[Install]
WantedBy=multi-user.target

Keep in mind to change to your user and your workingdirectory.

You can start the Service with: systemctl start comfoair2mqtt
You can look at the current status with: systemctl status comfoair2mqtt
If everything works, you can enable it for boot: systemctl enable comfoair2mqtt

I hope this works for you. @adorbis maybe we should extend this information and classify the environment for the current install instruction.

Best,
Tim

1 Like

Yes, definitely! Currently it is probably not clear enough that the installation instructions are TrueNAS (FreeBSD) specific.
Btw, in linux do you typically run it in a venv or just straight in the system? Actually my knowledge about python, virtual environments etc is quite limited…

I am more familar with other programming languages as well and usually only an python user. I for one scanned what you did and tried to make it fit in my environment. This means i am not sure, if “you do it normally” - but you definitely don’t have to use it.

Thanks to both for your help.

Yes my serial port is also /dev/ttyUSB0.

This helped alot, i just had to adjust sudo python3.8 ==> sudo python

Next step, the service, but i’m retrieving the data!

For information, i’m getting some warnings in the ssh:

19-01-2021 20:36:06 WARNING: function get_fan_status could not get serial data
19-01-2021 20:37:04 WARNING: get_temp function could not get serial data
19-01-2021 20:37:06 WARNING: get_ventilation_status function could not get serial data
19-01-2021 20:40:04 WARNING: function get_fan_status could not get serial data
19-01-2021 20:40:06 WARNING: function get_bypass_status could not get serial data
19-01-2021 20:41:04 WARNING: get_ventilation_status function could not get serial data

but i’m retrieving the data correctly, anyway.

1 Like

That’s great! I am glad that it worked :slightly_smiling_face:

yeah the comfoair serial protocol seems to be buggy some times - at least I had similar errors/warnings in openhab as well :-/ Also if you have an active controlling on the comfoair - parallel to your raspberry, it sometimes causes problems with the data. This is why I activated the PC Mode.

Have fun :slightly_smiling_face:

Same here, looks like the comfoair rs232 implementation is not the best. I’ve observed also with the original controller that it was taking some time for successful readings/controls.

So now i’m in the step to use the custom card.

I don’t know if i followed the correct way, but as the entity is not yet “discoverable” ( This is still a work in progress), i guess i have to create it manually.

To do so, i added in the configuration.yaml, the following:

climate:
  - platform: mqtt
    name: ca350_fan
    send_if_off: true

And i’ve installed the custom card “lovelace-comfoair”, added a card in the dashboard, for which ones i guess i’m close to get it working:
image

  • Fan speed (status + command) are not working, should I specifiy somewhere (in the climate declaration?) a mapping to the “fan” entity?
  • Which temperature is supposed to be the ones in the top middle? (Should i map it?)
  • For the buttons / infos in the bottom, i guess they are for the bypass/filter/summermode indicator, should the other 2 ones do something?
    ==> Edit: I found it in the code, it represents the binaries ca350_fan_speed_supply, ca350_filterstatus, ca350_bypass_valve (for the valve + preheater), and ca350_summer_mode (grey color is close to the black :slight_smile: )

Hi,
did you activate the climate Option in hacomfoairmqtt ?

HAEnableAutoDiscoveryClimate = True

If you activate it, everything should be automatically configured. With the Widget I still have some open topics:

  • Make active elements more visible
  • Make the active Fan more visible
  • Control the Temperature

Ah great, it works better with this parameter. (I was thinking that it should not be used because it was “in progress”). Thank you! (i’ve set this parameter to true, and removed the entry in configuration.yaml.)

For the visibility, what i can say for the “fan” ones, when i clic on a fan, the visibility on this one is good:
image

But to get the updated value in the screen, i have to refresh the page, so the “grey circle” disapears (any option to activate to get a refresh directly in the screen without refreshing the entired page?).

Hmm it seems that i have an bug in the widget. There is an timing issue in the service (read in interval and write instantly) this is why i never seem to stumble on it. I thought i was just not patient enough :laughing:

I will try to take a look in the coming days.

1 Like

Hello,

As this was my first custom integration in HA, i’ve written the different steps which i have followed, and some explanations (based of course on the previous posts + documentation). If it’s too much “beginner oriented” don’t hesitate to tell me, i can remove this post, otherwise i hope i can help some other beginners like me. Don’t hesitate if a modification is required.

As a prerequesite, you need to have a MQTT broker installed in your HA.

Architechture
First of all, a quick overview on the different physical devices and connections:


Note: The Raspberry has a raspbian installation.

Hacomfoairmqtt installation

  • Connect to the raspberry via SSH
  • Install the required packages:
sudo apt-get install python3-pip python3-yaml python3-serial
sudo pip3 install paho-mqtt python-etcd
  • Copy the hacomfoairmqtt files to the raspberry
sudo mkdir /usr/local/share/mytmp
sudo mkdir /usr/local/share/ca350
sudo git clone https://github.com/adorobis/hacomfoairmqtt/ /usr/local/share/mytmp/
sudo mkdir /usr/local/share/ca350/bin
sudo cp /usr/local/share/mytmp/src/ca350 /usr/local/share/ca350/bin/
  • Edit ca350 script with your personal configuration, so in my case:

sudo nano /usr/local/share/ca350/bin/ca350

  • Change the following entries (to be adjusted with your personnal data):
SerialPort='/dev/ttyUSB0' --> To be adjusted with your Serail port
MQTTServer='192.168.1.XXX' --> Ip address of my HA (which has the MQTT broker)
MQTTUser = ‘myuser’ --> User set in my MQTT broker settings
MQTTPassword = ‘mypassword’ --> Password set un my MQTT broker settings
HAEnableAutoDiscoveryClimate = True --> To publish the climate entity
  • Ctrl + o so save
  • Ctrl + x to exit

At this stage, launching the following command, the raspberry starts to publish the data:
sudo python3 /usr/local/share/ca350/bin/ca350

Last step on the raspberry, create a service to enable the automatic start at each boot.

  • Create the file:

sudo nano /etc/systemd/system/comfoair2mqtt.service

  • Fill it with this content (you can adjust the user if you prefer to use another user):
[Unit]
Description=comfoair2mqtt
After=network.target

[Service]
ExecStart=python3 ca350
WorkingDirectory=/usr/local/share/ca350/bin/
StandardOutput=inherit
StandardError=inherit
Restart=always
User =root

[Install]
WantedBy=multi-user .target
  • Ctrl + o to save
  • Ctrl + x to exit
  • Start the service:

systemctl start comfoair2mqtt

  • And enable it for automatic boot

systemctl enable comfoair2mqtt

At this stage, the raspberry will publish automatically (even after a reboot), the different entities are now available in HA with a “ca350” name (climate.ca350_climate, fan.ca350_fan, sensor.ca350_outsidetemp …), so they can be used, example:
image

Lovelace MQTT Comfoair card installation

Now let’s get a better view for HA display.

Let’s download comfoair-card.js (https://github.com/TimWeyand/lovelace-comfoair/blob/beeddb434ece08b3309784bb7538ede491245c10/comfoair-card.js), and store it under your www folder, in HA.
image

Now add this file as a resource for lovelace:

Configuration -> Lovelace customizing -> Resources tab -> ‘+ add resource’

Fill “url” with which ones you added the file in the step before, changing “www” in “local”, so in my example, I stored the file in /www/lovelace-comfoair, so I put /local/lovelace-comfoair/comfoair-card.js. Resource type is Javascript module.
image

At this moment, a new card is available for your dashboards.
Now go on your desired dashboard, add a card, and a new customized card will be available:
image

The link to every entity is already done by default (with the “ca350” names), so you should not modify anything:

Now let’s enjoy your new card to manage your MVHR.

Thanks again to @adorobis and @TimWeyand for your support and for the work done!

8 Likes

Looks great! I’m so happy that my initial work is so much better now thanks to @TimWeyand and your guide @Dapolux deserves a dedicate page in the Community Guides section of the forum.

1 Like

For anyone trying to replicate this, I will reply to this thread with things that were different on my machine (a normal Debian installation):

  • There seems to be a config file now, so instead of editing the ca350 file in /bin, do the following:
sudo cp /usr/local/share/ca350/config.ini.dist /usr/local/share/ca350/bin/config.ini
  • Then edit the config.ini file instead

So the MQTT stuff is working, I am seeing values under /comfoair and I am seeing a config entry under
/homeassistant/sensor/ca_outsidetemp.

However, the device and sensors are not autocreated in Homeassistant.
What am I doing wrong?

Here is what the script is outputting:

*****************************
* CA350 MQTT Home Assistant *
*****************************

01-02-2021 13:24:18 INFO: Changed RS232 mode to 3
01-02-2021 13:24:18 INFO: Home Assistant MQTT Autodiscovery Topic Set: homeassistant/fan/ca350_fan/config
01-02-2021 13:24:18 INFO: Home Assistant MQTT Autodiscovery Topic Set: homeassistant/sensor/ca350_[nametemp]/config
01-02-2021 13:24:19 INFO: Home Assistant MQTT Autodiscovery Topic Set: homeassistant/climate/ca350_climate/config
01-02-2021 13:24:20 INFO: Successfull subscribed to the comfoair/on/set topic
01-02-2021 13:24:20 INFO: Successfull subscribed to the comfoair/speed/set topic
01-02-2021 13:24:20 INFO: Successfull subscribed to the comfoair/comforttemp/set topic
01-02-2021 13:24:20 INFO: Successfull subscribed to the comfoair/ha_climate_mode/set topic
01-02-2021 13:24:20 INFO: Successfull subscribed to the comfoair/ha_climate_mode/fan/set topic

How have you configured the MQTT integration? If via configuration.yaml check that you don’t have the discovery: false there. If via gui check the following in the system options of MQTT integration:
image
If I recall properly I’ve seen somewhere a post suggesting to remove the integration and re-configure it from scratch. The discovery is enabled by default.

I’ve double-checked this a few times, it is configured via the UI and the option from your screenshot is enabled.
I am also using the HACS shelly autodiscovery plugin and already have a bunch of automations built on top of those devices.
When I remove and re-configure the integration, will the automations still work or would I need to rebuild all of that?

Hi,
from the logs it looks fine. Do you have more informations for us?

  • How is MQTT configured in Home Assistant? (discovery_prefix? discovery? - no yaml configuration for mqtt?)
  • Do you have except of the ca350_* entities other auto discovery entities in homeassistant/[type] ?
  • Do both alive messages exist (homeassistant/status and comfoair/status)?

Best,
Tim

OK, let’s see…

  • MQTT was configured using the UI with the following options:

    • Enable discovery: on
    • Enable birth message: on
    • Birth message topic: homeassistant/status
    • Birth message payload: online
    • Birth message QoS: 0
    • Birth message retain: off
    • Enable will message: on
    • Will message topic: homeassistant/status
    • Will message payload: online
    • Will message QoS: 0
    • Will message retain: off
  • (a bunch of shellies

  • homeassistant/status = online, same for comfoair/status

The shellies come in via https://github.com/bieniu/ha-shellies-discovery and I have a custom tasmota device with a bunch of sensors that were autocreated, so in theory, it should work…

Thanks a lot!

Pete

Just for reference: