Do you need a simple way to send data from Shelly smart plugs to an InfluxDB database without complicating things with dependencies? This is your script.
It only requires having the command-line tools curl and jq installed.
You need to have access credentials (password) configured for your Shelly device and have the RPC API enabled. The script assumes that all devices share the same credentials (password).
Copy shelly-infuxdb.sh to /usr/local/bin
Add exec permissions to shelly-influxdb.sh
sudo chmod +x /usr/local/bin/shelly-influxdb.sh
Copy shelly-influxdb.conf to /usr/local/etc and customize/edit your settings
sh /usr/local/bin/shelly-influxdb.sh /usr/local/etc/shelly-influxdb.conf
Create service definition:
cat > /etc/systemd/system/shelly-influxdb.service
[Unit]
Description=Run shelly influxdb script every 10 seconds
After=network.target
[Service]
Type=simple
ExecStart=/bin/sh -c '/usr/local/bin/shelly-influxdb.sh /usr/local/etc/shelly-influxdb.conf'
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
NOTE: avoid log flooding (every 10 seconds) on /etc/systemd/system/shelly-influxdb.service redirecting stdout & stderr to null & hide notice/info/debug syslog messages
[Unit]
Description=Run shelly influxdb script every 10 seconds
After=network.target
[Service]
Type=simple
ExecStart=/bin/sh -c '/usr/local/bin/shelly-influxdb.sh /usr/local/etc/shelly-influxdb.conf'
Restart=always
RestartSec=10
StandardOutput=null
StandardError=null
LogLevelMax=3
[Install]
WantedBy=multi-user.target
Enable service
sudo systemctl enable shelly-influxdb.service
Reload services
sudo systemctl daemon-reload
sudo systemctl restart shelly-influxdb.service
Verify service status
sudo systemctl status shelly-influxdb.service
Here are some example (NOTE: change 111111111111 to your real MAC address) queries to visualize data in Grafana with line charts:
SELECT mean("power") FROM "autogen"."smarthome" WHERE ("brand"::tag = 'Shelly' AND "type"::tag = 'plug' AND "mac"::tag = '111111111111') AND $timeFilter GROUP BY time($__interval) fill(none)
SELECT "energy" FROM "autogen"."smarthome" WHERE ("type"::tag = 'plug' AND "brand"::tag = 'Shelly' AND "mac"::tag = '111111111111') AND $timeFilter
SELECT mean("voltage") FROM "autogen"."smarthome" WHERE ("type"::tag = 'plug' AND "brand"::tag = 'Shelly' AND "mac"::tag = '111111111111') AND $timeFilter GROUP BY time($__interval) fill(none)
SELECT mean("temperature") FROM "autogen"."smarthome" WHERE ("type"::tag = 'plug' AND "brand"::tag = 'Shelly' AND "mac"::tag = '111111111111') AND $timeFilter GROUP BY time($__interval) fill(none)
create debian package