Python Plugin: Shelly MQTT

Python and python framework

Moderator: leecollings

lwolf
Posts: 216
Joined: Saturday 10 November 2018 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Hungary
Contact:

Re: Python Plugin: Shelly MQTT

Post by lwolf »

AimoPaukku wrote: Wednesday 01 March 2023 21:30 This sounds so easy... Edit settings.py file with a plain text editor, and set MQTT server IP address.
How?
It looks like you have installed everything that is needed.
git pull only needed for updating the script

Do not forget to enter into the script directory before starting or editing settings.py:

Code: Select all

cd shellyteacher4domo
How to edit?
I usually prefer to use mcedit, as it is unimaginable to me a Linux machine without mc aka midnight commander... (sudo apt-get install mc) but you can also use nano as it is preinstalled, and it is a simple text editor.

For example:

Code: Select all

nano settings.py
AimoPaukku
Posts: 43
Joined: Saturday 28 January 2017 9:35
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Contact:

Re: Python Plugin: Shelly MQTT

Post by AimoPaukku »

Code: Select all

pi@raspberrypi:~ $ cd shellyteacher4domo
pi@raspberrypi:~/shellyteacher4domo $ python3 shellyteacher4domo.py
Connecting to MQTT server...
Connecting to MQTT server...
Connecting to MQTT server...
Starting eval loop, waiting Shelly devices to appear on MQTT announce... press CTRL-C to cancel
ONLINE GEN2 shellyplus1pm
ONLINE GEN2 Shellies
>>>GEN2 device  shellyplus1pm-4855199d717c Shellies found
SUCCESS, I think so... Now I can see shelly sensors.

Do I still need to edit something?
AimoPaukku
Posts: 43
Joined: Saturday 28 January 2017 9:35
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Contact:

Re: Python Plugin: Shelly MQTT

Post by AimoPaukku »

After rebooting pi, devices do not update anymore.
MQTT is up and running, and shelly also.

I removed plugin and started again from beginning.
Now I do not see any discovered shelly device.... :shock:

Do I have to give

Code: Select all

python3 shellyteacher4domo.py
after every reboot?
lwolf
Posts: 216
Joined: Saturday 10 November 2018 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Hungary
Contact:

Re: Python Plugin: Shelly MQTT

Post by lwolf »

AimoPaukku wrote: Friday 03 March 2023 15:07 After rebooting pi, devices do not update anymore.
MQTT is up and running, and shelly also.

I removed plugin and started again from beginning.
Now I do not see any discovered shelly device.... :shock:

Do I have to give

Code: Select all

python3 shellyteacher4domo.py
after every reboot?
Which plugin did you removed? MQTT AD is a hardware, not a plugin. If the device is appearing after Domoticz restart, than MQTT AD is working. Removal of MQTT AD will not help.

There are a "retain" setting in settings.py it defaults to True, which instructs the MQTT broker, (mosquitto i guess) to preserve its contents when rebooted. I am using this method since a year and my Shelly motion survived several reboots, and still working in Domoticz through MQTT AD, and i had registered it only one time.
You can check if the content is saved if you start "MQTT Explorer" (for example) and read this topic content before and after reboot:
"homeassistant/switch/shellyplus1pm-4855199d717c/config"

If retained messages not saved, than check your mosquitto config, as it is not configured correctly:
https://stackoverflow.com/questions/361 ... -goes-down

Thanks for reporting this, i will update documentation about MQTT persistence.
I think it will be the problem, as Domoticz tries to read config informations stored under "homeassistant/*" and if the mosquitto persistence does not work it will be empty, so Domoticz has no information about how to communicate...
mcgiak
Posts: 18
Joined: Sunday 24 December 2017 10:15
Target OS: -
Domoticz version: 2023.2
Location: Veneto, Italy
Contact:

Re: Python Plugin: Shelly MQTT

Post by mcgiak »

Good evening
i have a problem with shelly em.
so far I have used the power meter only for consumption reading
now after installing photovoltaic, I have a power that is sometimes positive (consumption) and sometimes negative (production).
Unfortunately the sensor in domoticz, generated by the ShellyMQTT script (General kWh sensor - Energy Read Computed) always marks the value positive
That is, if I have shelly em which scores -100w, domoticz scores 100w.
This distorts the count because at the end of the day, the total is calculated as all consumption.
Has anyone already faced the problem?
User avatar
Sjonnie2017
Posts: 361
Joined: Wednesday 02 August 2017 19:43
Target OS: Linux
Domoticz version: Latest ß
Location: The Netherlands
Contact:

Re: Python Plugin: Shelly MQTT

Post by Sjonnie2017 »

Yup! I noticed the same behavior. In my case (reading power usage for a computer) it is no big deal. But it would be nice if this was corrected :)
ConBee II - TRÅDFRI lights + switches, loads of ChingLing dimmers and switches, Heiman and Xiaomi sensors
SolarEdge SE4000H (with active modbus_tcp)
YouLess Energy meter
Shelly 2.5 in roller shutter mode
amwr
Posts: 20
Joined: Wednesday 28 October 2015 17:48
Target OS: NAS (Synology & others)
Domoticz version:
Location: Switzerland
Contact:

Re: Python Plugin: Shelly MQTT

Post by amwr »

lwolf wrote: Saturday 04 February 2023 9:44 For simplicity i started to write a script (not a plugin!) shellyteacher4domo
Hi, thanks for the script. It took a while but I finally got it to work half-way. My issue seems to be that I have Plus 2PM and Pro3EM devices. For the Plus 2PM I just copied the Plus 1PM config, but of course, this way I'm not getting the whole configuration. Could anyone with more experience add these two devices?
AimoPaukku
Posts: 43
Joined: Saturday 28 January 2017 9:35
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Contact:

Re: Python Plugin: Shelly MQTT

Post by AimoPaukku »

lwolf wrote: Sunday 05 March 2023 14:39
Which plugin did you removed? MQTT AD is a hardware, not a plugin. If the device is appearing after Domoticz restart, than MQTT AD is working. Removal of MQTT AD will not help.

There are a "retain" setting in settings.py it defaults to True, which instructs the MQTT broker, (mosquitto i guess) to preserve its contents when rebooted. I am using this method since a year and my Shelly motion survived several reboots, and still working in Domoticz through MQTT AD, and I had registered it only one time.
I have this TRUE
You can check if the content is saved if you start "MQTT Explorer" (for example) and read this topic content before and after reboot:
"homeassistant/switch/shellyplus1pm-4855199d717c/config"

If retained messages not saved, than check your mosquitto config, as it is not configured correctly:
https://stackoverflow.com/questions/361 ... -goes-down

Thanks for reporting this, i will update documentation about MQTT persistence.
I think it will be the problem, as Domoticz tries to read config informations stored under "homeassistant/*" and if the mosquitto persistence does not work it will be empty, so Domoticz has no information about how to communicate...
My mosquetto will start after reboot.

I still have to start this manually and just after these, everything works again

Code: Select all

cd shellyteacher4domo
python3 shellyteacher4domo.py
lwolf
Posts: 216
Joined: Saturday 10 November 2018 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Hungary
Contact:

Re: Python Plugin: Shelly MQTT

Post by lwolf »

AimoPaukku wrote: Sunday 26 March 2023 14:20
lwolf wrote: Sunday 05 March 2023 14:39 There are a "retain" setting in settings.py it defaults to True,
I have this TRUE
Fine, than my part is working, you have to debug your mosquitto installation.
AimoPaukku wrote: Sunday 26 March 2023 14:20 My mosquetto will start after reboot.
Than please show your mosquitto.conf file, and check if mosquitto persistence file exists and can be accessed.
User avatar
jacobsentertainment
Posts: 211
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Python Plugin: Shelly MQTT

Post by jacobsentertainment »

Hi all,

Is there some support in this plugin for an shelly trv? Or is this still work in progress?
lwolf
Posts: 216
Joined: Saturday 10 November 2018 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Hungary
Contact:

Re: Python Plugin: Shelly MQTT

Post by lwolf »

amwr wrote: Sunday 26 March 2023 12:39 Hi, thanks for the script. It took a while but I finally got it to work half-way. My issue seems to be that I have Plus 2PM and Pro3EM devices. For the Plus 2PM I just copied the Plus 1PM config, but of course, this way I'm not getting the whole configuration. Could anyone with more experience add these two devices?
Plus 2PM is simple as it consists of 2 Input and 2 Switch device, so adding 1PM config and altering it is not a big deal. Although the Cover mode is a bit interesting and of course working otherwise than the old Shelly2... an experimantal example added the RPC payload has to work - in theory. I havent got a device for testing so be my guest. :) Plus 2PM support added in actual commit.

The Pro3EM is a completely different species of animal... the online Gen2 documentation is not very detailed, but i guess EMData lays in somewhere deeply hidden in "Notifications" area and with the current Domoticz MQTT AD implementation it will be impossible to distinct more than 1 channel data from a device. But its just guessing, if you can show some example MQTT payloads, i can tell. (MQTT Explorer is very good app for debugging)
rapinou
Posts: 3
Joined: Sunday 26 March 2023 17:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: Shelly MQTT

Post by rapinou »

lwolf wrote: Sunday 26 March 2023 16:52 Plus 2PM is simple as it consists of 2 Input and 2 Switch device, so adding 1PM config and altering it is not a big deal. Although the Cover mode is a bit interesting and of course working otherwise than the old Shelly2... an experimantal example added the RPC payload has to work - in theory. I havent got a device for testing so be my guest. :) Plus 2PM support added in actual commit.
Hi lwolf,
I have a Shelly Plus 2PM that I want to use in cover mode.
I tested your script which added the device to me in Domoticz in relay mode and not in cover mode.
Can I help you develop this part by giving you the necessary information?
rapinou
Posts: 3
Joined: Sunday 26 March 2023 17:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: Shelly MQTT

Post by rapinou »

Here are the Domotics logs for discovery if it helps.
2023-03-26 17:46:07 686 Error: MQTT Auto Discovery: MQTT_Discovery: Invalid/Unhandled data received! (Topic: homeassistant/cover/shellyplus2pm-5443b23d7858-roller-0/config, Message: {"name": "shellyplus2pm-5443b23d7858 Roller 0", "enabled_by_default": false, "cmd_t": "~rpc", "pl_open": "{\"id\":0,\"src\":\"domoticz\",\"method\":\"Cover Open\",\"params\":{\"id\":0}}", "pl_cls": "{\"id\":0,\"src\":\"domoticz\",\"method\":\"Cover Close\",\"params\":{\"id\":0}}", "pl_stop": "{\"id\":0,\"src\":\"domoticz\",\"method\":\"Cover Stop\",\"params\":{\"id\":0}}", "stat_t": "~status/cover:0", value_template: "{{ value_json state }}","state_closing": "closing","state_opening": "opening","state_open": "open","state_stopped": "stopped","state_closed": "closed","pos_t": "~status/cover:0", "pos_tpl": "{{ value_json current_pos }}","pos_open": 100, "pos_clsd": 0, "uniq_id": "shellyplus2pm-5443b23d7858-roller-0", "avty_t": "~online", "pl_avail": "true", "pl_not_avail": "false", "qos": 1, "~": "homeassistant/"})
lwolf
Posts: 216
Joined: Saturday 10 November 2018 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Hungary
Contact:

Re: Python Plugin: Shelly MQTT

Post by lwolf »

rapinou wrote: Sunday 26 March 2023 18:11 Here are the Domotics logs for discovery if it helps.
"~": "homeassistant/"
This line says that %shelly_topic% is equal with homeassistant which is not possible. It should be "shellyplus2pm-5443b23d7858/"
I have a bad feeling that you changed your Shelly device prefix to homeassistant instead of a proper unique prefix...
amwr
Posts: 20
Joined: Wednesday 28 October 2015 17:48
Target OS: NAS (Synology & others)
Domoticz version:
Location: Switzerland
Contact:

Re: Python Plugin: Shelly MQTT

Post by amwr »

lwolf wrote: Sunday 26 March 2023 16:52 The Pro3EM is a completely different species of animal... the online Gen2 documentation is not very detailed, but i guess EMData lays in somewhere deeply hidden in "Notifications" area and with the current Domoticz MQTT AD implementation it will be impossible to distinct more than 1 channel data from a device. But its just guessing, if you can show some example MQTT payloads, i can tell. (MQTT Explorer is very good app for debugging)
Thanks for the quick reaction! Here's some sample data from the Pro3EM:

Code: Select all

status/sys:
{"mac":"EC62608917D0","restart_required":false,"time":"17:49","unixtime":1679845785,"uptime":23,"ram_size":246900,"ram_free":125056,"fs_size":524288,"fs_free":172032,"cfg_rev":16,"kvs_rev":0,"webhook_rev":0,"available_updates":{}}

status/em:0
{"id":0,"a_current":0.029,"a_voltage":230.3,"a_act_power":2.0,"a_aprt_power":6.8,"a_pf":1.00,"b_current":0.027,"b_voltage":232.1,"b_act_power":0.2,"b_aprt_power":6.2,"b_pf":1.00,"c_current":1.870,"c_voltage":230.5,"c_act_power":324.7,"c_aprt_power":431.6,"c_pf":-0.81,"n_current":null,"total_current":1.926,"total_act_power":326.930,"total_aprt_power":444.520, "user_calibrated_phase":[],"errors": ["no_load"]}

status/emdata:0
{"id":0,"a_total_act_energy":22293.56,"a_total_act_ret_energy":17.92,"b_total_act_energy":2.76,"b_total_act_ret_energy":0.00,"c_total_act_energy":443743.43,"c_total_act_ret_energy":286.07,"total_act":466039.75, "total_act_ret":304.00}

events/rpc
{"src":"shellypro3em-ec62608917d0","dst":"xy/events","method":"NotifyStatus","params":{"ts":1679848559.06,"em:0":{"id":0,"a_act_power":1.8,"a_aprt_power":6.8,"a_current":0.029,"a_pf":1.00,"a_voltage":230.2,"b_act_power":-0.1,"b_aprt_power":6.4,"b_current":0.028,"b_pf":1.00,"b_voltage":232.0,"c_act_power":272.8,"c_aprt_power":381.9,"c_current":1.653,"c_pf":-0.79,"c_voltage":230.8,"n_current":null,"total_act_power":274.528,"total_aprt_power":395.079,"total_current":1.710}}}
lwolf
Posts: 216
Joined: Saturday 10 November 2018 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Hungary
Contact:

Re: Python Plugin: Shelly MQTT

Post by lwolf »

amwr wrote: Sunday 26 March 2023 18:39

Code: Select all

status/sys:
{"mac":"EC62608917D0","restart_required":false,"time":"17:49","unixtime":1679845785,"uptime":23,"ram_size":246900,"ram_free":125056,"fs_size":524288,"fs_free":172032,"cfg_rev":16,"kvs_rev":0,"webhook_rev":0,"available_updates":{}}

status/em:0
{"id":0,"a_current":0.029,"a_voltage":230.3,"a_act_power":2.0,"a_aprt_power":6.8,"a_pf":1.00,"b_current":0.027,"b_voltage":232.1,"b_act_power":0.2,"b_aprt_power":6.2,"b_pf":1.00,"c_current":1.870,"c_voltage":230.5,"c_act_power":324.7,"c_aprt_power":431.6,"c_pf":-0.81,"n_current":null,"total_current":1.926,"total_act_power":326.930,"total_aprt_power":444.520, "user_calibrated_phase":[],"errors": ["no_load"]}

status/emdata:0
{"id":0,"a_total_act_energy":22293.56,"a_total_act_ret_energy":17.92,"b_total_act_energy":2.76,"b_total_act_ret_energy":0.00,"c_total_act_energy":443743.43,"c_total_act_ret_energy":286.07,"total_act":466039.75, "total_act_ret":304.00}

events/rpc
{"src":"shellypro3em-ec62608917d0","dst":"xy/events","method":"NotifyStatus","params":{"ts":1679848559.06,"em:0":{"id":0,"a_act_power":1.8,"a_aprt_power":6.8,"a_current":0.029,"a_pf":1.00,"a_voltage":230.2,"b_act_power":-0.1,"b_aprt_power":6.4,"b_current":0.028,"b_pf":1.00,"b_voltage":232.0,"c_act_power":272.8,"c_aprt_power":381.9,"c_current":1.653,"c_pf":-0.79,"c_voltage":230.8,"n_current":null,"total_act_power":274.528,"total_aprt_power":395.079,"total_current":1.710}}}
Thanks the sample, its seems possible.. but PRO3 only has only 1 channel? Or em:1, em:2, emdata:1, emdata:2 are also exists?
amwr
Posts: 20
Joined: Wednesday 28 October 2015 17:48
Target OS: NAS (Synology & others)
Domoticz version:
Location: Switzerland
Contact:

Re: Python Plugin: Shelly MQTT

Post by amwr »

lwolf wrote: Sunday 26 March 2023 18:59
Thanks the sample, its seems possible.. but PRO3 only has only 1 channel? Or em:1, em:2, emdata:1, emdata:2 are also exists?
No, just em:0, etc.
You can see in em:0 that there are 3 measurements of each type, one for each phase a_, b_, c_.
lwolf
Posts: 216
Joined: Saturday 10 November 2018 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Hungary
Contact:

Re: Python Plugin: Shelly MQTT

Post by lwolf »

jacobsentertainment wrote: Sunday 26 March 2023 16:26 Is there some support in this plugin for an shelly trv? Or is this still work in progress?
I am sorry, but as i see the TRV returns its status in a nested structure:

Code: Select all

{
    "thermostats": [
        {
            "pos": -1,
            "target_t": {
                "enabled": true,
                "value": 24.0,
                "units": "C"
            },
            "tmp": {
                "value": 17.4,
                "units": "C",
                "is_valid": true
            },
            "schedule": false,
            "schedule_profile": 2,
            "boost_minutes": 0
        }
    ],
    "calibrated": false,
    "bat": {
        "value": 90,
        "voltage": 3.127
    },
    "charger": false,
    "ps_mode": 0,
    "dbg_flags": 0
}
In theory it should work, but i guess that Domoticz MQTT AD currently does not support such nesting which necessary:

"temperature_state_template": "{{ value_json.thermostats[0].target_t.value}}"
"current_temperature_template": "{{ value_json.thermostats[0].tmp.value }}"

At least i got these errors from Domoticz log:
Climate device unhandled temperature_state_template
Climate device unhandled current_temperature_template
lwolf
Posts: 216
Joined: Saturday 10 November 2018 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Hungary
Contact:

Re: Python Plugin: Shelly MQTT

Post by lwolf »

amwr wrote: Sunday 26 March 2023 19:10
lwolf wrote: Sunday 26 March 2023 18:59
Thanks the sample, its seems possible.. but PRO3 only has only 1 channel? Or em:1, em:2, emdata:1, emdata:2 are also exists?
No, just em:0, etc.
You can see in em:0 that there are 3 measurements of each type, one for each phase a_, b_, c_.
I see. Odd choice by Allterco, but can be processed.
amwr
Posts: 20
Joined: Wednesday 28 October 2015 17:48
Target OS: NAS (Synology & others)
Domoticz version:
Location: Switzerland
Contact:

Re: Python Plugin: Shelly MQTT

Post by amwr »

lwolf wrote: Sunday 26 March 2023 16:52 Plus 2PM is simple as it consists of 2 Input and 2 Switch device, so adding 1PM config and altering it is not a big deal. Although the Cover mode is a bit interesting and of course working otherwise than the old Shelly2... an experimantal example added the RPC payload has to work - in theory. I havent got a device for testing so be my guest. :) Plus 2PM support added in actual commit.
Thanks! Tried it, but I get this message:

Code: Select all

'utf-8' codec can't decode byte 0xb0 in position 1634: invalid start byte                                                                                  
---ERROR: GEN2 device  shellyplus2pm-b8d61a89a20c Shelly1 template not found  
Do you have an idea what the problem could be?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest