Bitmanipulation for status and command via mqtt payload

Hi all

I’m pretty new to Home assistant.
The whole reason I tried to set it up is because I have rather dumb “domotics” light control system.
It can only toggle output based on input switches (each with an own address strap).

What I have done to augment the domotics system is develop an arduino shield myself containing some i2c contolled IO expanders so I can trick the system to simulate button switches.
Also I’m able to report back the current status of each output of my light system.
Now I’m able to control the system via a stupid app and udp commands.

Now, i would like to connect HA as a control.

I set up mqtt and my arduino is perfectly capable of sending his status as a 32 bit mask (just an integer number in the mqtt data body). Each bit representing the state of one of the outputs.

Now the hard part (for me):
Below you see the mqtt related configuration in my configuration.yaml. What I would like to do is to be able to let HA communicatate via one mqtt Topic and 32bit value to control all lights.

So in my example, the kitchen is situated as bit 0 in the bitmask

I tried 2 versions to get the state:
1:

    light:
      - platform: mqtt
        name: "kitchen"
        command_topic: "DOMESTIA/SWITCHES"
        state_topic: "DOMESTIA/SWITCHES/STATUS"
        state_value_template: "{{ value|int|bitwise_and(1) > 0 }}"
        payload_on: "on"
        payload_off: "off"

2:

    light:
      - platform: mqtt
        name: "kitchen"
        command_topic: "DOMESTIA/SWITCHES"
        state_topic: "DOMESTIA/SWITCHES/STATUS"
        state_value_template: < 
            {% if {{ value|int|bitwise_and(1) }} > 0 %}
            "On"
            {% else %}
            "Off"
            {% endif %}
        payload_on: "On"
        payload_off: "Off"

Both no success. I checked around for templating but I cannot connect the code snippets i found to get the result I would like to achieve.

2nd part of my question is how i would be able to create the command being build up with the same kind of 32 bit mask as a command (So Command topic to set/clear the domotic outputs based on the 32 bit integer

I know it would be easier with building jsons strings to communicate between both systems, but i lack program memory to store all the parse code…

I hope somebody can get me started in the correct direction.

Do you have an example of the value you get from state_topic and what you want to inject to make actions in command_topic

The topic reply is a 32bit decimal number (so from 0 to 4294967295)
The bitmask makes is easy to get all the output statusses in 1 simple reply.

I don’t know if HA is capable of setting the state of all 32 entities upon receiving just 1 topic message…

So basically:

  • For the state_topic, I want to parse one particular bit (per output). if ‘1’, the state is “On”, ‘0’ represents “Off”
  • For the command_topic, I would like the “On”/“Off” command to be represented as a ‘1’/‘0’ in a 32bit mask (which can be converted to a decimal before it gets injected in the topic payload)

Hi Dries! Just stumbled on your post here. I have the same Domestia system in my home. 2-3 years back I also started looking for a way to integrate Domestia in to HA. I got it working in the end using command line switches calling Python scripts which on their turn send UDP packets to the LAN-DME module to turn on/off lights and to determine the on/off status every 15s or so. Your solution seems more elegant… :wink: did you get it working? Interested in learning how you managed to get MQTT working with Domestia…

FYI, I recently talked to Domestia on their future plans. They are currently supporting Google Home and are working on in paralel on supporting Alexa and Homekit.

Hi Benoit

Yes, I’ve got it running, but I have to mention here that my project is totally custom made.
I created a PCB where I put on some extra Domestia - “addressing” modules which are normally connected at the wall switches.

My Custom PCB is basically an I²C IO expander on an arduino uno which also has an ethernet shield.
The addressing modules are not directly connected to the IO expanders, I separated them Galvanically using opto isolators.

I still have a 2nd part of my project, which is reading back the current status of the outputs on domestia.
Again, adding galvanic isolation of the 220V to a 3v3 input on an IO expander and taking the data in on the arduino. This would allow home assistant to visualize the current status…

MQTT is easily set up on an Arduino, there are libraries available.

I would love to hand you the PCB layout and schematics, but unfortunately i lost all of my drawings in a PC crash years ago. One day, my PCB will start having issues, so then I will have to redraw everything myself.

I created this project far before Domestia had the LAN interfacing module. If I would redo it, I’d probably buy the Ethernet module and see what the possibilities are in connecting the system to home assistant.

Hopefully, Domestia realizes that we live in a world where everything connects to everything in a generic way.
I asked them years ago if they would foresee something like REST interfaces or another standard protocol.
I hope they have something in the pipeline in that field.

Dries

That sounds super interesting, but far beyond my skills which are limited to the software side… :wink:

I used to the have the first version of the LAN module but that one died on me. The second version is doing fine and also offers more options like remote control via the Domestia app. (I’m not very confident though that it is all very secure…)