Skip to content

Commit eda6d53

Browse files
committed
config
1 parent dd29454 commit eda6d53

File tree

5 files changed

+95
-57
lines changed

5 files changed

+95
-57
lines changed

Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ USER appuser
1212
COPY --from=builder /build/prg /app/
1313
WORKDIR /app
1414
CMD ["./prg"]
15+
VOLUME /config

README.md

+67-54
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# mqtt-timer
1+
# MQTT-Timer
22

3-
Programmable Timer for MQTT
3+
Programmable Timer for MQTT messaging.
44

55
[![Build/Test](https://github.com/Legobas/mqtt-timer/actions/workflows/go.yml/badge.svg)](https://github.com/Legobas/mqtt-timer/actions/workflows/go.yml)
66
[![CI/CD](https://github.com/Legobas/mqtt-timer/actions/workflows/build.yml/badge.svg)](https://github.com/Legobas/mqtt-timer/actions/workflows/build.yml)
@@ -9,9 +9,18 @@ Programmable Timer for MQTT
99
[![Docker Stars](https://badgen.net/docker/stars/legobas/mqtt-timer?icon=docker&label=stars)](https://hub.docker.com/r/legobas/mqtt-timer)
1010
[![Docker Image Size](https://badgen.net/docker/size/legobas/mqtt-timer?icon=docker&label=image%20size)](https://hub.docker.com/r/legobas/mqtt-timer)
1111

12-
MQTT-Timer is a programmable timer for MQTT messaging.
13-
Common daily or weekly timers can be specified in the configuration.
14-
Any timers can be added by MQTT JSON messages.
12+
## Features
13+
14+
* Standard timers are configurable
15+
* Timers can fire daily or at specific days of the week
16+
* Timers can use daily sunrise & sunset times based on the latitude/longitude coordinates
17+
* Timers can have a fixed offset before or after a timestamp
18+
* Timers can have a random offset before or after a timestamp
19+
* Timers can be removed/reset
20+
* Programmable timers can be specified by MQTT JSON Messages
21+
* Programmable timers are repeatable
22+
* Programmable timers can wait for a random number of seconds/minutes before being fired
23+
* A range of programmable timers can be set with one MQTT message
1524

1625
## Installation
1726

@@ -21,51 +30,57 @@ $ go get -u github.com/Legobas/mqtt-timer
2130

2231
# Configuration
2332

24-
MQTT-Timer can be configured with the `config.yml` yaml configuration file.
25-
26-
27-
| Config item | Description |
28-
| ------------------------- | ------------------------------------------------------------- |
29-
| latitude/longitude | GPS location used for Sunrise/Sunset |
30-
| MQTT |
31-
| URL | MQTT Server URL |
32-
| Username/Password | MQTT Server Credentials |
33-
| QOS | MQTT Server Quality Of Service |
34-
| Retain | MQTT Server Retain messages |
35-
| Timers |
36-
| id | Unique ID for this message (mandatory) |
37-
| time | Time in 15:04 or 15:04:05 format |
38-
| cron | Cron in '30 7 * * *' or '15 30 7 * * *' (with seconds) format |
39-
| topic | MQTT Topic |
40-
| message | simple string or JSON |
41-
| before, after | offset: fixed number of second or minutes |
42-
| randomBefore, randomAfter | offset: random number of second or minutes |
33+
MQTT-Timer can be configured with the `mqtt-timer.yml` yaml configuration file.
34+
The `mqtt-timer.yml` file has to exist in one of the following locations:
35+
36+
* A config directory in de filesystem root: `/config/mqtt-timer.yml`
37+
* A .config directory in the user home directory `~/.config/mqtt-timer.yml`
38+
* The current working directory
39+
40+
## Configuration options
41+
42+
| Config item | Description |
43+
| ------------------------- | ----------------------------------------------------------------- |
44+
| latitude/longitude | GPS location used for Sunrise/Sunset |
45+
| **MQTT** | |
46+
| URL | MQTT Server URL |
47+
| Username/Password | MQTT Server Credentials |
48+
| QOS | MQTT Server Quality Of Service |
49+
| Retain | MQTT Server Retain messages |
50+
| **Timers** | |
51+
| id | Unique ID for this message (mandatory) |
52+
| time | Time in `15:04` or `15:04:05` format |
53+
| cron | Cron in '`30 7 * * *`' or '`15 30 7 * * *`' (with seconds) format |
54+
| topic | MQTT Topic |
55+
| message | simple string or JSON |
56+
| before, after | offset: fixed number of seconds or minutes |
57+
| randomBefore, randomAfter | offset: random number of seconds or minutes |
4358

4459
Example config.yml:
4560

4661
```yml
47-
latitude: 51.50722
48-
longitude: -0.1275
49-
50-
mqtt:
51-
url: "tcp://<MQTT SERVER>:1883"
52-
username: <MQTT USERNAME>
53-
password: <MQTT PASSWORD>
54-
qos: 0
55-
retain: false
56-
57-
timers:
58-
- id: 001
59-
time: 22:30
60-
description: Light outside on at 22:30
61-
topic: shellies/Shelly1/relay/0/command
62-
message: on
63-
- id: 002
64-
time: sunrise
65-
before: 20 minutes
66-
description: Light outside off 20 minutes before sunrise
67-
topic: shellies/Shelly1/relay/0/command
68-
message: off
62+
latitude: 51.50722
63+
longitude: -0.1275
64+
65+
mqtt:
66+
url: "tcp://<MQTT SERVER>:1883"
67+
username: <MQTT USERNAME>
68+
password: <MQTT PASSWORD>
69+
qos: 0
70+
retain: false
71+
72+
timers:
73+
- id: 001
74+
time: 22:30
75+
description: Light outside on at 22:30
76+
topic: shellies/Shelly1/relay/0/command
77+
message: on
78+
- id: 002
79+
time: sunrise
80+
before: 20 minutes
81+
description: Light outside off 20 minutes before sunrise
82+
topic: shellies/Shelly1/relay/0/command
83+
message: off
6984
```
7085
7186
See also: [example/config.yml](example/config.yml)
@@ -77,25 +92,23 @@ See also: [example/config.yml](example/config.yml)
7792
```json
7893
{
7994
"id": "msg001",
80-
"description": "Dim light from now every 10 min.",
81-
"start": "15:03:00",
82-
"repeat": "1 min",
83-
"repeatTimes": 10,
95+
"description": "Dim light",
96+
"start": "now",
8497
"randomAfter": "1 min",
85-
"topic": "homeassistant/light04/dimmer",
98+
"topic": "/homeassistant/light04/dimmer",
8699
"message": "10%",
87-
"messages": ["100%", "80%", "60%", "20%", "0%"]
88100
}
89-
101+
```
90102

91103
## Timezone
92104

93105
By default all the times will be in the timezone of the server.
94106
In a docker environment the timezone can be specified by the TZ environment variable.
107+
95108
For example:
96109

97110
```bash
98-
$ docker run -e TZ=Europe/London mqtt-timer
111+
$ docker run -e TZ=America/New_York mqtt-timer
99112
```
100113

101114
## Credits

config.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ import (
55
"fmt"
66
"io/ioutil"
77
"log"
8+
"os"
9+
"path/filepath"
810

911
"gopkg.in/yaml.v3"
1012
)
1113

14+
const (
15+
CONFIG_FILE = "mqtt-timer.yml"
16+
CONFIG_DIR = ".config"
17+
CONFIG_ROOT = "/config"
18+
)
19+
1220
type Mqtt struct {
1321
Url string `yaml:"url"`
1422
Username string `yaml:"username"`
@@ -43,9 +51,24 @@ type Config struct {
4351
func getConfig() Config {
4452
var config Config
4553

46-
data, err := ioutil.ReadFile("./config/config.yml")
54+
configFile := filepath.Join(CONFIG_ROOT, CONFIG_FILE)
55+
msg := configFile
56+
data, err := ioutil.ReadFile(configFile)
4757
if err != nil {
48-
log.Fatal(err)
58+
homedir, _ := os.UserHomeDir()
59+
configFile := filepath.Join(homedir, CONFIG_DIR, CONFIG_FILE)
60+
msg += ", " + configFile
61+
data, err = ioutil.ReadFile(configFile)
62+
}
63+
if err != nil {
64+
workingdir, _ := os.Getwd()
65+
configFile := filepath.Join(workingdir, CONFIG_FILE)
66+
msg += ", " + configFile
67+
data, err = ioutil.ReadFile(configFile)
68+
}
69+
if err != nil {
70+
msg = "Configuration file could not be found: " + msg
71+
log.Fatal(msg)
4972
}
5073

5174
err = yaml.Unmarshal(data, &config)

mqtt-timer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ import (
1515
"github.com/nathan-osman/go-sunrise"
1616
)
1717

18-
var config = getConfig()
18+
var config Config
1919
var dailyTimers []Timer
2020
var scheduler *gocron.Scheduler
2121

2222
func init() {
2323
rand.Seed(time.Now().UnixNano())
2424
log.SetFlags(0)
25+
config = getConfig()
2526
}
2627

2728
func handleEvent(timer Timer) {

config/config.yml mqtt-timer.yml

File renamed without changes.

0 commit comments

Comments
 (0)