TawfikDaim / home_automation

home automation user stories
0 stars 0 forks source link

docker-compose for z2mqtt #70

Open TawfikDaim opened 3 months ago

TawfikDaim commented 3 months ago

how to find usb zigbee stick

create file: find_usb.sh

for sysdevpath in $(find /sys/bus/usb/devices/usb/ -name dev); do ( syspath="${sysdevpath%/dev}" devname="$(udevadm info -q name -p $syspath)" [[ "$devname" == "bus/" ]] && exit eval "$(udevadm info -q property --export -p $syspath)" [[ -z "$ID_SERIAL" ]] && exit echo "/dev/$devname - $ID_SERIAL" ) done

sudo chmod +x find_usb.sh

run the file by typing: ./find_usb.sh

results: /dev/ttyUSB0 - Silicon_Labs_Sonoff_Zigbee_3.0_USB_Dongle_Plus_0001 /dev/sdb1 - Generic_Flash_Disk_0745592A-0:0 /dev/ttyACM0 - ITEAD_SONOFF_Zigbee_3.0_USB_Dongle_Plus_V2_20230221134927 /dev/sda2 - AI_Mass_Storage-0:0

TawfikDaim commented 3 months ago

docker-compose.yml

You can change the port number, if there are serveral instances running

version: "3.1" services: z2mqtt_dockercompose: container_name: z2mqtt_dockercompose image: koenkk/zigbee2mqtt restart: always

priviliged: true

devices:

TawfikDaim commented 3 months ago

to find the correct port for this line serial: port: /dev/ttyACM0 use the command above run ./find_usb.sh

results: /dev/ttyUSB0 - Silicon_Labs_Sonoff_Zigbee_3.0_USB_Dongle_Plus_0001 /dev/sdb1 - Generic_Flash_Disk_0745592A-0:0 /dev/ttyACM0 - ITEAD_SONOFF_Zigbee_3.0_USB_Dongle_Plus_V2_20230221134927 /dev/sda2 - AI_Mass_Storage-0:0

/docker/z2mqtt/configuration.yaml

homeassistant: legacy_entity_attributes: true legacy_triggers: true frontend: true mqtt: base_topic: zigbee2mqtt server: mqtt://192.168.1.23:1883 user: Tawfik password: Newipad1! keepalive: 60

reject_unauthorized: true

version: 4

serial: port: /dev/ttyACM0 advanced: network_key:

TawfikDaim commented 3 months ago

homeassistant: legacy_entity_attributes: true legacy_triggers: true frontend: true mqtt: base_topic: zigbee2mqtt server: mqtt://192.168.1.53:1883 availability: active: timeout: 30 passive: timeout: 1500 device_options: homeassistant: last_seen: enabled_by_default: true serial: port: /dev/ttyACM0 advanced: network_key:

TawfikDaim commented 3 months ago

I also set the Last Seen timestamp type to ISO_8601 in the Settings -> Advanced area of Zigbee2MQTT.

TawfikDaim commented 3 months ago

This Last Seen value is actually brought over from Zigbee2MQTT into Home Assistant, but it’s part of a diagnostic entity that is disabled by default. To make use of it you need to enable it.

to do this you need to add the following parts to your Zigbee2MQTT configuration.yaml file.

device_options: homeassistant: last_seen: enabled_by_default: true

TawfikDaim commented 3 months ago

https://www.homeautomationguy.io/blog/managing-offline-devices-in-zigbee2mqtt

To create a template sensor go to Home Assistants settings page, and Helpers. Create a Helper, which is a Template sensor. Give it a name and paste this code into the state template area:

{% set result = namespace(sensors=[]) %} {% for state in states.sensor | rejectattr('attributes.device_class', 'undefined') | selectattr('attributes.device_class', '==', 'timestamp') %} {% if 'last_seen' in state.entity_id and (states(state.entity_id) == 'unavailable' or ((as_timestamp(now()) - as_timestamp(states(state.entity_id))) > ((24 | int) 60 60))) %} {% set result.sensors = result.sensors + [state.name | regex_replace(find=' last seen', replace='') ~ ' (' ~ relative_time(strptime(states(state.entity_id), '%Y-%m-%dT%H:%M:%S%z', 'unavailable')) ~ ')'] %} {% endif %} {% endfor %} {{ result.sensors | join(', ') }}

You will see a preview of the sensor at the bottom of the dialog box listing out any Offline devices, or ones that have not been seen for over 24 hours.

TawfikDaim commented 3 months ago

Paste this into dev tools, template:

{% set result = namespace(sensors=[]) %} {% for state in states.sensor | selectattr('attributes.unit_of_measurement', '==', 'lqi') %} {% if state_attr(state.entity_id, 'last_seen') != None and (as_timestamp(now()) - as_timestamp(state_attr(state.entity_id, 'last_seen'))) > ((1 | int) 60 60) %} {% set result.sensors = result.sensors + [state.name | regex_replace(find=' linkquality', replace='') ~ ' (' ~ relative_time(strptime(state_attr(state.entity_id, 'last_seen'), '%Y-%m-%dT%H:%M:%S%z')) ~ ')'] %} {% endif %} {% endfor %} {{ result.sensors | join(', ') }}

If you see no result, you maybe have lqi sensors disabled (z2m update some time ago disabled those sensors by default…). If so you need to go to configuration, entities, check show disabled entities and search for linkquality - you should then enable all linkquality sensors for mqtt integration

TawfikDaim commented 3 months ago

https://community.home-assistant.io/t/offline-detection-for-z2m-devices-with-last-seen/310138/5

create an automation based on this blueprint

Do you have succesfully imported and used any other blueprint? I’ve just deleted this blueprint on my local HA setup and imported it again succesfully.

Configuration → Blueprints → Import → Paste URL https://gist.github.com/Mr-Groch/bf073b142b507e3b6f8154223f81803b 27 → Preview → Import → Create Automation → Fill all required fields → Save

https://gist.github.com/Mr-Groch/bf073b142b507e3b6f8154223f81803b

You must have lqi sensors and last_seen option from Zigbee2MQTT enabled (see first post).

Image

actions:
  - service: notify.persistent_notification
    data:
      title: Sensors last_seen exceeded 4hrs
      message: |
        Sensors to check:
        {{sensors}}
        Message Timestamp: {{ now().strftime("%d-%m-%Y %H:%M.%S") }}
        Automation: [Devices, Z2M - last_seen exceeds 4hrs]

Image

Image