stas-demydiuk / domoticz-zigbee2mqtt-plugin

zigbee2mqtt plugin for domoticz
MIT License
136 stars 98 forks source link

Kwh meter for power switch not working properly. #605

Closed lwalbert closed 3 years ago

lwalbert commented 3 years ago

Issue description Due to recent changes in Domoticz the sValue now requires two values separated by ";" eg. sValue = power;energy I made a workaround in devices/sensor/kwh.py by adding the missing value to the return string when only one value in array. Also found that the value_key array doesn't work, or not holding the correct variable/string for getting power and energy reading, or it might be the for loop that doesn't work correctly. Any way, after several tries modifying for loop etc, I still couldn't get it to work, so made a workaround to get power and energy from device. My device is Develco SPLZB-131 power plug.

Attached code is the modified code that works for power and energy return in sValue.

import Domoticz
from devices.device import Device

class KwhSensor(Device):
    def __init__(self, devices, alias, value_key, device_name_suffix=''):
        super().__init__(devices, alias, ';'.join(value_key), device_name_suffix)
        self.value_keys = ['power', 'energy'] #value_key Added static keys for array
        self.energy_multiplier = 1000

    def create_device(self, unit, device_id, device_name):
        options = {}
        options['EnergyMeterMode'] = '1'

        return Domoticz.Device(Unit=unit, DeviceID=device_id, Name=device_name, TypeName="kWh", Options=options).Create()

    def get_message_value(self, message):
        value = {} #[] #added

        for item in self.value_keys:
            if item in message.raw: # removed item [not]
                #return None
                value[item] = message.raw[item] #added
            #value.append(message.raw[item])

        return value if len(value) > 0 else None

    def get_numeric_value(self, value, device):
        return 0

    def get_string_value(self, value, device):
        if 'energy' in value: #len(value) == 2: added static comparison due to changes in for loop
            return str(value['power']) + ";" + str(value['energy'] * self.energy_multiplier)
            #return str(value[0]) + ";" + str(value[1] * self.energy_multiplier)
        else:
            return str(value['power']) + ";" + str(0) #Added required 2´nd value to domoticz
            #return str(value[0])

Additional information Zigbee2MQTT version: v.1.18.1 (ConBee2/RaspBee2 0x264a0700) Python version: 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0] Domoticz version: V2020.2 build 13091 Plugin version: 3.0 (latest)

Could you please check and fix :) thanks

br lwalbert

mvzut commented 3 years ago

I have the same issue after my latest Domoticz beta update. The Zigbee device is a TS0121_plug, which suddenly stopped displaying the actual power usage. I tried the code of lwalber, this initially results in some updates of the power devices, but after a while it starts giving error messages:

2021-03-20 15:46:13.721 Error: Python Plugin System: (Zigbee2MQTT) 'onMessage' failed 'SystemError'.
2021-03-20 15:46:13.721 Error: Python Plugin System: (Zigbee2MQTT) ----> Line 293 in '/home/pi/domoticz/plugins/zigbee2mqtt/plugin.py', function onMessage
2021-03-20 15:46:13.721 Error: Python Plugin System: (Zigbee2MQTT) ----> Line 138 in '/home/pi/domoticz/plugins/zigbee2mqtt/plugin.py', function onMessage
2021-03-20 15:46:13.721 Error: Python Plugin System: (Zigbee2MQTT) ----> Line 167 in '/home/pi/domoticz/plugins/zigbee2mqtt/mqtt.py', function onMessage
2021-03-20 15:46:13.722 Error: Python Plugin System: (Zigbee2MQTT) ----> Line 213 in '/home/pi/domoticz/plugins/zigbee2mqtt/plugin.py', function onMQTTPublish
2021-03-20 15:46:13.722 Error: Python Plugin System: (Zigbee2MQTT) ----> Line 63 in '/home/pi/domoticz/plugins/zigbee2mqtt/devices_manager.py', function handle_mqtt_message
2021-03-20 15:46:13.722 Error: Python Plugin System: (Zigbee2MQTT) ----> Line 64 in '/home/pi/domoticz/plugins/zigbee2mqtt/adapters/base_adapter.py', function handle_mqtt_message
2021-03-20 15:46:13.722 Error: Python Plugin System: (Zigbee2MQTT) ----> Line 134 in '/home/pi/domoticz/plugins/zigbee2mqtt/devices/device.py', function handle_message
2021-03-20 15:46:13.722 Error: Python Plugin System: (Zigbee2MQTT) ----> Line 64 in '/home/pi/domoticz/plugins/zigbee2mqtt/devices/device.py', function update_device

Would be nice if this was fixed!

regards, mzut

P.S. I have the same software/plugin versions as lwalbert, for hardware I use a cc2531.

lwalbert commented 3 years ago

Hi @mvzut

can you try the workaround code again?

  1. first Stop the plugin in Domoticz
  2. update the kwh.py file
  3. flush python cache by issuing the following command from zigbee2mqtt folder: find . | grep -E "(pycache|.pyc|.pyo$)" | xargs rm -rf
  4. then restart the plugin and see if it keeps running or crashing.

It might be that I have modified some other files too , I'll re-check and revert back if I find other modifications.

Edit: Checked my code, all other modifications where reverted back. So the only modded file is the kwh.py

Edit 2: in Domoticz select Energy read:from device As there is another bug in Domoticz causing the Kwh metering object to toggle if Comuted selected.

Skjermbilde 2021-03-20 kl  19 09 44

Br lwalbert

mvzut commented 3 years ago

Hi,

Thanks for your help. The command gives a permission error, even if I run it with sudo. The "From device" option was already selected as default for the two devices it concerns. So unfortunately the error messages remain with you code changes. Any further help would be appreciated!

stas-demydiuk commented 3 years ago

Could you please update plugin and check now. Now the plugin should automatically choose "From device" option if the device supports energy stats or "Computed" in another case.

For devices that were created before the update, you need to do it manually or remove such devices from Domoticz so the plugin could recreate them with proper settings.

lwalbert commented 3 years ago

Hi @stas-demydiuk,

Great work :) Updated plugin, removed devices and let the plugin recreate. Works well here:) Thanks a lot for fixing the issue.

br

lwalbert

mvzut commented 3 years ago

For me it doesn't work unfortunately. I removed all devices related to the two TS0121_plug devices, stopped Domoticz, update the plugin with git pull in the plugin folder, and started Domoticz again. The newly created kWh devices still don't show the current usage (in Watts). In the Devices screen, they show "1, 10" under Data, this never seems to change: Naamloos In the Zigbee2mqtt logs, the MQTT messages seem ok: Naamloos 2

stas-demydiuk commented 3 years ago

Do you have any such issues with other devices and are there any errors in log?

mvzut commented 3 years ago

I do not have any other Zigbee devices which report power. I do have a P1 smart meter and the Enphase plugin, which both report power, and these devices still work fine. There are no errors in the logs related to this. Only the occasional ones from my Harmony Hub (sometimes has connection issues), and a Netatmo sensor that is not giving updates.

Note that it worked perfectly until my last Domoticz beta update. Oh and I also updated zigbee2mqtt yesterday.

mvzut commented 3 years ago

This is how the sensor shows up in Zigbee2MQTT's own dashboard: Naamloos 2 Naamloos

stas-demydiuk commented 3 years ago

Yes, I have the same TS0121_plug device and it works just fine, so the issue is most likely in the environment setup. Are you using the latest Domoticz beta version?

mvzut commented 3 years ago

Yes latest beta. By the way, why do the devices use "kWh" as SubType? My other devices which monitor power (smart meter, solar panels) have "Energy" as SubType.

mvzut commented 3 years ago

Your question about the latest beta made me doubt, so I checked again and found out that my Domoticz beta was 13091 so not the latest one of today (13102). So I updated again, and I briefly saw one of the devices update to "7 Watt"! But then Domoticz crashed. Just found out this is a known issue (see https://www.domoticz.com/forum/viewtopic.php?f=6&t=35855&p=271494#p271494). I reverted to my previous build (13091) and it is stable again, but without updates of these two sensor devices..

lwalbert commented 3 years ago

Hi @mvzut, Did you by any chance rename your main devices 'unfriendly name' to a 'friendly' from within Domoticz?

Skjermbilde 2021-03-21 kl  21 57 54

in that case: I just did that, and that caused the fault messages similar to the ones you got and the device did not respond any more. To solve this you'll have to restart the zigbee2mqtt service to start publish on the new name. sudo service zigbee2mqtt restart

Hope this solves your issue.

Br lwalbert

mvzut commented 3 years ago

Hi @lwalbert,

I do change the name when adding the device to the active devices. But that shouldn't make a difference. I tried not changing the name after adding, this doesn't help.

I do not understand why the devices always show "1, 10" in their "Data" field. This data is apparently not valid, so that the meter value is empty on the Utility tab.

I can try removing and adding them in Zigbee2MQTT. Perhaps that helps...

awalsum commented 3 years ago

I'm having exactly the same problem since updating this morning. Zigbee2mqtt was causing Domoticz to crash completely. After hours of trying to find the reason, I found it to be Zigbee2Mqtt. I updated everything, am on the latest Domoticz beta and did a git pull in Zigbee2Mqtt. The only way to get everything working again was to add Zigbee2Mqtt as new hardware. That created all new devices and now I had to add them all to my floorplans and scripts etc.

The only thing still not working is the power display. Any news on this? I desperately need this because some of my scripts run using power levels of some devices.

I have some wifi smart plugs (TP-link HS-110) and two P1 meters and they all work without issue.

lwalbert commented 3 years ago

For me it doesn't work unfortunately. I removed all devices related to the two TS0121_plug devices, stopped Domoticz, update the plugin with git pull in the plugin folder, and started Domoticz again. The newly created kWh devices still don't show the current usage (in Watts). In the Devices screen, they show "1, 10" under Data, this never seems to change: Naamloos In the Zigbee2mqtt logs, the MQTT messages seem ok: Naamloos 2

After playing around alot yesterday with deleting devices, pairing etc. I ended up with similar device for power. The power objects seems to be incorrectly included somehow and did not show up in Meter in database. the power device object seems to be included with a different type of general /kWh type.

The only way to recover was to delete zigbee2mqtt plugin folder, do a new git clone, delete domoticz database, and install the hardware device again. Not shure if its the zigbee2mqtt plugin or if its domoticz itselves that did this.

I'll see if I manage to recreate this later today/evening.

lwalbert commented 3 years ago

For me it doesn't work unfortunately. I removed all devices related to the two TS0121_plug devices, stopped Domoticz, update the plugin with git pull in the plugin folder, and started Domoticz again. The newly created kWh devices still don't show the current usage (in Watts). In the Devices screen, they show "1, 10" under Data, this never seems to change: Naamloos In the Zigbee2mqtt logs, the MQTT messages seem ok: Naamloos 2

After playing around alot yesterday with deleting devices, pairing etc. I ended up with similar device for power. The power objects seems to be incorrectly included somehow and did not show up in Meter in database. the power device object seems to be included with a different type of general /kWh type.

The only way to recover was to delete zigbee2mqtt plugin folder, do a new git clone, delete domoticz database, and install the hardware device again. Not shure if its the zigbee2mqtt plugin or if its domoticz itselves that did this.

I'll see if I manage to recreate this later today/evening.

Hi all, I have not been able to recreate this problem when running latest beta version of domoticz. So if you still experience issues you should update domoticz. The issues with kwh meters has been solved now in latest beta version (13117)

br lwalbert

awalsum commented 3 years ago

I updated to latest beta Domoticz (13119), but that didn't solve it for me. I still can't get any power readings on the plugs. When I check the mqtt flow, they are showing there. It must be something in the plugin for Domoticz.

I wanted to see if it had anything to do with devices already in use not being updated with the latest beta, so I got a brand new smart plug out of the box and paired it to Zigbee2Mqtt. I uncovered a new issue when doing this. I am able to set a switch command to "on" and the plug will physically switch, but the status in Domoticz remains "off". When I issue a new command to switch it off, it won't send that command because Domoticz thinks the plug is still off. It sends a new "on" command. Whe you follow Mqtt traffic, you can see that the plug is actually on. After physically switching the plug off with the button on the side, you can see in the traffic that the plug reports it is "off".

mvzut commented 3 years ago

I upgraded Domoticz but the problem persists. Both my TS0121 plugs still only show "1,10" as data for the corresponding Domoticz power devices. The other devices that are created (volt, ampere) work fine. I am almost tempted to create a calculated virtual device for power. But surely that shouldn't be necessary? As mentioned before, the MQTT messages contain the correct power.

lwalbert commented 3 years ago

Hi @mvzut

there might be incorrect values in database. try with this url from your browser to update the sValue: replace the <domoticz ip: domoticz port> with your domoticz ip and port. replace the <device id> with your idx device id. also make sure you have selected energy read from device. http://<domoticz ip: domoticz port>/json.htm?type=command&param=udevice&idx=<device id>&nvalue=0&svalue=0;0

the browser should return:

{
    "status" : "OK",
    "title" : "Update Device"
}
awalsum commented 3 years ago

Hi @mvzut

there might be incorrect values in database. try with this url from your browser to update the sValue: replace the <domoticz ip: domoticz port> with your domoticz ip and port. replace the <device id> with your idx device id. also make sure you have selected energy read from device. http://<domoticz ip: domoticz port>/json.htm?type=command&param=udevice&idx=<device id>&nvalue=0&svalue=0;0

the browser should return:

{
  "status" : "OK",
  "title" : "Update Device"
}

That worked for me. So how would I go about fixing it in Domoticz?

lwalbert commented 3 years ago

hi @awalsum, the url where supposed to "kick-start" the kwh meter in domoticz if stucked with incorrect value in database. you don't see any live data in domoticz after issuing the json?

awalsum commented 3 years ago

No, I still have 1,20.

mvzut commented 3 years ago

After doing this, I see "0.000 kWh" for a few seconds, but then it goes to "1, 10" again...

lwalbert commented 3 years ago

Strange, have you tried to only delete the kwh device, then stop the zigbee2mqtt plugin in domoticz hardware and restart it?

lwalbert commented 3 years ago

hi,

Probably you need to push reconfigure button in zigbee2mqtt frontend for your device:

Skjermbilde 2021-03-27 kl  12 40 24

if that doesn't work: Try editing the kwh.py file in: domoticz/plugins/zigbee2mqtt/devices/sensor/kwh.py

add +";0" at the last line in else statement return.

so it looks like this:

if len(value) == 2:
            return str(value[0]) + str(int(value[1] * self.energy_multiplier))
        else:
            return str(value[0]) + ";0"

The latest beta domoticz has a check function for a valid value in this format "0;0" If your device for some reason only return one valid value, domoticz ignores it.

mvzut commented 3 years ago

Thanks for your support! But I'm afraid we're still not there... Reconfiguring had no effect, and the change in kwh.py also makes no difference unfortunately.

More things I can try? Perhaps put a few Domoticz.Log(...) lines at tactical locations in the code?

awalsum commented 3 years ago

I tried modifying the kwh.py file too, but it isn't working for me either. The strange thing is, I have 11 of these plugs, and some work while others don't. I can't see any differences in the configuration of them. The latest plug that I paired also won't give me kwh readings, but it isn't reporting volt or ampere either. There is no reporting at all. I can send a switch command to switch it on, but the state is not reporting back to Domoticz. That leads Domoticz to believe the plug is in the off position, making it impossible to ever turn it off again. The raw traffic shows the plug is working, however. It is definitely a Domoticz issue. What I don't understand is why some are working and others aren't.

It has to have something to do with the TS-0121 plug, because I have Aqara temp & humidity sensors which are working fine and also a Eurotronic thermostat valve which is working.

Btw, how do you get that frontend page?

awalsum commented 3 years ago

i also deleted the power devices which weren't working and when they automatically popped back up, there was no change. That didn't help it.

awalsum commented 3 years ago

i have just found some differences in the MQTT traffic. The plugs which are displaying properly do NOT report "power_outage_memory".

afbeelding

lwalbert commented 3 years ago

Add to frontend and experimental to /opt/zigbee2mqtt/data/configuration.yaml gives you a web interface.

homeassistant: false
permit_join: true
frontend:
      port: 8081
experimental:
      new_api: true
awalsum commented 3 years ago

Reconfiguring "Kelder ventilatie" through the frontend solved the problems with this plug. All values are updated now and I am able to switch it on and off. Not so much luck with the other plugs however.

I'm not at home at the moment so can't physically reach the plugs. I'm going to try and delete the plug that don't work completely from Domoticz and re-pair them, probably tomorrow or Monday. See if that fixes them.

mvzut commented 3 years ago

Reconfiguring "Kelder ventilatie" through the frontend solved the problems with this plug. All values are updated now and I am able to switch it on and off. Not so much luck with the other plugs however.

I'm not at home at the moment so can't physically reach the plugs. I'm going to try and delete the plug that don't work completely from Domoticz and re-pair them, probably tomorrow or Monday. See if that fixes them.

Please let me know if re-paring worked, if so I will also try that.

awalsum commented 3 years ago

Pairing another new plug worked too. After pairing I did, however, have to press reconfigure in the Frontend for it to work. That wasn't the case before all these problems, but if it works, Im fine with that. I have yet to try a used plug that isn't working for me now. I will probably try that tomorrow.

mvzut commented 3 years ago

I removed the two plugs from Zigbee2MQTT (via Domoticz), re-paired them, reconfigured them in Zigbee2MQTT, restarted both Zigbee2MQTT and Domoticz, and suddenly I have power readings again! No idea if all these steps were necessary and what did the trick eventually. But I'm very happy that it works again. Fingers crossed if it stays like this...

mvzut commented 3 years ago

I have been experimenting a bit more, and like @awalsum suggested, the problem is probably related to the "power_outage_memory" field in the MQTT messages! After re-pairing my plugs these fields are not present in the messages anymore, whereas these were previously there (see my earlier screenshot of the log).

I actually need this setting, one of the plugs is monitoring the energy use of my aquarium, so this plug should always go on after a power outage. Therefore I set this memory setting to "On" again via the Zigbee2MQTT web configuration, but immediately afterwards the power readings stopped again in Domoticz!

Can it be that the plugin checks for the MQTT field with the word "power" in the title, but then accidentally finds the "power_outage_memory" field? That would explain why I got weird values like "1, 10".

awalsum commented 3 years ago

Pairing another new plug worked too. After pairing I did, however, have to press reconfigure in the Frontend for it to work. That wasn't the case before all these problems, but if it works, Im fine with that. I have yet to try a used plug that isn't working for me now. I will probably try that tomorrow.

As I said, I would try and pair a used plug. You need to remove all the devices relating to the plug from your device list first. Then remove the plug out of the Zigbee2Mqtt list. After doing that, I allowed new devices to join and paired the plug. You need to assign a new friendly name and go to the Zigbee2Mqtt Frontend and press reconfigure for the paired plug. After that, everything works again.

SargonofAssyria commented 3 years ago

Sorry, opened a new issue: https://github.com/stas-demydiuk/domoticz-zigbee2mqtt-plugin/issues/614 This seems very much related.

sammyke007 commented 2 years ago

How was this fixed? I'm still getting the 1,10 and 1,30 values indeed because I enabled a power outage option...

sammyke007 commented 2 years ago

I fixed it for now with blacklisting/disabling the power_outage_memory parameter in each device with the filtered_attributes in Z2M frontend, but I guess that's not the right way to do it? The plugin seems to read the "power_outage_memory" parameter instead of the "power" parameter.