Skip to content

Commit e7bfabf

Browse files
authored
Update run_local.markdown
1 parent d42bfbd commit e7bfabf

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

source/hassio/run_local.markdown

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,51 @@ while true
4747
do
4848
if OUTPUT="$(/read_my_sensor.sh)"
4949
then
50-
mosquitto_pub -h "$MQTT_SERVER" -p "$MQTT_PORT" -t "$TOPIC" -m "$OUTPUT" || true
50+
mosquitto_pub -h "$MQTT_SERVER" -p "$MQTT_PORT" -u "$USER" -P "$PASSWORD" -t "$TOPIC" -m "$OUTPUT" || true
5151
else
5252
echo "$(data) [ERROR] can't read sensor: $OUTPUT"
5353
fi
5454

5555
sleep "$WAIT_TIME"
5656
done
57+
5758
```
5859

5960
### {% linkable_title Commands %}
61+
Short story of that caption: We wait on incoming data from mqtt broker to do some things. We can also use on HomeAssistant input_boolean that trigger a automation to publish a custom command to mqtt topic they can process multible things in one add-on.
62+
63+
Our Dockerfile need to install:
64+
65+
```
66+
RUN apk --no-cache add tzdata jq mosquitto-clients
67+
```
68+
69+
Now we can process it with `run.sh`:
70+
```bash
71+
#!/bin/bash
72+
set -e
73+
74+
CONFIG_PATH=/data/options.json
6075

76+
# possible options for processing
77+
MQTT_SERVER=$(jq --raw-output '.server' $CONFIG_PATH)
78+
MQTT_PORT=$(jq --raw-output '.port' $CONFIG_PATH)
79+
TOPIC=$(jq --raw-output '.topic' $CONFIG_PATH)
80+
USER=$(jq --raw-output '.user' $CONFIG_PATH)
81+
PASSWORD=$(jq --raw-output '.password' $CONFIG_PATH)
82+
83+
# read data
84+
while read -r message
85+
do
86+
if [ "$message" == "on" ]; then
87+
/do_command_on.sh || true
88+
else
89+
/do_command_off.sh || true
90+
fi
91+
92+
done <<(mosquitto_sub -h "$MQTT_SERVER" -p "$MQTT_PORT" -u "$USER" -P "$PASSWORD" -t "$TOPIC" -q 1)
93+
94+
```
6195
6296
6397
[mqtt-addon]: /addons/mosquitto/

0 commit comments

Comments
 (0)