roleoroleo / yi-hack_ha_integration

Home Assistant custom integration for Yi cameras: yi-hack-MStar, yi-hack-Allwinner, yi-hack-Allwinner-v2, yi-hack-v5 and sonoff-hack
GNU General Public License v3.0
224 stars 32 forks source link

Correct configuration procedure for MQTT and multiple cameras #91

Closed maxromanovsky closed 4 months ago

maxromanovsky commented 2 years ago

First of all, thank you for the wonderful integration.

Second, the issue :)

I have read through the docs and issues on this repo, but still struggle to understand the correct configuration flow. Let's assume I have multiple cameras running https://github.com/roleoroleo/yi-hack-Allwinner-v2 (r30gb, fw 0.2.2) and a Home Assistant (2022.08.1) with MQTT configured (as an integration and as an addon that actually runs the Mosquitto broker).

Is this the proper configuration flow?

  1. Setup the MQTT broker
  2. Add MQTT integration to HA
  3. Configure MQTT on each camera (enabled, server IP, server port, unique client ID for each camera, username, password)
  4. Don't configure the MQTT Advertise & Homeassistant MQTT Discovery ?page=mqtt_adv as mentioned in https://github.com/roleoroleo/yi-hack_ha_integration/issues/70#issuecomment-1013924883
  5. Install this integration via HACS
  6. Configure cameras in HA integrations UI

Here are additional issues that I've encountered:

maxromanovsky commented 2 years ago

@roleoroleo do you have any hints? I'd be happy to contribute the correct installation procedure to documentation in the repo.

roleoroleo commented 2 years ago

What if I change any parameter later on? Will it be discovered by the Home Assistant integration later? Or does it detect any parameters only when you configure the camera in HA first time? Here it says that topics are automatically detected, but doesn't say if it occurs on the first connection to the camera, or on each connection:

You cannot change ip, port, username and password of the cam. The other parameters should be automatically detected.

Do I need to configure username and password? Here it says, that I don't need

Which username and password do you mean? Mqtt server or cam? But in general you don't need them.

  • If I have 2 cameras, how to configure the unique client ID? Does it need to correspond with the device name in HA? Or will it me discovered automatically as mentioned here?

You can use whatever you want, it's not used by ha. It is only for connection to the broker: if the broker receives 2 different connection with the same client id, the second one is refused.

I've performed this procedure for 2 cameras, however from what I see the MQTT (motion detection) doesn't distinguish between the cameras (i.e. if motion is detected by one of the cameras, the status and image is propagated to both camera.yi_hack_a2_xxxxx_motion_detection_cam entities)

This is a strange behavior. Are you using the same mqtt topic for the cams?

Also, when I try to rename a device (from yi_hack_a2_337fe9 to something like YI Dome Guard Living Room) and confirm renaming the entities, HA crashes, and after restart only device is renamed (entities all have old IDs) When I try to rename an entity directly (e.g. to camera.yi_dome_guard_living_room), HA also crashes and restarts

I need to check this problem. Is there a log?

  • PTZ entity didn't appear

PTZ is a service, not an entity: yi_hack.ptz

maxromanovsky commented 2 years ago

@roleoroleo thank you for the answer.

Which username and password do you mean? Mqtt server or cam? But in general you don't need them.

I meant MQTT username & password on camera page /index.html?page=mqtt. I configured them manually before adding cameras to Home Assistant, but I was confused with this comment from you: https://github.com/roleoroleo/yi-hack_ha_integration/issues/55#issuecomment-981493919 I tried not configuring them, but then integration didn't work. I am using MQTT addon & integration, everything runs within HASS. Without username/password mosquitto_sub couldn't find any messages from cameras.

This is a strange behavior. Are you using the same mqtt topic for the cams? Yes. Should I use different topics? I couldn't find any documentation on how this works, and assumed that HASS might be distinguishing the cameras by client id. Could you explain, how does HASS correlate MQTT topic with particular camera instance? Something like that?

  • Retrieve MQTT topic from camera config upon connecting via HTTP with your integration (only when configuring? every time when camera changes status to online?)
  • Map that topic to the camera instance
  • Subscribe to messages from that topic and correlate them with the camera

If my understanding is correct, and I should have 1 topic per camera, then can I just change this config in the camera, or do I need to remove & re-add camera entities again?

Also, when I try to rename a device (from yi_hack_a2_337fe9 to something like YI Dome Guard Living Room) and confirm renaming the entities, HA crashes, and after restart only device is renamed (entities all have old IDs) When I try to rename an entity directly (e.g. to camera.yi_dome_guard_living_room), HA also crashes and restarts I need to check this problem. Is there a log?

No, I don't have it anymore. I can try to reproduce and capture a log.

PTZ is a service, not an entity: yi_hack.ptz Awesome, I tried to call in via Developer Tools and it works! I just found that WebRTC has the PTZ control, will try it later.

trombik commented 2 years ago

only when configuring? every time when camera changes status to online?

MQTT topics are retrieved when HA setups the entry via async_setup_entry().

https://github.com/roleoroleo/yi-hack_ha_integration/blob/01e3762d24a459073b1531df8a3ac272c7df3676/custom_components/yi_hack/__init__.py#L30-L55

https://developers.home-assistant.io/docs/config_entries_index/#setting-up-an-entry says:

During startup, Home Assistant first calls the normal component setup, and then call the method async_setup_entry(hass, entry) for each entry. If a new Config Entry is created at runtime, Home Assistant will also call async_setup_entry(hass, entry).

do I need to remove & re-add camera entities again?

probably, yes.

AlexRux commented 10 months ago

I have the same issue. Is this fixed? How?

Two Yi Cam that shows the same status via MQTT. (If i click to turn off the led on one, led will be turned off on both yi cam, for example)

@roleoroleo

AlexRux commented 10 months ago

image

Fixed!

This also need to be changed and to be unique, not only the ID.

Topics Prefix | The MQTT Topic prefix where the camera will publish messages. The final topic will be TOPIC_PREFIX/TOPIC_XXXXX (Example: yicam/motion). -- | --

yicam for both is the issue.

roleoroleo commented 10 months ago

This also need to be changed and to be unique, not only the ID.

Yes, it's true.

MrBass8 commented 8 months ago

Hi, I'm having the same problem. I have 7 cameras with all unique and configured parameters (hostname, unique id, topics prefix, client id etc.). On ha they are all seen as a single device (they are all grouped together but I can command the individual commands without problems) and they also all appear with strange names yi_hack_a2_xxxxxxx. I tried a lot but couldn't solve it. I also removed and reinserted the cameras, but it didn't work. in mqtt_adv I also tried changing the model from yi_hack to something else, but they are never seen as single devices. Is there a solution? Thank you

roleoroleo commented 8 months ago

I don't understand: are you using the integration or mqtt advertise?

MrBass8 commented 8 months ago

I'm using the integration on ha. The entire mqtt part is also configured on each camera. in fact the broker (like the integration) sees all the cameras as a single device

roleoroleo commented 8 months ago

Please, share your config: http://IP-CAM/cgi-bin/get_configs.sh?conf=system http://IP-CAM/cgi-bin/get_configs.sh?conf=mqtt hiding your personal data.

MrBass8 commented 8 months ago

ok, I'll only show you the ones for one camera since they are all configured the same way system: { "HTTPD":"yes", "TELNETD":"no", "SSHD":"no", "FTPD":"no", "BUSYBOX_FTPD":"no", "DISABLE_CLOUD":"yes", "REC_WITHOUT_CLOUD":"yes", "MQTT":"yes", "RTSP":"yes", "RTSP_STREAM":"high", "RTSP_AUDIO":"aac", "SPEAKER_AUDIO":"yes", "SNAPSHOT":"no", "ONVIF":"no", "ONVIF_WSDD":"no", "ONVIF_PROFILE":"high", "ONVIF_NETIF":"wlan0", "ONVIF_WM_SNAPSHOT":"no", "NTPD":"yes", "NTP_SERVER":"", "PROXYCHAINSNG":"no", "SWAP_FILE":"no", "RTSP_PORT":"", "ONVIF_PORT":"80", "HTTPD_PORT":"8080", "USERNAME":"***********", "PASSWORD":"***********", "TIMEZONE":"", "FREE_SPACE":"20", "FTP_UPLOAD":"no", "FTP_HOST":"", "FTP_DIR":"", "FTP_DIR_TREE":"no", "FTP_USERNAME":"", "FTP_PASSWORD":"", "FTP_FILE_DELETE_AFTER_UPLOAD":"yes", "SSH_PASSWORD":"", "CRONTAB":"", "RTSP_ALT":"no", "SNAPSHOT_VIDEO":"no", "SNAPSHOT_LOW":"no", "TIMELAPSE":"no", "TIMELAPSE_FTP":"no", "TIMELAPSE_DT":"60", "TIMELAPSE_VDT":"", "TIME_OSD":"no", "DEBUG_LOG":"no", "TIMELAPSE_FTP_SAME_NAME":"no", "SWAP_SWAPPINESS":"15", "EVENTS_TIME":"autodetect", "HOSTNAME":"Cam_Sala", "NULL":"NULL" } mqtt { "MQTT_IP":"10.30.8.3", "MQTT_PORT":"1883", "MQTT_CLIENT_ID":"cam_sala", "MQTT_USER":"*********", "MQTT_PASSWORD":"*********", "MQTT_PREFIX":"cam_sala", "MQTT_PREFIX_CONFIG":"living_cam-config", "TOPIC_BIRTH_WILL":"status", "TOPIC_AI_HUMAN_DETECTION":"ai_human_detection", "TOPIC_MOTION":"motion_detection", "TOPIC_MOTION_IMAGE":"motion_detection_image", "TOPIC_MOTION_FILES":"motion_files", "TOPIC_BABY_CRYING":"baby_crying", "TOPIC_SOUND_DETECTION":"sound_detection", "BIRTH_MSG":"online", "WILL_MSG":"offline", "AI_HUMAN_DETECTION_START_MSG":"human_start", "AI_HUMAN_DETECTION_STOP_MSG":"human_stop", "MOTION_START_MSG":"motion_start", "MOTION_STOP_MSG":"motion_stop", "BABY_CRYING_MSG":"crying", "SOUND_DETECTION_MSG":"sound", "MQTT_KEEPALIVE":"120", "MQTT_QOS":"1", "MQTT_RETAIN_BIRTH_WILL":"1", "MQTT_RETAIN_AI_HUMAN_DETECTION":"1", "MQTT_RETAIN_MOTION":"1", "MQTT_RETAIN_MOTION_IMAGE":"1", "MQTT_RETAIN_MOTION_FILES":"1", "MQTT_RETAIN_BABY_CRYING":"1", "MQTT_RETAIN_SOUND_DETECTION":"1", "MOTION_IMAGE_DELAY":"0.5", "AI_HUMAN_DETECTION_MSG":"human", "AI_VEHICLE_DETECTION_MSG":"vehicle", "AI_ANIMAL_DETECTION_MSG":"animal", "NULL":"NULL" }

roleoroleo commented 8 months ago

Another conf please: http://IP-CAM/cgi-bin/get_configs.sh?conf=mqtt_advertise

About mqtt configuration... You don't need to set retain to 1 except for MQTT_RETAIN_BIRTH_WILL.

MrBass8 commented 8 months ago

ok, I will change those settings that you recommended above all

{ "MQTT_ADV_LINK_ENABLE":"yes", "MQTT_ADV_LINK_BOOT":"yes", "MQTT_ADV_LINK_CRON":"no", "MQTT_ADV_LINK_CRONTAB":"0 * * * *", "MQTT_ADV_LINK_TOPIC":"links", "MQTT_ADV_LINK_RETAIN":"1", "MQTT_ADV_LINK_QOS":"0", "MQTT_ADV_INFO_GLOBAL_ENABLE":"yes", "MQTT_ADV_INFO_GLOBAL_BOOT":"yes", "MQTT_ADV_INFO_GLOBAL_CRON":"no", "MQTT_ADV_INFO_GLOBAL_CRONTAB":"0 * * * *", "MQTT_ADV_INFO_GLOBAL_TOPIC":"info_global", "MQTT_ADV_INFO_GLOBAL_RETAIN":"1", "MQTT_ADV_INFO_GLOBAL_QOS":"0", "MQTT_ADV_CAMERA_SETTING_ENABLE":"yes", "MQTT_ADV_CAMERA_SETTING_BOOT":"yes", "MQTT_ADV_CAMERA_SETTING_CRON":"no", "MQTT_ADV_CAMERA_SETTING_CRONTAB":"0 * * * *", "MQTT_ADV_CAMERA_SETTING_TOPIC":"camera_setting", "MQTT_ADV_CAMERA_SETTING_RETAIN":"1", "MQTT_ADV_CAMERA_SETTING_QOS":"0", "MQTT_ADV_TELEMETRY_ENABLE":"yes", "MQTT_ADV_TELEMETRY_BOOT":"yes", "MQTT_ADV_TELEMETRY_CRON":"no", "MQTT_ADV_TELEMETRY_CRONTAB":"*/10 * * * *", "MQTT_ADV_TELEMETRY_TOPIC":"telemetry", "MQTT_ADV_TELEMETRY_RETAIN":"1", "MQTT_ADV_TELEMETRY_QOS":"0", "HOMEASSISTANT_ENABLE":"yes", "HOMEASSISTANT_BOOT":"yes", "HOMEASSISTANT_CRON":"no", "HOMEASSISTANT_CRONTAB":"0 * * * *", "HOMEASSISTANT_MQTT_PREFIX":"homeassistant", "HOMEASSISTANT_NAME":"Cam Sala", "HOMEASSISTANT_IDENTIFIERS":"cam_sala", "HOMEASSISTANT_MANUFACTURER":"YI", "HOMEASSISTANT_MODEL":"Yi Hack", "HOMEASSISTANT_RETAIN":"1", "HOMEASSISTANT_QOS":"1", "NULL":"NULL" }

MrBass8 commented 8 months ago

meanwhile I forgot to thank you for the help and for the wonderful firmware :)

roleoroleo commented 8 months ago

If you want to use this integration, you should disable mqtt advertising, specifically this one: immagine

This last feature is useful if you want to add the cam to ha using the standard mqtt integration and not my custom integration. Please try turning off mqtt advertising, remove all entries from ha and try again.

MrBass8 commented 8 months ago

ok, I'll try it with a camera. Thanks in the meantime for everything

MrBass8 commented 8 months ago

I just tested it and nothing, the problem is the same as before. The integration always sees me as the strangely named camera (yi_hack_a2_5267fb) and puts all the entities into one device. Obviously I performed all the necessary reboots.

I saw that the last digits correspond to the final ones of the camera's MAC address

roleoroleo commented 8 months ago

The name yi_hack_a2_xxxxxxx is normal. It's a unique name created on the fly using the mac address of the cam. For example, if the mac of your cam is 00:01:02:03:04:05 the name will be yi_hack_a2_030405

The problem is: why all the entities are in the same device? Could you check the mac addresses of the cams (index page of the webgui)?

MrBass8 commented 8 months ago

Screenshot 2024-03-28 115658

that's how they look.

ps: are you Italian?

roleoroleo commented 8 months ago

This is what you should see: immagine

ps: are you Italian?

Yes.

MrBass8 commented 8 months ago

I don't know.... to me it shows everything as a single device. I don't know where to check anymore... I also removed the ubiquiti integration, seeing as it appeared among the cameras, but nothing.

roleoroleo commented 8 months ago

Which version are you using?

MrBass8 commented 8 months ago

of what specifically? however I have everything updated to the latest version (ha, yi hack, integration etc...)

roleoroleo commented 8 months ago

Is there any error when you start ha? About yi-hack integration, I mean.

MrBass8 commented 8 months ago

no, no errors on integration

roleoroleo commented 8 months ago

The integration uses a two different methods to communicate with the cam:

IMHO, the problem you described is not related to mqtt. But I don't understand why it happens. The name depends on the mac address, the mac address is returned by a http request, and the http request is related to a specific IP address.

MrBass8 commented 8 months ago

I don't think the problem is that I changed the httpd port from 80 to 8080

roleoroleo commented 8 months ago

No, if you removed and added the cam again after this change.

roleoroleo commented 8 months ago

Send me the status of two different cams: http://IP-CAM/cgi-bin/status.json

MrBass8 commented 8 months ago

{ "name":"yi-hack-allwinner-v2", "hostname":"Cam_Sala", "fw_version":"0.3.1", "home_version":"9.0.19.02_202007141512", "model_suffix":"y21ga", "ptz":"no", "serial_number":"BFUSYxxxxxxxx", "local_time":"Sat Apr 18 16:53:52 GMT 2020", "uptime":"74261.73", "load_avg":"0.55 1.29 1.49", "total_memory":"60912", "free_memory":"15688", "free_sd":"20%", "local_ip":"10.30.8.52", "netmask":"255.255.255.0", "gateway":"10.30.8.1", "mac_addr":"XX:XX:XX:52:67:FB", "wlan_essid":"ShadowDom", "wlan_strength":"60" }

{ "name":"yi-hack-allwinner-v2", "hostname":"Cam_Cucina", "fw_version":"0.3.1", "home_version":"9.0.19.02_202007141512", "model_suffix":"y21ga", "ptz":"no", "serial_number":"BFUSYxxxxxxxxxxxxxxx", "local_time":"Fri Mar 29 06:54:44 GMT 2024", "uptime":"74011.92", "load_avg":"1.10 1.62 1.56", "total_memory":"60912", "free_memory":"15392", "free_sd":"2%", "local_ip":"10.30.8.53", "netmask":"255.255.255.0", "gateway":"10.30.8.1", "mac_addr":"XX:XX:XX:51:AC:37", "wlan_essid":"ShadowDom", "wlan_strength":"70" }

{ "name":"yi-hack-allwinner-v2", "hostname":"Cam_Studio", "fw_version":"0.3.1", "home_version":"9.0.19.09_202010291747", "model_suffix":"y21ga", "ptz":"no", "serial_number":"IFUSYxxxxxxxxxxx", "local_time":"Fri Mar 29 07:55:27 CET 2024", "uptime":"74029.75", "load_avg":"2.13 1.56 1.57", "total_memory":"60912", "free_memory":"15876", "free_sd":"2%", "local_ip":"10.30.8.54", "netmask":"255.255.255.0", "gateway":"10.30.8.1", "mac_addr":"XX:XX:XX:D8:87:BB", "wlan_essid":"ShadowDom", "wlan_strength":"70" }

MrBass8 commented 8 months ago

I corrected the date on the cameras after noticing that they were wrong, but I don't think it fits the problem

roleoroleo commented 8 months ago

Status are ok. I don't understand.

MrBass8 commented 8 months ago

the thing is, I don't even have any errors, so I wouldn't even know what else to do. even if I reload the integration (copy all the contents of the custom component) I don't think it changes much.

roleoroleo commented 8 months ago

I've never seen a problem like this, I don't know how to help you.

github-actions[bot] commented 5 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.