-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
395 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#!/bin/bash | ||
# | ||
# you MUST change at least one of the following API keys, and the lat lng variables. | ||
# openweatherAPI, weatherAPIkey, weatherbitAPIkey, climacellAPIkey | ||
# lat = your latitude | ||
# lon = your longitude | ||
# probabilityOver = Enable rain delay if probability of rain today is grater than this number. | ||
# Range is 0 to 100, so 50 is 50% | ||
# sprinklerdEnableDelay = the URL to SprinklerD | ||
# | ||
# The below are NOT valid, you MUST change them to your information. | ||
|
||
# API Keys from the following are supported | ||
# openweathermap.org weatherapi.com weatherbit.io climacell.co | ||
|
||
openweatherAPIkey='-----' | ||
weatherbitAPIkey='-----' | ||
climacellAPIkey='-----' | ||
weatherAPIkey='-----' | ||
|
||
lat='42.3601' | ||
lon='-71.0589' | ||
|
||
# 101 means don't set rain delay from this script, use the SprinklerD config (webUI) to decide if to set delay | ||
probabilityOver=101 | ||
|
||
# If you are not running this from the same host as SprinklerD, then change localhost in the below. | ||
sprinklerdHost="http://localhost:80" | ||
|
||
|
||
|
||
######################################################################################################################## | ||
# | ||
# Should not need to edit below this line. | ||
# | ||
######################################################################################################################## | ||
|
||
|
||
sprinklerdEnableDelay="$sprinklerdHost/?type=option&option=24hdelay&state=reset" | ||
sprinklerdProbability="$sprinklerdHost/?type=sensor&sensor=chanceofrain&value=" | ||
|
||
openweatherURL="https://api.openweathermap.org/data/2.5/onecall?lat=$lat&lon=$lon&appid=$openweatherAPIkey&exclude=current,minutely,hourly,alerts" | ||
weatherbitURL="https://api.weatherbit.io/v2.0/forecast/daily?key=$weatherbitAPIkey&lat=$lat&lon=$lon" | ||
climacellURL="https://data.climacell.co/v4/timelines?location=$lat%2C$lon&fields=precipitationProbability×teps=1d&units=metric&apikey=$climacellAPIkey" | ||
weatherAPIlURL="http://api.weatherapi.com/v1/forecast.json?key=$weatherAPIkey&q=$lat,$lon&days=1&aqi=no&alerts=no" | ||
|
||
true=0 | ||
false=1 | ||
|
||
echoerr() { printf "%s\n" "$*" >&2; } | ||
echomsg() { if [ -t 1 ]; then echo "$@" 1>&2; fi; } | ||
|
||
function getURL() { | ||
url=$1 | ||
jq=$2 | ||
factor=$3 | ||
|
||
JSON=$(curl -s "$url") | ||
|
||
if [ $? -ne 0 ]; then | ||
echoerr "Error reading URL, '$1' please check!" | ||
echoerr "Maybe you didn't configure your API and location?" | ||
echo 0; | ||
return $false | ||
else | ||
probability=$(echo $JSON | jq "$jq" ) | ||
fi | ||
|
||
probability=$(echo "$probability * $factor / 1" | bc ) | ||
echo $probability | ||
return $true | ||
} | ||
|
||
# Test we have everything installed | ||
command -v curl >/dev/null 2>&1 || { echoerr "curl is not installed. Aborting!"; exit 1; } | ||
command -v jq >/dev/null 2>&1 || { echoerr "jq is not installed. Aborting!"; exit 1; } | ||
command -v bc >/dev/null 2>&1 || { echoerr "bc not installed. Aborting!"; exit 1; } | ||
|
||
pop=-1 | ||
|
||
if [ "$#" -lt 1 ]; then | ||
echomsg "Pass at least one command line parameter (openweather, weatherbit, climacell, weatherapi)" | ||
exit 1; | ||
fi | ||
|
||
for var in "$@" | ||
do | ||
case "$var" in | ||
openweather) | ||
if [ "$openweatherAPIkey" == "-----" ]; then echomsg "missing OpenWeather API key" && continue; fi | ||
if probability=$(getURL "$openweatherURL" '.["daily"][0].pop' "100"); then | ||
echomsg "OpenWeather probability of rain today is $probability%" | ||
pop=$(echo "if($probability>$pop)print $probability else print $pop" | bc -l) | ||
fi | ||
;; | ||
weatherbit) | ||
if [ "$weatherbitAPIkey" == "-----" ]; then echomsg "missing WeatherBit API key" && continue; fi | ||
if probability=$(getURL "$weatherbitURL" '.data[0].pop' "1"); then | ||
echomsg "WeatherBit probability of rain today is $probability%" | ||
pop=$(echo "if($probability>$pop)print $probability else print $pop" | bc -l) | ||
fi | ||
;; | ||
climacell) | ||
if [ "$climacellAPIkey" == "-----" ]; then echomsg "missing ClimaCell API key" && continue; fi | ||
if probability=$(getURL "$climacellURL" '.data.timelines[0].intervals[0].values.precipitationProbability' "1"); then | ||
echomsg "ClimaCell probability of rain today is $probability%" | ||
pop=$(echo "if($probability>$pop)print $probability else print $pop" | bc -l) | ||
fi | ||
;; | ||
weatherapi) | ||
if [ "$weatherAPIkey" == "-----" ]; then echomsg "missing WeatherAPI API key" && continue; fi | ||
if probability=$(getURL "$weatherAPIlURL" '.forecast.forecastday[0].day.daily_chance_of_rain' "1"); then | ||
echomsg "WeatherAPI probability of rain today is $probability%" | ||
pop=$(echo "if($probability>$pop)print $probability else print $pop" | bc -l) | ||
fi | ||
;; | ||
esac | ||
done | ||
|
||
# Remove all new line, carriage return, tab characters | ||
# from the string, to allow integer comparison | ||
pop="${pop//[$'\t\r\n ']}" | ||
|
||
if [ "$pop" -lt 0 ]; then | ||
echoerr "Error reading Probability, please check!" | ||
exit 1 | ||
fi | ||
|
||
echomsg "Chance of rain $pop%" | ||
|
||
curl -s "$sprinklerdProbability$pop" > /dev/null | ||
|
||
if (( $(echo "$pop > $probabilityOver" | bc -l) )); then | ||
echomsg "Enabeling rain delay" | ||
curl -s "$sprinklerdEnableDelay" > /dev/null | ||
fi | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
|
||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <strings.h> | ||
|
||
|
||
#include "mongoose.h" | ||
#include "config.h" | ||
#include "net_services.h" | ||
#include "version.h" | ||
|
||
#define HASS_DEVICE "\"identifiers\": " \ | ||
"[\"SprinklerD\"]," \ | ||
" \"sw_version\": \"" SD_VERSION "\"," \ | ||
" \"model\": \"Sprinkler Daemon\"," \ | ||
" \"name\": \"SprinklerD\"," \ | ||
" \"manufacturer\": \"SprinklerD\"," \ | ||
" \"suggested_area\": \"pool\"" | ||
|
||
#define HASS_AVAILABILITY "\"payload_available\" : \"1\"," \ | ||
"\"payload_not_available\" : \"0\"," \ | ||
"\"topic\": \"%s/" MQTT_LWM_TOPIC "\"" | ||
|
||
const char *HASSIO_TEXT_SENSOR_DISCOVER = "{" | ||
"\"device\": {" HASS_DEVICE "}," | ||
"\"availability\": {" HASS_AVAILABILITY "}," | ||
"\"type\": \"sensor\"," | ||
"\"unique_id\": \"%s\"," | ||
"\"name\": \"%s\"," | ||
"\"state_topic\": \"%s/%s\"," | ||
"\"icon\": \"mdi:card-text\"" | ||
"}"; | ||
|
||
const char *HASSIO_SWITCH_DISCOVER = "{" | ||
"\"device\": {" HASS_DEVICE "}," | ||
"\"availability\": {" HASS_AVAILABILITY "}," | ||
"\"type\": \"switch\"," | ||
"\"unique_id\": \"%s\"," | ||
"\"name\": \"%s\"," | ||
"\"state_topic\": \"%s/%s\"," | ||
"\"command_topic\": \"%s/%s/set\"," | ||
"\"payload_on\": \"1\"," | ||
"\"payload_off\": \"0\"," | ||
"\"qos\": 1," | ||
"\"retain\": false" | ||
"}"; | ||
|
||
const char *HASSIO_VALVE_DISCOVER = "{" | ||
"\"device\": {" HASS_DEVICE "}," | ||
"\"availability\": {" HASS_AVAILABILITY "}," | ||
"\"type\": \"valve\"," | ||
"\"device_class\": \"water\"," | ||
"\"unique_id\": \"%s\"," // sprinklerd_zone_1 | ||
"\"name\": \"Zone %d (%s)\"," // 1 island | ||
"\"state_topic\": \"%s/zone%d\"," // 1 | ||
"\"command_topic\": \"%s/zone%d/set\"," // 1 | ||
"\"value_template\": \"{%% set values = { '0':'closed', '1':'open'} %%}{{ values[value] if value in values.keys() else 'closed' }}\"," | ||
"\"payload_open\": \"1\"," | ||
"\"payload_close\": \"0\"," | ||
"\"qos\": 1," | ||
"\"retain\": false" | ||
"}"; | ||
|
||
void publish_mqtt_hassio_discover(struct mg_connection *nc) | ||
{ | ||
char msg[2048]; | ||
char topic[256]; | ||
char id[128]; | ||
int i; | ||
|
||
|
||
sprintf(id,"sprinklerd_status"); | ||
sprintf(topic, "%s/sensor/sprinklerd/%s/config", _sdconfig_.mqtt_ha_dis_topic, id); | ||
sprintf(msg, HASSIO_TEXT_SENSOR_DISCOVER, | ||
_sdconfig_.mqtt_topic, | ||
id, | ||
"Status", | ||
_sdconfig_.mqtt_topic, "status" ); | ||
send_mqtt_msg(nc, topic, msg); | ||
|
||
sprintf(id,"sprinklerd_active_zone"); | ||
sprintf(topic, "%s/sensor/sprinklerd/%s/config", _sdconfig_.mqtt_ha_dis_topic, id); | ||
sprintf(msg, HASSIO_TEXT_SENSOR_DISCOVER, | ||
_sdconfig_.mqtt_topic, | ||
id, | ||
"Active Zone", | ||
_sdconfig_.mqtt_topic, "active" ); | ||
send_mqtt_msg(nc, topic, msg); | ||
|
||
|
||
|
||
sprintf(id,"sprinklerd_calendar"); | ||
sprintf(topic, "%s/switch/sprinklerd/%s/config", _sdconfig_.mqtt_ha_dis_topic, id); | ||
sprintf(msg, HASSIO_SWITCH_DISCOVER, | ||
_sdconfig_.mqtt_topic, | ||
id, | ||
"Calendar Schedule", | ||
_sdconfig_.mqtt_topic, "calendar", | ||
_sdconfig_.mqtt_topic, "calendar" ); | ||
send_mqtt_msg(nc, topic, msg); | ||
|
||
sprintf(id,"sprinklerd_24hdelay"); | ||
sprintf(topic, "%s/switch/sprinklerd/%s/config", _sdconfig_.mqtt_ha_dis_topic, id); | ||
sprintf(msg, HASSIO_SWITCH_DISCOVER, | ||
_sdconfig_.mqtt_topic, | ||
id, | ||
"24 Hour rain delay", | ||
_sdconfig_.mqtt_topic, "24hdelay", | ||
_sdconfig_.mqtt_topic, "24hdelay" ); | ||
send_mqtt_msg(nc, topic, msg); | ||
|
||
sprintf(id,"sprinklerd_cycleallzones"); | ||
sprintf(topic, "%s/switch/sprinklerd/%s/config", _sdconfig_.mqtt_ha_dis_topic, id); | ||
sprintf(msg, HASSIO_SWITCH_DISCOVER, | ||
_sdconfig_.mqtt_topic, | ||
id, | ||
"Cycle All Zones", | ||
_sdconfig_.mqtt_topic, "cycleallzones", | ||
_sdconfig_.mqtt_topic, "cycleallzones" ); | ||
send_mqtt_msg(nc, topic, msg); | ||
|
||
|
||
|
||
|
||
//for (i=(_sdconfig_.master_valve?0:1); i <= _sdconfig_.zones ; i++) | ||
// Don't publish zome0/master valve to ha | ||
for (i=1; i <= _sdconfig_.zones ; i++) | ||
{ | ||
sprintf(id,"sprinklerd_zone_%d", _sdconfig_.zonecfg[i].zone); | ||
sprintf(topic, "%s/valve/sprinklerd/%s/config", _sdconfig_.mqtt_ha_dis_topic, id); | ||
sprintf(msg, HASSIO_VALVE_DISCOVER, | ||
_sdconfig_.mqtt_topic, | ||
id, | ||
_sdconfig_.zonecfg[i].zone, | ||
_sdconfig_.zonecfg[i].name, | ||
_sdconfig_.mqtt_topic,_sdconfig_.zonecfg[i].zone, | ||
_sdconfig_.mqtt_topic,_sdconfig_.zonecfg[i].zone | ||
); | ||
|
||
send_mqtt_msg(nc, topic, msg); | ||
/* | ||
length += sprintf(buffer+length, "{\"type\" : \"zone\", \"zone\": %d, \"name\": \"%s\", \"state\": \"%s\", \"duration\": %d, \"id\" : \"zone%d\" },", | ||
_sdconfig_.zonecfg[i].zone, | ||
_sdconfig_.zonecfg[i].name, | ||
(digitalRead(_sdconfig_.zonecfg[i].pin)==_sdconfig_.zonecfg[i].on_state?"on":"off"), | ||
_sdconfig_.zonecfg[i].default_runtime * 60, | ||
_sdconfig_.zonecfg[i].zone); | ||
*/ | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef HASSIO_H_ | ||
#define HASSIO_H_ | ||
|
||
void publish_mqtt_hassio_discover(struct mg_connection *nc); | ||
|
||
#endif |
Oops, something went wrong.