enesbcs / shellyteacher4domo

Shelly device teacher for Domoticz MQTT Autodiscovery hardware
Apache License 2.0
10 stars 4 forks source link

Shelly EM support #5

Closed tbart closed 1 year ago

tbart commented 1 year ago

Hi! First off, thanks for implementing this (and all the other IoT stuff), much appreciated!

I have a Shelly EM here which is not yet supported by the teacher. I tried copying over the Shelly 1 PM template and renaming to SHEM. This basically makes the relay and overtemperature sensor available, so partial success. But toggling the relay does not work (neither do I see a change in domoticz when I switch in the Shelly's webUI nor the other way round, switching in domoticz does not really switch the relay)

However, I'd (naturally) like to also add power measurement support.

How do these templates get created? What needs to mapped where?

The following is a mosquitto_sub command for all subtopics of this device generated with mosquitto_sub -h mosquitto_server -t "shellies/shellyem-123456789012/#" -F %J

Error: Message payload is not valid JSON on topic shellies/shellyem-34945473D3B2/relay/0.

{"tst":"2023-03-11T16:43:19.953152+0100","topic":"shellies/shellyem-123456789012/emeter/0/power","qos":0,"retain":0,"payloadlen":6,"payload":611.53}
{"tst":"2023-03-11T16:43:19.953278+0100","topic":"shellies/shellyem-123456789012/emeter/0/reactive_power","qos":0,"retain":0,"payloadlen":6,"payload":382.27}
{"tst":"2023-03-11T16:43:19.953337+0100","topic":"shellies/shellyem-123456789012/emeter/0/voltage","qos":0,"retain":0,"payloadlen":6,"payload":238.85}
{"tst":"2023-03-11T16:43:19.953388+0100","topic":"shellies/shellyem-123456789012/emeter/0/total","qos":0,"retain":0,"payloadlen":6,"payload":1891.9}
{"tst":"2023-03-11T16:43:19.953438+0100","topic":"shellies/shellyem-123456789012/emeter/0/total_returned","qos":0,"retain":0,"payloadlen":3,"payload":0}
{"tst":"2023-03-11T16:43:19.953482+0100","topic":"shellies/shellyem-123456789012/emeter/1/power","qos":0,"retain":0,"payloadlen":4,"payload":0}
{"tst":"2023-03-11T16:43:19.953528+0100","topic":"shellies/shellyem-123456789012/emeter/1/reactive_power","qos":0,"retain":0,"payloadlen":4,"payload":0}
{"tst":"2023-03-11T16:43:19.953576+0100","topic":"shellies/shellyem-123456789012/emeter/1/voltage","qos":0,"retain":0,"payloadlen":6,"payload":238.85}
{"tst":"2023-03-11T16:43:19.953652+0100","topic":"shellies/shellyem-123456789012/emeter/1/total","qos":0,"retain":0,"payloadlen":3,"payload":0}
{"tst":"2023-03-11T16:43:19.953703+0100","topic":"shellies/shellyem-123456789012/emeter/1/total_returned","qos":0,"retain":0,"payloadlen":3,"payload":0}
{"tst":"2023-03-11T16:43:33.775189+0100","topic":"shellies/shellyem-123456789012/emeter/0/energy","qos":0,"retain":0,"payloadlen":3,"payload":612}
{"tst":"2023-03-11T16:43:33.777174+0100","topic":"shellies/shellyem-123456789012/emeter/0/returned_energy","qos":0,"retain":0,"payloadlen":1,"payload":0}
{"tst":"2023-03-11T16:43:33.777264+0100","topic":"shellies/shellyem-123456789012/emeter/1/energy","qos":0,"retain":0,"payloadlen":1,"payload":0}
{"tst":"2023-03-11T16:43:33.777304+0100","topic":"shellies/shellyem-123456789012/emeter/1/returned_energy","qos":0,"retain":0,"payloadlen":1,"payload":0}
Error: Message payload is not valid JSON on topic shellies/shellyem-34945473D3B2/relay/0.

{"tst":"2023-03-11T16:43:49.930766+0100","topic":"shellies/shellyem-123456789012/emeter/0/power","qos":0,"retain":0,"payloadlen":6,"payload":609.46}
{"tst":"2023-03-11T16:43:49.930859+0100","topic":"shellies/shellyem-123456789012/emeter/0/reactive_power","qos":0,"retain":0,"payloadlen":6,"payload":379.26}
{"tst":"2023-03-11T16:43:49.930897+0100","topic":"shellies/shellyem-123456789012/emeter/0/voltage","qos":0,"retain":0,"payloadlen":6,"payload":238.19}
{"tst":"2023-03-11T16:43:49.930929+0100","topic":"shellies/shellyem-123456789012/emeter/0/total","qos":0,"retain":0,"payloadlen":6,"payload":1902.2}
{"tst":"2023-03-11T16:43:49.930981+0100","topic":"shellies/shellyem-123456789012/emeter/0/total_returned","qos":0,"retain":0,"payloadlen":3,"payload":0}
{"tst":"2023-03-11T16:43:49.931023+0100","topic":"shellies/shellyem-123456789012/emeter/1/power","qos":0,"retain":0,"payloadlen":4,"payload":0}
{"tst":"2023-03-11T16:43:49.931058+0100","topic":"shellies/shellyem-123456789012/emeter/1/reactive_power","qos":0,"retain":0,"payloadlen":4,"payload":0}
{"tst":"2023-03-11T16:43:49.931099+0100","topic":"shellies/shellyem-123456789012/emeter/1/voltage","qos":0,"retain":0,"payloadlen":6,"payload":238.19}
{"tst":"2023-03-11T16:43:49.931141+0100","topic":"shellies/shellyem-123456789012/emeter/1/total","qos":0,"retain":0,"payloadlen":3,"payload":0}
{"tst":"2023-03-11T16:43:49.931181+0100","topic":"shellies/shellyem-123456789012/emeter/1/total_returned","qos":0,"retain":0,"payloadlen":3,"payload":0}

Here is a nicer formatted json_pp version of the same:

{
   "payload" : 611.53,
   "payloadlen" : 6,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/0/power",
   "tst" : "2023-03-11T16:43:19.953152+0100"
}
{
   "payload" : 382.27,
   "payloadlen" : 6,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/0/reactive_power",
   "tst" : "2023-03-11T16:43:19.953278+0100"
}
{
   "payload" : 238.85,
   "payloadlen" : 6,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/0/voltage",
   "tst" : "2023-03-11T16:43:19.953337+0100"
}
{
   "payload" : 1891.9,
   "payloadlen" : 6,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/0/total",
   "tst" : "2023-03-11T16:43:19.953388+0100"
}
{
   "payload" : 0,
   "payloadlen" : 3,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/0/total_returned",
   "tst" : "2023-03-11T16:43:19.953438+0100"
}
{
   "payload" : 0,
   "payloadlen" : 4,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/1/power",
   "tst" : "2023-03-11T16:43:19.953482+0100"
}
{
   "payload" : 0,
   "payloadlen" : 4,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/1/reactive_power",
   "tst" : "2023-03-11T16:43:19.953528+0100"
}
{
   "payload" : 238.85,
   "payloadlen" : 6,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/1/voltage",
   "tst" : "2023-03-11T16:43:19.953576+0100"
}
{
   "payload" : 0,
   "payloadlen" : 3,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/1/total",
   "tst" : "2023-03-11T16:43:19.953652+0100"
}
{
   "payload" : 0,
   "payloadlen" : 3,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/1/total_returned",
   "tst" : "2023-03-11T16:43:19.953703+0100"
}
{
   "payload" : 612,
   "payloadlen" : 3,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/0/energy",
   "tst" : "2023-03-11T16:43:33.775189+0100"
}
{
   "payload" : 0,
   "payloadlen" : 1,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/0/returned_energy",
   "tst" : "2023-03-11T16:43:33.777174+0100"
}
{
   "payload" : 0,
   "payloadlen" : 1,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/1/energy",
   "tst" : "2023-03-11T16:43:33.777264+0100"
}
{
   "payload" : 0,
   "payloadlen" : 1,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/1/returned_energy",
   "tst" : "2023-03-11T16:43:33.777304+0100"
}
{
   "payload" : 609.46,
   "payloadlen" : 6,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/0/power",
   "tst" : "2023-03-11T16:43:49.930766+0100"
}
{
   "payload" : 379.26,
   "payloadlen" : 6,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/0/reactive_power",
   "tst" : "2023-03-11T16:43:49.930859+0100"
}
{
   "payload" : 238.19,
   "payloadlen" : 6,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/0/voltage",
   "tst" : "2023-03-11T16:43:49.930897+0100"
}
{
   "payload" : 1902.2,
   "payloadlen" : 6,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/0/total",
   "tst" : "2023-03-11T16:43:49.930929+0100"
}
{
   "payload" : 0,
   "payloadlen" : 3,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/0/total_returned",
   "tst" : "2023-03-11T16:43:49.930981+0100"
}
{
   "payload" : 0,
   "payloadlen" : 4,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/1/power",
   "tst" : "2023-03-11T16:43:49.931023+0100"
}
{
   "payload" : 0,
   "payloadlen" : 4,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/1/reactive_power",
   "tst" : "2023-03-11T16:43:49.931058+0100"
}
{
   "payload" : 238.19,
   "payloadlen" : 6,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/1/voltage",
   "tst" : "2023-03-11T16:43:49.931099+0100"
}
{
   "payload" : 0,
   "payloadlen" : 3,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/1/total",
   "tst" : "2023-03-11T16:43:49.931141+0100"
}
{
   "payload" : 0,
   "payloadlen" : 3,
   "qos" : 0,
   "retain" : 0,
   "topic" : "shellies/shellyem-123456789012/emeter/1/total_returned",
   "tst" : "2023-03-11T16:43:49.931181+0100"
}

Is flashing tasmoto onto the Shelly a better solution? Your note "Energy reporting will not be good, until Domoticz AD implements basic math in templates" makes me doubt whether this will really have the benefit I strive for.. Shelly's own app however shows the same total_returned as MQTT, I think that should suffice, if Domoticz just logs these values.

Or is there a certain cause you have explicitly not implemented support for it yet?

Thanks in advance for any help provided!

enesbcs commented 1 year ago

But toggling the relay does not work (neither do I see a change in domoticz when I switch in the Shelly's webUI nor the other way round, switching in domoticz does not really switch the relay)

Based on this error message: Error: Message payload is not valid JSON on topic shellies/shellyem-34945473D3B2/relay/0 your payload is invalid, either because an incorrect semicolon, { " \ } , or line ending, cant tell if i cant see.

How do these templates get created? What needs to mapped where?

Home Assistant Discovery MQTT messages are documented here: https://www.home-assistant.io/integrations/mqtt/ (but it have a long-long learning curve, this is the reason why this script exists)

These keywords will be replaced with the device actual data at runtime: %discovery_prefix% %shelly_id% %shelly_model% %shelly_mac% %shelly_ip% %shelly_mode%

Is flashing tasmoto onto the Shelly a better solution? Your note "Energy reporting will not be good, until Domoticz AD implements basic math in templates" makes me doubt whether this will really have the benefit I strive for.. Shelly's own app however shows the same total_returned as MQTT, I think that should suffice, if Domoticz just logs these values.

I do not see any reason to flash a Shelly device with Tasmota. Consult with Domoticz forum first: https://www.domoticz.com/forum/viewtopic.php?t=39925

Yep when i wrote "energy reporting" i mean it. Energy is reported in watt-minute for some reason and Domoticz has a Wh sensor... See https://github.com/enesbcs/shellyteacher4domo/issues/6 "total_returned" is reported in Watt-hour so conversion not needed and it will work without problem.

Or is there a certain cause you have explicitly not implemented support for it yet?

Energy fields is implemented in devices, just defaulted as disabled see https://github.com/enesbcs/shellyteacher4domo/issues/6 If you mean why Shelly EM templates not created yet, the reason is very simple: i dont have a Shelly EM. But based on your MQTT examples i can add support it.

enesbcs commented 1 year ago

Shelly EM templates added at commit https://github.com/enesbcs/shellyteacher4domo/commit/4e3ef837a3b6036893586fb8bb40583559972e4d

tbart commented 1 year ago

Thank you for your extremely quick response, much appreciated. I haven't been so quick (ShellyEM not at my house and COVID struck again), so sorry for the delay.

However, I can confirm the teacher taught Domoticz very well! It seems all metrics got integrated into Domoticz. The relay also works. It's only been a few minutes that data is coming in, so only a first impression. But voltage and resistive power seem to make sense. We see -140W of reactive power (on an idling heat pump), I am unsure what to make of this value (I have an EE background, so yeah, if there is some power supply and frequency transformation going on, it's pretty plausible voltage and current are not in phase, but still, it seems a little high). I guess however these are just the Shelly's values and your tool is not to blame.

So I'd say this issue can be closed if you don't want me to test anything more. The wrong JSON for the relay we talked about above does not seem to be an issue.

enesbcs commented 1 year ago

Thank you for testing, closing issue!