Skip to content

Instantly share code, notes, and snippets.

@jffz
Last active January 12, 2024 21:43
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jffz/dbcfed2792ab07749ddf0a3e6f5039ea to your computer and use it in GitHub Desktop.
Save jffz/dbcfed2792ab07749ddf0a3e6f5039ea to your computer and use it in GitHub Desktop.
Publish hiveos logs to mqtt broker
#!/bin/bash
# MQTT settings
BROKER=""
TOPIC=""
USER=""
PASS=""
# Ensure mosquitto-clients is installed
dpkg -s mosquitto-clients >/dev/null 2>&1 || apt update && apt install -y mosquitto-clients
# Stream logs to MQTT
tail -f /var/log/hive-agent.log | while read -r line; do
json_line=$(echo ${line} | awk -F'[<>]' {'print $2'})
if $(echo "$json_line" | jq -e 'has("method")'); then
mosquitto_pub -h $BROKER -t $TOPIC -u "$USER" -P "$PASS" -m "$json_line"
fi
done
- platform: mqtt
name: hiveos_stats
state_topic: "hiveos"
value_template: "{{ value_json.params.uptime }}"
json_attributes_topic: "hiveos"
json_attributes_template: "{{ value_json.params.miner_stats | to_json }}"
- platform: mqtt
name: hiveos_params
state_topic: "hiveos"
value_template: "{{ value_json.params .uptime }}"
json_attributes_topic: "hiveos"
json_attributes_template: "{{ value_json.params | to_json }}"
@krasatos
Copy link

krasatos commented May 20, 2021

value_template: "{{ value_json.params .uptime }}"

There's a whitespace before .uptime that should not be there :) - (i think)

@krasatos
Copy link

value_json.params.uptime should be value_json.params.miner_stats.uptime on both sensors or it's throwing an error on HA logs

@PablexXXXX
Copy link

Great. Can anyone describe how to implement this? Should I compile this? Or just copy and paste on my hiveos?

@mderubertis
Copy link

@PablexXXXX add the mqtt_sensors.yaml to your HA configuration, and run the hiveos2mqtt.sh on your miner. You will need to make sure that it runs on boot. I will share my experience on setting up the script to start on boot when I give it a try.

@om2don
Copy link

om2don commented Aug 19, 2022

Thanks for your great work.
i have managed to get the worker to connect to the MQTT broker on my home assistant, but i get no data, and sensors are unavailable
i get this on mqtt broker logs
1660905302: New connection from 192.168.1.140:51864 on port 1883.
1660905302: New client connected from 192.168.1.140:51864 as mosqpub|26074-hive02_30 (p1, c1, k60, u'mqtt').

then in the config.yaml for my home assistant i have added
sensor:

  • platform: mqtt
    name: hiveos_stats
    state_topic: "hive_mqtt_1"
    value_template: "{{ value_json.params.miner_stats.uptime }}"
    json_attributes_topic: "hive_mqtt_1"
    json_attributes_template: "{{ value_json.params.miner_stats | to_json }}"

  • platform: mqtt
    name: hiveos_params
    state_topic: "hive_mqtt_1"
    value_template: "{{ value_json.params.miner_stats.uptime }}"
    json_attributes_topic: "hive_mqtt_1"
    json_attributes_template: "{{ value_json.params | to_json }}"

im a bit confused
should i add a new file somewhere named mqtt_sensors.yaml
and if yes, where should the file be located

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment