Hello,
I offer 2 TUTORIALS that explain how to use BASH commands to send data to Gladys and to receive data from it.
BASH is simple to use and is present on many GNU/Linux systems (such as Raspberry) and will in any case be reproducible for another SHELL.
It allows executing any system command and managing the entire PC.
For communication, you must use the MQTT protocol.
Gladys very kindly offers to install an MQTT broker in one click via Integration > MQTT > Configuration > Use a broker in a Docker container.
Once installed, you can create devices via Integration > MQTT > Devices > New.
TUTORIAL n°1 : Display the Raspberry temperature in Gladys.
The idea is therefore to retrieve the Raspberry temperature via a BASH script and send it to Gladys.
On the Gladys side:
-
Creating the device:
Name : Raspberry Temperature
External Id : mqtt:Salle:RaspTemp (mqtt:Room:Element)
Room : Salle -
**Adding a feature of type : **
Name : Raspberry Temperature
Feature external ID : mqtt:RaspTemp:temp (mqtt:Element:Function)
Unit : °C
Minimum value : 0 (to reach this temperature you’d have to cool it with nitrogen…)
Maximum value : 110 (perfect for using it as a heater)
Keep state history : Yes
Is it a sensor : Yes
MQTT topic to publish : You can set aside the topic that will be used later. -
Adding the device to the dashboard:
Nothing specific here, proceed as usual.
That’s it, we’re done with Gladys ![]()
On the BASH side:
Let’s move on to the BASH scripts that I will put in /opt/mosquittos/ (or another location of your choice but you will need to adapt the paths).
I comment the code as much as possible so it is as understandable as possible; feel free to read it.
- Creating a common script /opt/mosquitto/MQTTConfig.sh :
It will be sourced by the others (different TUTORIALS), it contains :
A function testing the required commands and packages to install (for Ubuntu) if necessary.
The connection credentials for the MQTT broker created by Gladys accessible via Integration > MQTT > Configuration.
Variables to color the output.
#!/bin/bash
#############################################
## File to source in MQTT scripts ##
#############################################
### Variables to customize
# Connection to the MQTT broker
User="gladys"
Pass="XYZ"
### Variables to customize
function CommandCheck()
{
# Function checking for the presence of required commands
# Parameter: Command name and package (optional) separated by :
# e.g.: mosquitto_pub:mosquitto-clients
# If no parameter is given
[[ -z ${1} ]] && return 1
# Limit variables to the function
local Command Package Parameter ReturnValue
ReturnValue=0
# Process all parameters
for Parameter in "${@}"
do
Command="${Parameter%%:*}"
Package="${Parameter##*:}"
# If the command does not exist
if ! which ${Command} &>/dev/null
then
# Set return value to 1 to display all echos before stopping
ReturnValue=1
echo -e "[${ROUGE}Erreur${RAZ}] ArrĂŞt du script car la commande ${BLEUFONCE}${Command}${RAZ} est introuvable." 1>2
# If a package is given
[[ ${Package} ]] && echo -e "Pour ubuntu : ${FUCHSIA}sudo apt-get install ${Package}${RAZ}" 1>2
fi
done
return ${ReturnValue}
}
# Colors for output
FUCHSIA="\\e[1;35m"
RAZ="\\e[m"
BLEUFONCE="\\e[1;34m"
ROUGE="\\e[1;31m"
ORANGE="\\e[1m\\[\\e[38;5;202m\\]"
Only the variables between ### Variables to customize need to be adapted.
- Creating the script /opt/mosquitto/BashToGladys.sh :
Its purpose is to retrieve the Raspberry temperature at regular intervals and send this information to Gladys.
#/bin/bash
###################################################
## Sending the Raspberry temperature to Gladys ##
###################################################
### Variables to customize
# Number of seconds between temperature retrievals
# A small value is not a very good idea...
TimeOut=60
# Device topic address
# Available in Gladys > Integration > MQTT > Devices > Raspberry Temperature > Temperature > MQTT topic to publish
Topic="gladys/master/device/mqtt:Salle:RaspTemp/feature/mqtt:RaspTemp:temp/state"
### Variables to customize
# Loading the common file
source /opt/mosquitto/MQTTConfig.sh
# Check for required commands which, if missing, will stop the script
# Requires the mosquitto_pub command from package mosquitto-clients
! CommandCheck "mosquitto_pub:mosquitto-clients" && exit 1
# Block the script if TimeOut is less than 1
if [[ ${TimeOut:-0} -lt 1 ]]
then
echo -e "[${ROUGE}Erreur${RAZ}] Le TimeOut est inadapté." 1>2
exit 1
fi
# Infinite loop
while true
do
# Retrieve the Raspberry temperature
Temperature=$(/usr/bin/vcgencmd measure_temp)
Temperature=${Temperature//[^0-9.]/}
# If the variable is not empty
if [[ ${Temperature} ]]
then
# Display the info in the SHELL
echo -e "[${FUCHSIA}$(date +'%x %X')${RAZ}] La température du Raspberry est de ${BLEUFONCE}${Temperature}°c${RAZ}."
# Send the information to Gladys with mosquitto_pub
# -u User : User for connecting to the MQTT broker, comes from MQTTConfig.sh
# -P Pass : Password for connecting to the MQTT broker, comes from MQTTConfig.sh
# -t Topic : Address of the device Topic created in Gladys, defined at the top of the script
# -m Temperature : Use the temperature
mosquitto_pub -u "${User}" -P "${Pass}" -t "${Topic}" -m "${Temperature}"
# Get the return value of the mosquitto_pub command
MosquittoReturns=${?}
# Display mosquitto error code if return code > 0
(( ${MosquittoReturns} )) && echo -e "[${ROUGE}Erreur${RAZ}] La commande mosquitto_sub a renvoyé le code ${MosquittoReturns}." 1>2
# If the temperature variable is empty
else
# Display an error message
echo -e "[${ROUGE}Erreur${RAZ}] Température non récupérée." 1>2
fi
# Pause the script for the requested time
sleep "${TimeOut}"
done
Only the variables between ### Variables to customize need to be adapted.
- Running the script:
Just run:
bash /opt/mosquitto/BashToGladys.sh
With a TimeOut of 60 seconds, it then displays:
[16/02/2023 10:35:05] La température du Raspberry est de 39.4°c.
[16/02/2023 10:36:05] La température du Raspberry est de 40.4°c.
[16/02/2023 10:37:05] La température du Raspberry est de 40.4°c.
[16/02/2023 10:38:05] La température du Raspberry est de 39.9°c.
…
And the value is properly updated in Gladys.
If the mosquitto_pub command does not exist, the script displays:
[Erreur] ArrĂŞt du script car la commande mosquitto_pub est introuvable.
Pour ubuntu : sudo apt-get install mosquitto-clients
If everything works, you just need to run the script at Raspberry startup (crontab or other).
Limitation :
It is only possible to send integers and floating point numbers to Gladys.
Sending text does not work, no error is emitted but nothing is displayed.
Other tutorials:
TUTORIAL n°2: Run a BASH command via a Gladys action., in the message below.
TUTORIAL n°3: Indicate the presence of a user via their phone’s Wi-FI.
TUTORIAL n°4: Managing your vacuum robot under Valetudo in Gladys.