Air conditioner - AUX - mqtt configuration

Greetings.
There will be two parts in this post: descriptive and question.

Part I: My configuration
I have AUX ASW-H09A4 WiFi air conditioner (further AC). I chose it especially to work with home assistant (further HA). It has Broadlink WiFi module.
It worth mentioning that I installed python version of HA to have minimal footprint and be able to make rapid update (but that’s another story).

This article assumes you already have mosquitto MQTT broker configured and users added.

To control my AC unit I use this script here: https://github.com/liaan/broadlink_ac_mqtt
Here’s my service file (/etc/systemd/system/ac_mqtt.service) for this script:

[Unit]
Description=AC connection service
After=syslog.target network.target ha.service
PartOf=ha.service

[Service]
WorkingDirectory=/opt/broadlink_ac_mqtt
ExecStartPre=/bin/sleep 20
ExecStart=/opt/homeassistant/bin/python3 /opt/broadlink_ac_mqtt/monitor.py -b
Restart=on-failure
RestartSec=60s

[Install]
WantedBy=multi-user.target

This service has it’s own config file which should be located in <workdir>/settings (in my case /opt/broadlink_ac_mqtt/settings) or specified via command line.
Here’s my config file with some placeholders:

service:
    daemon_mode: True
    update_interval: 10
    self_discovery: True
    bind_to_ip: False

mqtt:
    host: 127.0.0.1
    port: 1883
    client_id: ac_to_mqtt
    user: <mqtt user, added in mqtt broker>
    passwd: <your passord here>
    # Line below shows where in mqtt you will search for AC messages
    # feel free use name to your liking
    topic_prefix: /aircon
    auto_discovery_topic: homeassistant
    auto_discovery_topic_retain: False
    discovery: False

##Devices
devices:
- ip: 192.168.0.10 (or any other IP you/dhcp like)
  mac: <here should be mac of your AC, I looked my up in DHCP>
  name: climatic_ctrl
  port: 80

In HA I have my AC device, which can be added to lovelace UI or controlled via automations.

– Up until this line everything works -------------------
Part II: This should work…
The issue I’ve been having is that if HA service restarts (or starts) after ac_mqtt is started, then HA show it unavailable. Once again:

  1. Start HA.
  2. Start ac_mqtt
  3. everything fine.
  4. Restart HA
  5. air conditioner controls unavailable.

I’m relatively new to HA, so I have a question: What will be the best way to resolve this?
Things that crossed my mind:

  1. Make ac_mqtt service dependant on ha service in system - works so-so, doesn’t shine when HA gets restarted from UI.
  2. Track aircon entity state and on unavailable use shell_script to restart services - seems a little clumsy.

Would be glad to hear any suggestions.

1 Like

Ok, I’ll answer my own question:

shell_command:
  restart_ac_connector: /usr/bin/systemctl restart ac_aux.service

automation

alias: restart ac connector on ha start
description: ''
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - service: shell_command.restart_ac_connector
mode: single

and

alias: restart ac connector
description: ''
trigger:
  - platform: state
    entity_id: climate.mqtt_<mac_here>
    to: unavailable
condition: []
action:
  - service: shell_command.restart_ac_connector
mode: single

Pretty much works.

1 Like

how would this be installed in hassio?

There is a docker version of this package (ac2mqtt): GitHub - broadlink-ac/broadlink_ac_mqtt_docker: Docker version of Broadlink AC to Mqtt
Right now I’m trying to figure out how to make hassio restart ac2mqtt container or update mqtt status on start up, since direct script execution is not possible on host.

Now here’s my version of “get ac back online”. Draft.

  1. Install this custom component: GitHub - ualex73/monitor_docker: Monitor Docker containers from Home Assistant
  2. Configure hassio adding following to configuration.yaml:
monitor_docker:
  - name: hass
    containers:
      - hass
      - ac2mqtt
    monitored_conditions:
      - version
      - containers_running
      - containers_total
      - containers_cpu_percentage
      - containers_memory_percentage
      - state
      - status
  1. Add script to use for restart (You can add this directly to automation, but there are a couple of those and it will create code duplication):
restart_ac2mqtt:
  alias: restart ac2mqtt
  sequence:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.hass_ac2mqtt
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.hass_ac2mqtt
  mode: single
  icon: mdi:air-conditioner
  1. Add couple automations:
- id: 'ac1'
  alias: restart ac connector
  description: ''
  trigger:
  - platform: state
    entity_id: climate.mqtt_<your_mac_here>
    to: unavailable
  condition: []
  action:
  - service: script.restart_ac2mqtt
    data: {}
  mode: single
- id: 'ac2'
  alias: restart ac connector on ha start
  description: ''
  trigger:
  - platform: homeassistant
    event: start
  condition: []
  action:
  - service: script.restart_ac2mqtt
    data: {}
  mode: single

Ok, upgrade time.
Method described above proved to be a bit complicated, so I decided to simplify everything a bit. Also make infrastructure ‘docker’ friendly, so here’s new deal:

Directory structure:
config/ - directory with config for ac2mqtt
docker-config.yml - configuration for docker

Requirements:

  1. You should have docker installed and setup.
  2. You should have mqtt server (I’ll use localhost for example, feel free to use any other address if needed).
  3. You should have docker-compose installed

config/config.yml

service:
    daemon_mode: True
    update_interval: 10
    self_discovery: True
    bind_to_ip: False

mqtt:
    host: 127.0.0.1 # <- this here is address of your MQTT server
    port: 1883
    client_id: ac_to_mqtt
    user: <username> # <- put your mqtt username here
    passwd: <password goes here>
    topic_prefix: /aux # this topic will be used to get data in home assistant
    auto_discovery_topic: homeassistant
    auto_discovery_topic_retain: False
    discovery: False

docker-compose.yml

version: '3'
services:
  ac2mqtt:
    container_name: ac2mqtt
    environment:
      - PUID=<home_assistant UID from /etc/passwd>
      - PGID=<home_assistant GID from /etc/group>
      - TZ=<your timezone here>
    hostname: ac2mqtt
    image: broadlinkac/broadlink_ac_mqtt
    network_mode: host
    restart: unless-stopped
    volumes:
      - ./config:/config

Replace variables in brackets, docker-compose up -d and your connector is up and running.

image
What should I change for ?

PUID - id of user that runs homeassistant
PGID - same with goup.

for example, home assistant runs as user hass, group ha.
PUID should be grep hass /etc/passwd
PGID be grep ha /etc/group

For TZ see list of timezones and specify yours. For example Greenwich (GMT+00) time be Europe/London
TZ=Europe/London