How to build CGate, CGateWeb, MQTT, Home Assistant and Home Bridge

Discussion in 'Third-Party Solutions' started by theboyg, Nov 30, 2021.

  1. theboyg

    theboyg

    Joined:
    Apr 3, 2005
    Messages:
    206
    Likes Received:
    8
    How to build CGate, CGateWeb, MQTT, Home Assistant and Home Bridge

    After pulling together all the disparate information I could and, along with the learning curve of Home Assistant, YAML, MQTT and more, here's my guide on how to start from scratch to get you up and running. It took a lot of trial and error with curveballs along the way, but this is a reproducible installation.

    You'll need two Raspberry Pis plus a suitable serial to USB or RS232 if using a USB or RS232 CNI, or ignore this if you have an Ethernet CNI (which I don't have).

    The steps will be:
    1. On Pi #1, Install Home Bridge (optional, if you need it. I use it to surface Unifi Protect cameras into Home Assistant, plus Siri)
    2. Install Java
    3. Install CGate
    4. Install CGateweb
    5. Install Home Assistant on Pi #2
    6. Configure the MQTT broker in Home Assistant
    7. Configure Home Assistant for MQTT and C-BUS
    8. Configure Home Assistant scripts for C-BUS devices

    1. Home Bridge

    First grab Home Bridge v1.0.17 from https://github.com/homebridge/homebridge-raspbian-image/releases

    V1.0.25 doesn’t work as it breaks MQTT in NPM with Node 16. There maybe a release later than 1.0.17 that is viable, but this works with Node 14 and you can update Home Bridge once it’s installed

    2. Install Java

    Code:
             sudo apt update
             sudo apt upgrade
             sudo apt install openjdk-8-jdk openjdk-8-jre
             java -version
    3. Install cgate using these instructions
    https://blog.addictedtopi.com/2017/05/21/installing-c-gate-on-a-raspberry-pi/
    • note : use the Debian Jessie rules for the Pi install, and select pi as the user instead of root in the ser2sock.service file (if you’re using a serial connection)

    As of writing the latest cgate is at https://updates.clipsal.com/Clipsal...ite/cis/technical/CGate/cgate-2.11.4_3251.zip


    Once installed:
    -Configure /usr/local/bin/cgate/config/access.txt

    • i.e. if you’re on a 10.2.x.x network, and 101 is your PC for provisioning C-BUS

    Code:
    remote 10.2.0.255 Program
    remote 10.2.0.101 Program
    interface 127.0.0.1 Program
    - Configure /usr/local/bin/cgate/config/C-gateConfig.txt
    • Ensure project.default= and project.start= is your project file
    • Don’t forget to transfer your project file to the /usr/local/bin/cgate/tag directory

    4. Install CGateWeb

    Review these instructions https://addictedtopi.tumblr.com/post/101165284373/setting-up-cgateweb-c-bus-to-mqtt-on-linux

    But don’t follow them as outlined, do this instead

    Code:
    cd /usr/local/bin
    sudo git clone https://github.com/the1laz/cgateweb.git
    sudo chown -R pi:pi cgateweb
    cd cgateweb
    sudo npm install
    
    (Take npm advice on screen)
    
    sudo npm install forever -g
    mkdir log
    nano settings.js
    -Now edit the settings, uncomment the credentials and specify what you will use in Home Assistant for MQTT. As an example, specify the username and password as mqtt and mqtt, you will specify this later in the MQTT broker in Home Assistant.
    -
    Set the project name to your bus project name, and set the IP of your Home Assistant Pi
    Ensure the following are set:
    Code:
    exports.cbusip = '127.0.0.1';
    exports.cbusname = "(your project name here)";
    exports.mqtt = 'IPofyourHomeAssistantPC:1883';

    -Now do the following
    Code:
    sudo nano /etc/systemd/system/cgateweb.service
    Code:
    [Unit]
    Description=cgateweb
    [Service]
    ExecStart=node /usr/local/bin/cgateweb/index.js
    Restart=always
    User=pi
    Group=pi
    Environment=PATH=/usr/bin:/usr/local/bin
    Environment=NODE_ENV=production
    WorkingDirectory=/usr/local/bin/cgateweb
    [Install]
    WantedBy=multi-user.target
    - Then run:


    Code:
    sudo systemctl enable cgateweb.service

    We move to Pi #2


    5. Install Home Assistant
    Follow the instructions on their website

    6. Install the MQTT Broker

    Once installed, specify the username and password in the config such as mqtt/mqtt

    Your MQTT config should look something like this:

    Code:
    logins:
      - username: mqtt
        password: mqtt
    customize:
      active: false
      folder: mosquitto
    certfile: fullchain.pem
    keyfile: privkey.pem
    require_certificate: false

    7. Configure Home Assistant for MQTT and CBUS

    Install the File Editor Home Assistant plugin so we can now edit the .yaml files

    Ensure these are listed in the configuration.yaml

    Code:
    automation: !include_dir_merge_list automation/
    light: !include light.yaml
    • -Create a folder called ‘automation’
    • -Create a new file called light.yaml
    • -Create a new file cbusautomation.yaml in a folder called automation
    In the light.yaml you can start to specify your C-BUS devices.

    An example for a dimmer

    Code:
      - platform: mqtt
        name: Den
        state_topic: "cbus/read/254/56/0/state"
        command_topic: "cbus/write/254/56/0/switch"
        brightness_state_topic: "cbus/read/254/56/0/level"
        brightness_command_topic: "cbus/write/254/56/0/ramp"
        payload_off: "OFF"
        on_command_type: "brightness"
        unique_id: mq_den
        brightness_scale: "100"
    or a switch

    Code:
      - platform: mqtt
        state_topic: "cbus/read/254/56/16/state"
        command_topic: "cbus/write/254/56/16/switch"
        payload_off: "OFF"
        payload_on: "ON"
        name: Cinema
        unique_id: mq_cinema
    8. Do some Home Assistant logic based on C-BUS activity

    In your new cbusautomation.yaml , in the automation folder, you can create script to respond to C-CBUS commands.

    This one toggles a C-BUS switch that already controls a garage door

    Code:
    - id: cbus_garage_door_control_toggle
      alias: Garage Door
      trigger:
      - entity_id: light.garage_double_door_control
        platform: state
      action:
      - service: light.turn_off
        target:
          entity_id: light.garage_double_door_control
        data:
          transition: 1
      mode: single 
    This one grabs the dim level of the C-BUS ID, and tells a Shelly RGBW LED to set the LED to the same level

    Code:
    - id: cbus_shelly_led
      alias: cbus_shelly_led
      trigger:
      - entity_id: light.shelly.led1
        platform: state
        attribute: brightness
      action:
      - service: light.turn_on
        target:
          entity_id: light.shelly.led1
        data:
          brightness: "{{ state_attr('cbus_shelly_led,'brightness') |int(0) }}"
      mode: single

    And lastly, reboot both Pis.

    Big thanks to @Damaxx for the inspiration!
     
    Last edited: Nov 30, 2021
    theboyg, Nov 30, 2021
    #1
    Ian, zei20t, Damaxx and 1 other person like this.
    1. Advertisements

  2. theboyg

    zei20t

    Joined:
    Aug 18, 2010
    Messages:
    128
    Likes Received:
    1
    Location:
    Sydney, Australia
    ive found using node-red as a logic engine is amazing. and easy.

    im mostly using HA as the GUI, NR does all logic and interfacing between device types.

    saves so much on yaml code
     
    zei20t, Dec 14, 2021
    #2
    1. Advertisements

  3. theboyg

    theboyg

    Joined:
    Apr 3, 2005
    Messages:
    206
    Likes Received:
    8
    I've been running into a lot of issues where the system becomes unresponsive, from what I can tell cgateweb can't read changes to C-BUS, but can write. I'm now using this fork on GitHub and if you want to try, swap out section 4 code as follows:
    Code:
    cd /usr/local/bin
    sudo git clone https://github.com/rbhr/docker-cgate-mqtt.git
    mv docker-cgate-mqtt cgateweb
    sudo chown -R pi:pi cgateweb
    cd cgateweb
    sudo npm install
    
    (Take npm advice on screen)
    
    sudo npm install forever -g
    mkdir log
    nano settings.js
    
     
    theboyg, Jan 9, 2022
    #3
  4. theboyg

    MichaelD

    Joined:
    Apr 3, 2016
    Messages:
    44
    Likes Received:
    0
    Location:
    UK
    I'm using cgateweb to send and receive messages through my MQTT broker, it does become unresponsive, and can eventually choke C-Bus altogether so even the lightswitches don't work.

    I'm using a second instance of cgate to check the C-Bus log through a CNI, and what appears to happen is HomeSeer3 will send a single 'on' for a c-bus group, but somewhere between the HS3 and the C-Bus network, that single on command becomes 30+ on commands. I have a night-time HS3 housekeeping event that sets a whole series of groups to their default value, so about 40 groups being set one after the other, and that can take over 5 minutes, with the whole c-bus network being overwhelmed by hundreds of repeated messages.

    The only way I've found to fix this is to unplug the Pi then plug it back in again, then it will all work OK for a few weeks, then by about 4 weeks or so, it will be back babbling repeated messages onto C-Bus and the Pi needs powercycling again.,
     
    MichaelD, Jan 9, 2022
    #4
  5. theboyg

    Pian

    Joined:
    Monday
    Messages:
    2
    Likes Received:
    0
    Hello
    I have been using the wonderful cgateweb for several years now on various platforms, and though I'd chip in with a few thoughts.

    (As an aside, I was one of the users who persuaded Home Assistant to change theor mqtt light implementation to work with cbus, back in 2017.)

    I don't think there is a need for multiple devices - one pi should be able to cope with all of this if you don't have heavy Home Assistant requirements. That said, i migrated to an i3 NUC a couple of years ago and haven't regretted it.

    I strongly suggest that you look into using Docker to run each of these programs. It keeps them separate from the base operating system, and makes things easier to debug and maintain. Personally, I have individual Docker containers running (among others)
    - ser2sock - to interface between the USB on the NUC and the serial port on the CNI
    - cgate - to manage the c-bus system
    - cgateweb - to interface with mqtt
    - an mqtt broker
    - of course, home assistant
    - (I'd also suggest that you look at including Dozzle to see logs, and maybe Portainer to help with container restarts)

    Aside from the usual fun and games when HA does an update, this setup has proved to be as stable as you like for me. And certainly easier to manage and see what is going on that my previous collection of pi's

    I am happy to offer assistance and share my docker-compose setup if it is felt useful - just holler
     
    Pian, Jan 24, 2022 at 3:31 PM
    #5
  6. theboyg

    theboyg

    Joined:
    Apr 3, 2005
    Messages:
    206
    Likes Received:
    8
    Hey, @Pian

    I'd happily run all of these as Dockers. I did try the cgate docker I found, but it was very unreliable using an old cgate build and Toolkit connecting to it from a Windows machine caused all sorts of errors to pop up which I'd never seen before, either running locally or to the Pis natively. So I deleted that and have not found another.

    If you have a list of the docker images you run, or share your compose script, please let me know, I'm up for testing them out. Home Assistant has it's own Pi 4 and I don't run any other containers on it, I'll leave that alone.

    Thanks!
     
    Last edited: Jan 24, 2022 at 8:16 PM
    theboyg, Jan 24, 2022 at 8:11 PM
    #6
  7. theboyg

    Pian

    Joined:
    Monday
    Messages:
    2
    Likes Received:
    0
    Hi @theboyg

    Although it is now two years old, the docker image steppinghat/cgate-server:latest works just great for me. It is using v2.11.4 of c-gate. I have it using host networking, as I was being lazy when I started using it and have never gotten around to making it more secure.

    For the other two (ser2sock and cgateweb) I built my own containers. The Dockerfile for cgateweb (once the cgateweb.service and index.js file for cgateweb are in the same folder) is simple enough:

    Code:
    FROM node:8-alpine
    WORKDIR /app
    COPY . /app
    RUN npm install
    CMD node index.js
    For ser2sock is it ever so slightly more complicated. I use a entrypoint.sh file which is
    Code:
    #!/bin/sh
    set -e
    
    # Exit with error if SERIAL_DEVICE wasn't provided
    if [ -z "${SERIAL_DEVICE}" ]; then
      echo >&2 "You must specify the SERIAL_DEVICE environment variable."
      exit 1
    fi
    
    exec ser2sock -f -p ${LISTENER_PORT} -s ${SERIAL_DEVICE} -i 0.0.0.0 -b ${BAUD_RATE}
    
    and a Dockerfile which is
    Code:
    FROM alpine:latest
    
    # Set environment variables.
    ENV \
      PAGER=more \
      LISTENER_PORT=10001 \
      BAUD_RATE=9600
    
    # Install packages.
    RUN \
      apk --update add \
        build-base \
        git \
        libressl-dev && \
      rm -rf /var/cache/apk/*
    
    # Install the ser2sock application.
    RUN \
      cd /tmp && \
      git clone https://github.com/nutechsoftware/ser2sock.git && \
      cd ser2sock && \
      ./configure && \
      make && \
      cp ser2sock /usr/local/bin/ && \
      cp -R etc/ser2sock /etc/ && \
      cd .. && \
      rm -rf ser2sock
    
    # Add files to the container.
    COPY ./entrypoint.sh /docker-entrypoint
    
    # Set the entrypoint script.
    ENTRYPOINT ["/docker-entrypoint"]
    
    # Expose the listener port
    EXPOSE ${LISTENER_PORT}
    
    Let me know if you meed any help with configuring, but it should be pretty straightforward
     
    Pian, Jan 25, 2022 at 9:54 AM
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.