autoSteve / acMqtt

CBus Automation Controller: Home Assistant, MQTT, Philips Hue and more (for the SHAC/NAC/AC2/NAC2)
GNU General Public License v3.0
14 stars 6 forks source link

group application override results in duplicate entities in HA #11

Closed arrikhan closed 1 year ago

arrikhan commented 1 year ago

Hi,

When changing the default application (ie: light) via tags (One of light, fan, cover, select, sensor, switch, binary_sensor, bsensor or button), I end up with two entities per CBUS Group

eg:

light.cbus_mqtt_254_56_27 switch.cbus_mqtt_254_56_27

This exists for temperature sensors, fans, switches, covers.

The order in which I established connection was

  1. enable your scripts,
  2. enable MQTT keywords across all cbus groups (scripted)
  3. enable additional keywords for application and other elements (icons, primary name)

I was expected the discovery capability to manage this as I cannot delete the duplicate entities still with light. in HA as its controlled by discovery.

Additionally, I noticed when I updated CBUS names in CBUS Util, exported CBL file, deleted changed groups in SHAC object list and imported in replacement objects into SHAC, it didnt update in HA either. It still shows original name.

Is this a discovery issue?

I've tried deleting all objects in SHAC and reimporting clean CBL, applying keywords.. no change.

I'm running 5500SHAC HW: 5500SHAC (i.MX28) SW: 1.11.0

Home Assistant Home Assistant Core Installed version 2022.10.5

autoSteve commented 1 year ago

First issue:

To clean up the duplicates go grab a copy of MQTT Explorer (http://mqtt-explorer.com/) by Thomas Nordquist. Brilliant tool.

Connect to the broker, and open up the homeassistant topic. You'll see the lights/fans/covers etc., so select the cbusmqtt... topic you want to delete, and click the trash can on the right. This will result in duplicates being removed in HA.

Second issue: My code.

It may be possible for me to first check whether a topic (example cbus_mqtt_254_56_0) already exists in a different discovery topic. i.e. If there is an entry under light/ but I am adding a switch/ topic then first remove the light/ topic.

I'll have a look this weekend.

Thanks for the report!

autoSteve commented 1 year ago

Please validate @arrikhan. This script should both clean up existing duplicates, plus also prevent their creation.

autoSteve commented 1 year ago

@arrikhan wrote: "Additionally, I noticed when I updated CBUS names in CBUS Util, exported CBL file, deleted changed groups in SHAC object list and imported in replacement objects into SHAC, it didnt update in HA either. It still shows original name."

More information about this needed. Do you mean CGL file?

Is this to say that when objects are deleted in your SHAC that the discovery topics are not removed for HA? If so I cannot duplicate that. When I create a test object, then assign it the keyword MQTT it is presented as a discovery topic. Deleting the test object results in the script removing its discovery topic.

If deletion occurred, and then import very soon after then I'm not sure what the script would do. Check for changes is by default every 30 seconds. To avoid relying on check changes, delete the objects, disable/enable MQTT Send Receive, then import.

EDIT: Come to think of it, bulk deletion then immediate script restart will probably not remove the discovery topics. Remove them, wait for check changes to run, and then import CGL. Otherwise, use MQTT Explorer to clean up before the import.

arrikhan commented 1 year ago

When I delete the fan with fan tag in shac, it removes only the fan entity from HA. The light version is still there. I will do more testing.

autoSteve commented 1 year ago

You might need to clear all previous discovery out using MQTT Explorer.

arrikhan commented 1 year ago

Removing the MQTT flag with no application flag (ie: fan) removes the light entity. Changing the application flag after applying the MQTT flag is the problem. I can correct by removing the MQTT flag whilst the appropriate application flag is present (either default of nothing for light or specific one such as fan).

arrikhan commented 1 year ago

I do not seem to be able to subscribe to the homeassistant topic. I can see all my cbus topics by subscribing to cbus/# which essentially displays all. Maybe this has something to do with it.

autoSteve commented 1 year ago

do not seem to be able to subscribe to the homeassistant topic. I can see all my cbus topics by subscribing to cbus/# which essentially displays all. Maybe this has something to do with it.

In MQTT Explorer? Subscribe to #, which is the top level topic (and default) and you will see everything.

autoSteve commented 1 year ago

If you can't manage to clean up in MQTT Explorer, then insert the following code at line 822. Save. Disable/enable. Then add the MQTT tag to one of your duplicates, wait for addition, then remove the tag and wait for deletion. To speed things up set checkChanges to a value less than 30 seconds, but greater than or equal to 3. Set it back to 30 when you're done to lower CPU utilisation

-- Brute force removal of all types for @aarikhan client:publish(mqttDiscoveryTopic..'light/'..v.oid..'/config', 'junk', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'switch/'..v.oid..'/config', 'junk', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'fan/'..v.oid..'/config', 'junk', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'cover/'..v.oid..'/config', 'junk', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'select/'..v.oid..'/config', 'junk', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'sensor/'..v.oid..'/config', 'junk', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'switch/'..v.oid..'/config', 'junk', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'binary_sensor/'..v.oid..'/config', 'junk', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'button/'..v.oid..'/config', 'junk', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'light/'..v.oid..'/config', '', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'switch/'..v.oid..'/config', '', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'fan/'..v.oid..'/config', '', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'cover/'..v.oid..'/config', '', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'select/'..v.oid..'/config', '', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'sensor/'..v.oid..'/config', '', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'switch/'..v.oid..'/config', '', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'binary_sensor/'..v.oid..'/config', '', mqttQoS, RETAIN) client:publish(mqttDiscoveryTopic..'button/'..v.oid..'/config', '', mqttQoS, RETAIN)

i.e. After the lines:

topic = mqttDiscoveryTopic..v.type..'/'..v.oid..'/config' client:publish(topic, '', mqttQoS, RETAIN); logger('Removed discovery topic '..topic)

arrikhan commented 1 year ago

Subscribed via mqtt-explorer successfully, deleted duplicates and restarting SHAC script cleaned up HA.

Thanks for looking into this.