TUTORIAL No.2: Run a BASH command via a Gladys action.
The idea is to monitor a Gladys device topic from BASH and execute the appropriate commands.
Gladys side:
-
Device creation:
Name : Appel BASH
External ID : mqtt:Salle:AppelBASH
Room : Salle -
Add a feature of type Button Click (for example):
Name : Appel BASH
Feature external ID : mqtt:AppelBASH:Action
Min value : 0
Max value : 9 (Allows 10 different actions)
Keep state history : Yes
Is it a sensor : No
MQTT topic to listen to : You can note the topic that will be used later. -
Define a scene:
Trigger : One of your choice (such as the state change of a real button, for example).
Action : Control a device.
Device : Appel BASH
Value : 1 (between 0 and 9)
You will only need to trigger the scene once the BASH scripts are finished.
BASH scripts.
-
Reuse the common script /opt/mosquitto/MQTTConfig.sh from TUTORIAL No.1.
-
Create the script /opt/mosquitto/GladysToBash.sh :
It will listen to the topic of the device created in Gladys.
#!/bin/bash
#####################################################################
## Monitoring a Gladys topic with execution of BASH commands ##
#####################################################################
### Variables to customize
# You must customize the CASE in the WHILE loop.
# Device topic address
# Available in Gladys > Integration > MQTT > Devices > Appel BASH > Button Click > MQTT topic to listen to
Topic="gladys/device/mqtt:salle:AppelBASH/feature/mqtt:AppelBASH:Action/state"
### Variables to customize
# Load the common file
source /opt/mosquitto/MQTTConfig.sh
# Requires the mosquitto_sub command from the mosquitto-clients package
! CommandCheck "mosquitto_sub:mosquitto-clients" \u0026\u0026 exit 1
echo "Le script attend de recevoir les commandes de Gladys." 1\u003e\u00262
echo "Il ne rend la main que si des break et exit sont intégrés à la boucle while." 1\u003e\u00262
# Loop continuously listening to the Gladys device topic
# -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 Topic of the device created in Gladys, defined at the beginning of the script
# Capture the returns into the variable Value
while read Value
do
# Skip processing if Value is not between 0 and 9
if [[ "${Value}" != [0-9] ]]
then
echo -e "[${ORANGE}Attention${RAZ}] Valeur ${Value} inconnue..." 1\u003e\u00262
continue
fi
echo -e "[${FUCHSIA}$(date +'%x %X')${RAZ}] Exécution de la commande n°${Value}."
# Case to perform the correct action depending on Value
# Use of echo to avoid actually executing the commands in this TUTORIAL
case "${Value}" in
# Reboot the Raspberry
0) echo sudo reboot ;;
# Wake up a PC via WakeOnLan in the background so as not to block the loop
1) echo wakeonlan XX:YY:ZZ:ZZ:YY:XX \u0026 ;;
# Start an alarm via a wav file in vlc in the background so as not to block the loop
2) echo vlc "/opt/alarme.wav" \u0026 ;;
# Kill all vlc commands
3) echo killall vlc ;;
# Any other actions of your choice...
[3-9]) echo "Réalisation de l'action ${Value}.";;
esac
done \u003c \u003c(mosquitto_sub -u "${User}" -P "${Pass}" -t "${Topic}")
You must adapt the variables between ### Variables to customize as well as the actions in the case of the while loop.
For information, it is possible to specify the number of messages expected by mosquitto_sub before it returns control:
mosquitto_sub -u "${User}" -P "${Pass}" -t "${Topic}" -C x # Where x is the number of messages
- Script execution:
Just call the script with a simple:
bash /opt/mosquitto/GladysToBash.sh
The script does not return control to the prompt, this is normal; the loop is infinite.
It displays:
The script is waiting to receive commands from Gladys.
It only returns control if break and exit are integrated into the while loop.
- Executing the desired scene from Gladys:
Nothing special here; just execute the scene manually or via another device.
The shell then displays:
[16/02/2023 11:35:05] Execution of command No.5.
[16/02/2023 11:35:55] Execution of command No.2.
[16/02/2023 11:45:55] Execution of command No.3.
If everything works, the only thing left is to run the script at Raspberry startup.
Other tutorials:
TUTORIAL No.1: Display the Raspberry temperature in Gladys.
TUTORIAL No.3: Indicate a user’s presence via their phone’s Wi‑Fi.
TUTORIAL No.4: Managing your robot vacuum running Valetudo in Gladys.