Meross: python library with mqtt

Admittedly, the solution for HomeAssistant looks completely fleshed out. Sorry, this is sadly not that.

1 Like

For those who are interested, I did some work on a meross-bridge to connect Meross Thermostats with OpenHAB. It is still an external service that translates Meross MQTT to OpenHAB MQTT.

Maybe a native OH2 add-on would be better but this is using the meross-iot Python library and I didn’t want to re-invent the weel.

So feel free to use it, feedback is welcome:

3 Likes

Alle die mit dem klasse Skript gerne den Status ihres Garagentors in mqtt gepublished haben möchten:

Einfach in Zeile 59:
client.publish("meross/%s" %(eventobj.device.name),eventobj.door_state)
einfügen

PS: Danke für das Skript und die Beschreibung!

2 Likes

Hello, everybody. First of all, thank you very much for this nice tutorial. I was able to follow more or less everything and I think almost everything works, but since the Meross plug is only the second thing I implemented in Openhab, I have some basic problems with the connection between OH and the plug.
So I have an item in OH and I can switch the “plug” on and off and I also get the message in MQTT.fx “meross/device_name = OFF”, but the real plug does not switch off. I also used the examples from https://github.com/albertogeniola/MerossIot/ and was able to switch the plug off with putty and ssh. The meross2mqtt.service is also running.
Now I cannot get connections from OH to the ms310 plug. Do I need to add anything before which is basic and not mentioned here? How does OH know where the Python script is located? Is there something like in the exec-binding where I have to write the path or something like that?
Thanks for your help!

Great tutorial and sample code using albertogeniola MerossIot python library :smiley:

This code works fine using meross_iot==0.3.1.9.

But is there someone that succeeded to rewrite it using the latest meross_iot 0.4 which is a complete rewrite of the meross_iot library using async functions ?

Maybe I will have a check on Daniel Morlock Meross-bridge as a great inspiration !

I finally did it :slightly_smiling_face:.

For those interested by an upgraded version using merossiot 0.4 and Python3.9, have a look at this code

extensively inspired by excellent code from Daniel Morlock / meross-bridge

2 Likes

Hi, I am using “meross2mqttv2”, use it successfully in terminal, but when I create a service for it, it failed to start. Is anyone successfully created service for it?

Eric

Hi Denis,

thank you so much, I’m using your amended Phython 3.9 script with my Openhab 3.0 Raspberry Pi system. It works perfectly! I’m using it with my Meross MSS550X wallswitches.

so following is needed:

for those who need to configure the MS550X Openhab 3.0 channel should also work with Openhab 2.5:

Meross outgoing value transformation .js:
setMerossState.js.txt (119 Bytes)

Hi supoeric,

this is my service unit configuration, I copied Denis script into my openhab folder at /etc/openhab/scripts

[Unit]
Description=meross2mqttV2
After=mosquitto.service

[Service]
Type=simple
ExecStart=/usr/bin/python3 /etc/openhab/scripts/meross2mqttV2.py --mqtt-server 127.0.0.1 --mqtt_usr openhab --mqtt_pswd <‘your mqtt pwd’> -e <‘your meross mail’> -p <‘your meross pwd’> -l var/log/meross2mqtt/meross.log
Restart=always
User=openhab
Group=openhab
Environment=PATH=/usr/bin:/usr/sbin:/etc/openhab/scripts
Environment=NODE_ENV=production
WorkingDirectory=/etc/openhab/scripts

[Install]
WantedBy=multi-user.target

Status of my service - it works with the above config despite the last column it’s not Gone or Offline.
No clue what this means, but the service is running at I can control my switches via openhab.

1 Like

Thanks Betahydri,
I will try it tonight.

Eric

@Denis_Lambert and @betahydri thanks a lot for your effort. I finally came around to update to oh3 and try the new version for merossIot.
Running the script from “meross2mqtt v2” I encountered a TimeOutError. I “fixed” it putting a try - except around line 204. Not proper, but works when I run it from the command line. I am still having troubles when using the service though.

Edit: Okay, I had a few errors in my service file. It is working!
I am still getting this error, as mentioned before:

Mar 23 09:57:12 openHABianPi python3.9[1922]:   File "/usr/local/lib/python3.9/asyncio/base_events.py", line 596, in run_forever
Mar 23 09:57:12 openHABianPi python3.9[1922]:     self._run_once()
Mar 23 09:57:12 openHABianPi python3.9[1922]:   File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1890, in _run_once
Mar 23 09:57:12 openHABianPi python3.9[1922]:     handle._run()
Mar 23 09:57:12 openHABianPi python3.9[1922]:   File "/usr/local/lib/python3.9/asyncio/events.py", line 80, in _run
Mar 23 09:57:12 openHABianPi python3.9[1922]:     self._context.run(self._callback, *self._args)
Mar 23 09:57:12 openHABianPi python3.9[1922]:   File "/etc/openhab/scripts/meross02.py", line 79, in start
Mar 23 09:57:12 openHABianPi python3.9[1922]:     self.log.info("Setting Last will message=", lwm, "topic is", self.args.mqtt_ident)
Mar 23 09:57:12 openHABianPi python3.9[1922]: Message: 'Setting Last will message='
Mar 23 09:57:12 openHABianPi python3.9[1922]: Arguments: ('Meross Gone Offline', 'topic is', 'meross')

Is this normal?

Hello @daJoe ,

The error you got was simply caused by old formatting code. It was not harmful and offending line could have been commented out but it is now corrected in the latest version of this code. Also added the try … except in case the asyn_update takes more time than expected (5 sec)

1 Like

Youre right. Measuring the power, voltage and current is not published by the device itself. You have to make a request to get the data.

Im new to openhab. But i think its easy to adapt the python scripts here to do an active polling

Hi

I’m running OH 3.1.0
Python3 --version 3.8.10
Meross_iot 0.4.2.3

Sorry for all the text, but I can’t get my Meross switch to work with my local MQTT / openhab

I use Albertogeniola Merosslot example script regarding to this link and my mss710 turns on and off.

I look at daJoe Johannes R:s script, regarding to this link

If I’m running daJoe Johannes R “old” script meross2mqtt.py with meross_iot==0.3.1.5 and tested with meross_iot==0.4.2.3

I got error on line 3, (I can’t import MerossEvent)

error
No module named meross_iot.meross.event

Where can I found this module?

Next step

Then I followed Opus Terra this link

My “new” meross2mqttV2.py file and I changed following

parser.add_argument(’–mqtt-server’, dest=‘192.168.1.174’,

parser.add_argument (’-e’, ‘–email’, dest=‘my real email@hotmail.com’,

It says in the script that meross password can be set later

When I run

'sudo python3 meross2mqttV2.py

I get this error
meross2mqttV2.py: error: the following arguments are required: -e/–email

Why this error?

Then I add my password in meross2mqttV2.py file

parser.add_argument (’-p’, ‘–password’, dest=‘my real meross password’

I get this error
File “Meross2mqttV2.py”, line 63
self.loop = loop:
^
SyntaxError: invalid syntax

When I’m running meross-bridge manual with meross_iot 0.4.0.0rc9
regarding to this link meross bridge
I installed meross bridge via suggestion 2 and ssh

Then run

sudo meross-bridge --mqtt-server 192.168.1.174 -e myemail@hotmail.com -p my merosspassword

I get this error

INFO:Initializing the MQTT connection…
INFO:Discovery found a new Meross device Fönstret (18072498697172251axxxxxxxxx).
ERROR:Timeout occurred while waiting a response for message b’{“header”: {“from”: “/app/68xxx-4aa4594b7e208d39xxxxxxxxx/subscribe”, “messageId”: “d2134e453b1170xxxxxxxxxx”, “method”: “GET”, “namespace”: “Appliance.System.Ability”, “payloadVersion”: 1, “sign”: “0775fc51a3080b4a687d43afexxxxxxxxx”, “timestamp”: 1632xxxxxx}, “payload”: {}}’ sent to device uuid 18072498697172251xxxxxxxxx. Timeout was: 5.0 seconds
ERROR:Failed to retrieve abilities for device Fönstret (1807249869717225xxxxxxxxxx). This device won’t be enrolled.
INFO:Starting consumer for OpenHAB MQTT messgaes …

If I keep following the meross bridge guide.
I add/made a file in /etc/default/meross-bridge with my email and meross password
chmod 0600

Then I looked for a meross-bridge.service file in /etc/systemd/, but I don’t have anyone so I can’t enable/start meross-bridge.service

My Meross info

My meross switch info

INFO:The following devices are new to me: [{“uuid”: “18072498697172251axxxxxxx”, “online_status”: “ONLINE”, “dev_name”: “F\u00f6nstret”, “dev_icon_id”: “device027_un”, “bind_time”: “2021-05-07T20:25:04”, “device_type”: “mss710”, “sub_type”: “un”, “channels”: [{}], “region”: “eu”, “fmware_version”: “2.1.4”, “hdware_version”: “2.0.0”, “user_dev_icon”: “”, “icon_type”: 1, “skill_number”: “”, “domain”: “mqtt-eu-2.meross.com”, “reserved_domain”: “mqtt-eu-2.meross.com”}]

My snifferlog

INFO - USER-TOPIC (/app/68xxx/subscribe) ← {‘header’: {‘timestampMs’: xxx, ‘timestamp’: 163xxxxxx, ‘sign’: ‘18c9ef6e1411170138axxxxxxxx’, ‘payloadVersion’: 1, ‘namespace’: ‘Appliance.Control.ToggleX’, ‘method’: ‘PUSH’, ‘messageId’: ‘bedb780340b1xxxxxxxxxxx’, ‘from’: ‘/appliance/18072498697172251axxxxxxxxx/publish’}, ‘payload’: {‘togglex’: [{‘onoff’: 1, ‘lmTime’: 1632xxxxxx, ‘channel’: 0}]}}

2021-09-19 15:34:43,721 - Sniffer - INFO - --------------- More data -----------------
2021-09-19 15:34:43,721 - Sniffer - DEBUG - Disconnected from MQTT brocker
2021-09-19 15:34:46,293 - Sniffer - INFO - Sysdata for Fönstret (18072498697172251axxxxxxx): {‘all’: {‘system’: {‘hardware’: {‘type’: ‘mss710’, ‘subType’: ‘us’, ‘version’: ‘2.0.0’, ‘chipType’: ‘mt7682’, ‘uuid’: ‘18072498697172251axxxxxxxxxx’, ‘macAddress’: ‘34:29:xx:xx:xx:xx’}, ‘firmware’: {‘version’: ‘2.1.4’, ‘compileTime’: ‘2018/11/30 11:02:57 GMT +08:00’, ‘wifiMac’: ‘74:ac:xx:xx:xx:xx’, ‘innerIp’: ‘192.168.1.113’, ‘server’: ‘mqtt-eu-2.meross.com’, ‘port’: 443, ‘userId’: 68xxx}, ‘time’:

‘online’: {‘status’: 1}}, ‘digest’: {‘togglex’: [{‘channel’: 0, ‘onoff’: 1, ‘lmTime’: 1632058482}], ‘triggerx’: [], ‘timerx’: [{‘channel’: 0, ‘id’: ‘26786d69xxxxxxxxx’, ‘count’: 1}, {‘channel’: 0, ‘id’: ‘e844fddaeb4xxxxx’, ‘count’: 1}]}}}

I have been reading Albertogiola:s page regarding to this link but I guess I have missed something:

Do I have to add some file somewhere with my mqtt broker(mosquitto) info or is it OK with just have my local ip in the meross2mqttV2.py file as above?

I don’t understand where I have to put my device info, from my snifferlog and which info is relevant in that file (and file ending/ for example devices.xx ?).

I guess I need to add my local device ipaddress, device mac address, uuid etc somewhere?

My mosquitto is tested/running via MQTT explorer

Thank you

Anyone is running Meross Garage Door Opener MSG100 with the script based on Meross 0.4 bridge?
The script recognizes my device, but I cannot integrate into OpenHAB.

I solved some of it.

I have miscalculated or lack of knowledge

I went back and changed mersso2mqttV2.py so with no changes in the file.
Then run
sudo python3 meross2mqttV2.py --email myemail@email.com --password mypassword

And I got connected.

mqtt explorer says

{“state”: “OFF”, “type”: “mss710”, “ChannelName”: “Main channel”, “friendlyName”: “F\u00f6nstret”, “online”: true}

Now I am in the same situation as @demichve

I can’t connect my channel to openhab.

The log says
string OFF could not be converted to JSON
string ON could not be converted to JSON Handling message from topic “meross/Fönstret/set”: OFF with qos 0 and retain flag 0

Openhab
I have just add in channel
mqtt state topic
meross/Fönstret/channel_0

mqtt command topic
meross/Fönstret/set

custom on
ON
custom off
OFF

Is command
switch to on

@Andreas3
You have to active the right transformations in OpenHab:
JSONPath Transformation
Javascript Transformation

Then it should work.

Yes. You need to go in Advance mode for the regarding channel and fix those two fields:

incoming Value Transformations
JSONPATH:$.state

Outgoing Value Transformation
JS:setZigbeeState.js

where setZigbeeState.js is simply this code that goes in the transform Openhab3 directory:

(function(x){

    var result = new Object();
    result.state = x;
    return JSON.stringify(result);
   
})(input)

Great, Thanks!
(I also forgot to install Javascript transformation :see_no_evil:)

I add/update the incoming and outgoing value in OH3.
I add the merossfile.js in /etc/openhab/transform

reboot

run meross2mqttV2.py script

Mqtt explorer
meross ={“state”:true}
Fönstret
channel_0 ={“state”:“OFF”,“type” etc etc
set = {“state”:“OFF”}

Now when I toggle ON/OFF I get a number of errors instead

-- Logging error ---
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/meross_iot/manager.py", line 630, in _async_send_and_wait_ack
    return await asyncio.wait_for(future, timeout, loop=self._loop)
  File "/usr/lib/python3.7/asyncio/tasks.py", line 423, in wait_for
    raise futures.TimeoutError()
concurrent.futures._base.TimeoutError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/etc/openhab/scripts/Meross2/meross2mqttV2.py", line 162, in handle_message
    await device[0].async_turn_on(channel=0)
  File "/usr/local/lib/python3.7/dist-packages/meross_iot/controller/mixins/toggle.py", line 110, in async_turn_on
    drop_on_overquota=drop_on_overquota)
  File "/usr/local/lib/python3.7/dist-packages/meross_iot/controller/device.py", line 233, in _execute_command
    drop_on_overquota=drop_on_overquota)
  File "/usr/local/lib/python3.7/dist-packages/meross_iot/manager.py", line 624, in async_execute_cmd
    timeout=timeout)
  File "/usr/local/lib/python3.7/dist-packages/meross_iot/manager.py", line 634, in _async_send_and_wait_ack
    raise CommandTimeoutError()
meross_iot.model.exception.CommandTimeoutError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.7/logging/__init__.py", line 1034, in emit
    msg = self.format(record)
  File "/usr/lib/python3.7/logging/__init__.py", line 880, in format
    return fmt.format(record)
  File "/usr/lib/python3.7/logging/__init__.py", line 619, in format
    record.message = record.getMessage()
  File "/usr/lib/python3.7/logging/__init__.py", line 380, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
Call stack:
  File "/etc/openhab/scripts/Meross2/meross2mqttV2.py", line 374, in <module>
    main()
  File "/etc/openhab/scripts/Meross2/meross2mqttV2.py", line 370, in main
    Runner().run()
  File "/etc/openhab/scripts/Meross2/meross2mqttV2.py", line 333, in run
    loop.run_until_complete(self.bridge.consume())
  File "/usr/lib/python3.7/asyncio/base_events.py", line 571, in run_until_complete
    self.run_forever()
  File "/usr/lib/python3.7/asyncio/base_events.py", line 539, in run_forever
    self._run_once()
  File "/usr/lib/python3.7/asyncio/base_events.py", line 1775, in _run_once
    handle._run()
  File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/etc/openhab/scripts/Meross2/meross2mqttV2.py", line 122, in consume
    await self.async_handle_openhab_mqtt_message(client, userdata, message)
  File "/etc/openhab/scripts/Meross2/meross2mqttV2.py", line 141, in async_handle_openhab_mqtt_message
    await self.handle_message("/".join(msg_split[1:-1]), str(message.payload.decode("utf-8")))
  File "/etc/openhab/scripts/Meross2/meross2mqttV2.py", line 189, in handle_message
    self.log.error("Unexpected error in MQTT message handling:", sys.exc_info()[0])
Message: 'Unexpected error in MQTT message handling:'
Arguments: (<class 'meross_iot.model.exception.CommandTimeoutError'>,)



As stated here , this code was written for python 3.9. maybe that’s why you got all those errors …