Home Assistant - iot-root/garden-of-eden GitHub Wiki

Home Assistant

The mqtt.py contains the topic discovery for HomeAssistant (HASS)

HASS Setup

  1. Make sure you add the MQTT integration.

Schedule...

description: Combined schedule for Gardyn light and pump control
trigger:
  - platform: time
    at: "06:00:00"
  - platform: time
    at: "09:00:00"
  - platform: time
    at: "17:00:00"
  - platform: time
    at: "20:00:00"
  - platform: time
    at: "08:00:00"
  - platform: time
    at: "11:15:00"
  - platform: time
    at: "08:05:00"
  - platform: time
    at: "16:00:00"
  - platform: time
    at: "16:05:00"
  - platform: time
    at: "21:00:00"
  - platform: time
    at: "21:05:00"
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.now.strftime('%H:%M:%S') == '06:00:00' }}"
        sequence:
          - service: light.turn_on
            entity_id: light.gardyn_light
            data:
              brightness_pct: 50
      - conditions:
          - condition: template
            value_template: "{{ trigger.now.strftime('%H:%M:%S') == '09:00:00' }}"
        sequence:
          - service: light.turn_on
            entity_id: light.gardyn_light
            data:
              brightness_pct: 70
      - conditions:
          - condition: template
            value_template: "{{ trigger.now.strftime('%H:%M:%S') == '11:15:00' }}"
        sequence:
          - service: light.turn_on
            entity_id: light.gardyn_light
            data:
              brightness_pct: 100
      - conditions:
          - condition: template
            value_template: "{{ trigger.now.strftime('%H:%M:%S') == '17:00:00' }}"
        sequence:
          - service: light.turn_on
            entity_id: light.gardyn_light
            data:
              brightness_pct: 50
      - conditions:
          - condition: template
            value_template: "{{ trigger.now.strftime('%H:%M:%S') == '20:00:00' }}"
        sequence:
          - service: light.turn_off
            entity_id: light.gardyn_light
      - conditions:
          - condition: template
            value_template: >-
              {{ trigger.now.strftime('%H:%M:%S') in ['08:00:00', '16:00:00',
              '21:00:00'] }}
        sequence:
          - service: switch.turn_on
            entity_id: switch.gardyn_pump
      - conditions:
          - condition: template
            value_template: >-
              {{ trigger.now.strftime('%H:%M:%S') in ['08:05:00', '16:05:00',
              '21:05:00'] }}
        sequence:
          - service: switch.turn_off
            entity_id: switch.gardyn_pump
mode: single

PI Setup

Install MQTT Broker Note: you can run the broker on any device, it doesnt have to live on the pi.

Update packages:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y mosquitto mosquitto-clients
sudo systemctl enable mosquitto.service
sudo systemctl status mosquitto.service

Create your broker username and password:

Note: we will have to update the .env so mqtt.py and config.py can pull them in.

mosquitto_passwd -c /etc/mosquitto/passwd <some_username>

Run sudo nano /etc/mosquitto/mosquitto.conf to edit the config file and paste the following:

#/etc/mosquitto/mosquitto.conf

pid_file /run/mosquitto/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

listener 1883 0.0.0.0

allow_anonymous false
password_file /etc/mosquitto/passwd

include_dir /etc/mosquitto/conf.d

Restart the service to ensure the latest config settings are being used:

sudo systemctl restart mosquitto.service

check everything is running:

sudo systemctl status mosquitto.service

To test out your broker:

mosquitto_pub -h localhost -t testtopic -m "Hello, MQTT!" -u "yourusername" -P "yourpassword"

Camera

This is a temp solution until I work it in to the mqtt.py

In your HA config, you can manually edit configuration.yml and add matt image topics manually

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# MQTT Configuration
mqtt:
  image:
    - name: "Camera A"
      image_topic: "gardyn/image/camera_a"
      encoding: "b64"  # Use "b64" for Base64-encoded images or "" for raw binary
      content_type: "image/jpeg"  # Specify the image format (e.g., image/jpeg, image/png)
    - name: "Camera B"
      image_topic: "gardyn/image/camera_b"
      encoding: "b64"  # Use "b64" for Base64-encoded images or "" for raw binary
      content_type: "image/jpeg"  # Specify the image format (e.g., image/jpeg, image/png)

Then you can create a script like publish-cams.sh with the following:

#!/bin/bash
sudo fswebcam -d /dev/video0 -r 2500x1900 -S 2 -F 2 /tmp/capture_a.jpg
sudo fswebcam -d /dev/video2 -r 2500x1900 -S 2 -F 2 /tmp/capture_b.jpg
mosquitto_pub -t "gardyn/image/camera_a" -f /tmp/capture_a.jpg -u gardyn -P somepassword
mosquitto_pub -t "gardyn/image/camera_b" -f /tmp/capture_b.jpg -u gardyn -P somepassword