Python Script Assistance - Meross Sensons

Good afternoon,

I recently purchased a meross temperature / humidity sensor for my finished basement. I want to be able to get readings from it and incorporate that into HG.

I found a Python script library that seems to have what I need but I am running into issues getting the Python script in an HG Program to work.

Following the install instructions on the Python Meross Iot Library:

I was able to install pip3 and python 3.7 on my Pi.

I then created a new HG Program and copied the code from the Usage into the program. It complies but I get run time errors.

Specifically, the:
from meross_iot.cloud.devices.door_openers import GenericGarageDoorOpener

lines result in run time errors as it cannot find the files / folders.

Hoping someone may have some experience with Python Script in HG and shed some light on how those libraries cab be correctly loaded.

Thanks

I assume you know that by default Python 2.7 is installed in HG. I’m not sure if you set up a virtual environment before you installed Python 3 and pointed HG to it. Anything Python related I’m using in HG uses Python 2.7 but it’s probably time for an upgrade as it’s at EOL at this stage. Again this is just general advice and you probably knew this anyway.

Yes, I have both 2.7 and 3.7 installed. HG should not be affected, I needed 3.7 for the Meross library - just need to figure out how to get it loaded

Are both versions installed in their own separate environment.

I mainly use Python in Home Assistant and just create a venv and install the relevant application. It’s quite simple that way.

Not knowing what level of Python experience you have I found this primer to be quite informative. https://virtualenv.pypa.io/en/latest/user_guide.html

Not many have successfully managed to get beyond Python 2.7 in HG. Of course Buster is the first release that has Python 3.7 in the repos as standard. It’s virtually impossible to remove Python 2.7 from your install.

Have a look at the contents of the HG Gdebi package installer for clues. You could possibly request that Python 3.7 is included as standard unless there is a good reason not to (refactoring code forward compatibility issues etc).

Can you get your script to run from the Pi commandline?

I haven’t had a chance to work on this more today, but I can try that when I get a chance.

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.