Python Script Assistance - Meross Sensons

Here’s a library and script developed in Python 2.7 as far as I know covering Meross sensors. You may be able to modify it for use if your Python 3.7 route doesn’t work. I imagine you’ve enough info to be working with at this stage.

My advice as always to HG users is if you are making hardware purchases based around HG make sure the protocol is supported and there’s a MIG already in place for it. While I still use HG it’s mainly for its X10 support. Every other protocol I use is supported straight out of the box with HASSIO (Home Assistant) including your Meross sensors.

I’m using that library or a derivative of it. I was getting run time errors on:
from meross_cloud import MerossHttpClient

And similar lines. I did find some code that shows how to update the PATH variable and I was working on that but did not have time today. Hopefully I can get back to it over the weekend.

… and I bought that sensor because it allowed remote checking and IFTTT, which I set up. It was after that I had the idea to Integrate it (or try) into HG.

Do bear in mind that HG uses the Iron Python 2.7 engine and not the regular Python which has its own backend. Again it’s just something you may want to take note of.

Just as an aside, to save yourself a lot of time have you considered offloading the sensors to a separate instance of Python and deploying the results to an MQTT client/broker. Just food for thought.

Played around with this some more this morning. I needed to change the paths I was trying to append to match where the meross python library was:
import sys

this will tell HG where to look for libraries

sys.path.append("/home/pi/.local/lib/python3.7")
sys.path.append("/home/pi/.local/lib/python3.7/site-packages")
sys.path.append("/home/pi/.local/bin/")

But neither this:
import MerossManager

nor this:
from meross_iot.manager import MerossManager

is working and I get a runtime error.

On the pi itself via Putty, there is a meross_info_gather program in the /home/pi/.local/bin directory.
at the command line, I typed:
python3 meross_gather_info

And the program ran (it asks for your email and password and then connects and does some stuff). It creates a zip file (not sure how to open the zip file or copy it from my pi to my desktop to be able to see it however).

On another note, I do have the Meross sensor set up in IFTTT and it will send me a text message via an applet when the humidity is out of the defined range. Wondering if it may be simpler to have the IFTTT applet make a HG API call and turn on a device I create.

I found IFTTT to slow for my liking for turning on lights. Other things didn’t seem to bother me so it maybe worth atleast a try.
Also since you can get things done vis the command line you could have HG run the script via command line instead of directly. This may also be slower then you’d like. I do this for my broadlink stuff.

If it’s slow I’m not worried, it’s not a light I’m controlling. I basically want to see if I can have the AC in my basement turn on and off based on the Meross sensor readings.

Youd probably be happy with eirher approach. However I prefere to stay away from cloud automation services as much as I can.

I’d still like to get the Python script figured out- would be good to have more options. I haven’t been able to get the library files to import however.

Unfortunately there isn’t a large amount of Python examples available in the HG community. As you can see from searches it’s mainly C# and Javascript. Pity really as Python is such an easy environment to develop in.

You might be lucky if one of the HG Python programmers drop by here at some stage and steer you in the right direction if you haven’t found a solution yourself by then.

I made some progress with this the last few days (off and on working on this).

Using this:
sys.path.append("/home/pi/.local/lib/python3.7/site-packages/")

appears to be the correct path now to the meross package and this:
from meross_iot.cloud.devices.subdevices.sensors import SensorSubDevice

does not result in the same error about not finding the module - it does however result in an error on an unexpected “:”.

Similarly, this line:
from meross_iot.meross_event import MerossEventType

no longer has the unknown module error but instead has this error:
“no module named enum”

I think the issue is that the MerossEventType module has some dependencies on other modules that need to be loaded. In the code, this line:

import os

was giving me errors so I commented it out - my guess is that the enum module is part of that. In addition to the os module, this line is also in the code:

from random import randint

Similarly, that is giving me an error.

I am going to try and find where the os and random packages are on my pi unit and see if I can update the sys.path to include that and see if that resolves the import errors for those packages.

I resolved the enum module issue:
sys.path.append("/usr/lib/python2.7/dist-packages/")
import enum

But getting an error on:
from meross_iot.manager import MerossManager

and a missing base64 module.

I tried one of the other python scripts in the same library and getting similar errors. I think its a dependency issue with some of the modules (i.e. base64) and their not being available in the HG Python Script editor - I tried multiple ways to get the missing modules imported with no luck.

So I’m moving on to try and replicate what the code was doing via an api call instead
i,.e.

https://iot.meross.com/v1/Auth/Login

which returns"
{“apiStatus”:1023,“sysStatus”:0,“data”:null,“info”:“Sign check failed”,“timeStamp”:“2020-06-10 12:23:47”}

I need to find out how to pass the user email and password and the other required fields as part of that api url.

Should you be trying to establish an https connection to Meross. Have you tried http instead.

A nice little write up for Meross in Domoticz that might be of use. I’ve Domoticz installed and it’s quite useful.https://www.domoticz.com/forum/viewtopic.php?f=56&t=25007&sid=8469f1da863b3a54d71e977e8bf82f99&start=20

I just tried http instead of https - I don’t get anything back with that. With https I am at least getting the string returned I mentioned.

I may reach out to the person who developed the Python library and see if they could share the API documentation from Meross. In the meantime, I will check out the link you provided.
Thanks

I read thru the info on the link (Domoticz) and it seems to be the same approach I am attempting. At least in HG, the issue appears to be trying to get the script to compile and run with the import commands finding the modules it needs without error - there appear to be dependencies in the meross modules to other modules that are not being loaded (i.e. the base64 error). I’m stuck trying to resolve that.

Interestingly, in the Domocticz post from the link you provided it is mentioned that IFTTT is also an option - and that is what I had been working on the other day. I have a dummy switch set up in HG and then defined an IFTTT applet to send an On or Off command (based on the humidity) to HG and that does seem to work.

If that proves reliable, I may just use that but the python script offered much more info and control (i.e. you can get the actual temp and humidity readings wheras the IFTTT applet just sends a message when the trigger is reached, and does not include the actual temp or humidity value).

Yes Alberto would be the man to contact on all things Meross and Python. Possibly the best route to go at this stage https://github.com/albertogeniola/MerossIot

I posted this on the github site for the Meross IOT library:

Perfect and the Github is active as can be seen from the latest update. I’ll give you one thing, you’re persistent and hopefully a solution will pay off as a result.