Skip to content

Commit

Permalink
0.2.15 - config more!
Browse files Browse the repository at this point in the history
  • Loading branch information
alump committed May 17, 2024
1 parent a557fa1 commit bae6efc
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.2.15

- make rounding (decimals) configurable

## 0.2.14

- Deprecated Python replaced
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Updates sensor.sht30_temperature and sensor.sht30_humidity entities (unless diff
with current values every 60 seconds (can be changed).

## TODO features
- Sampling amount and frequency and amount of decimals to config
- Maybe support for Fahrenheit.
- Support for second i2c port, other devices?
- Better logging (still better)
Expand Down
10 changes: 7 additions & 3 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: "SHT30 i2c Sensor"
description: "Reads temperature and humidity from SHT30 i2c sensor"
version: "0.2.14"
version: "0.2.15"
slug: "sht30_sensor"
url: https://github.com/alump/alump-sht30
init: false
Expand All @@ -25,12 +25,16 @@ options:
temperature_friendly_name: Local Temperature (SHT30)
humidity_entity_id: sensor.sht30_humidity
humidity_friendly_name: Local Relative Humidity (SHT30)
send_interval: 60
samples: 20
send_interval: 30
samples: 10
temperature_decimals: 2
humidity_decimals: 1
schema:
temperature_entity_id: str
temperature_friendly_name: str
humidity_entity_id: str
humidity_friendly_name: str
send_interval: int(1,86400)
samples: int(1,86400)
temperature_decimals: int(0,5)
humidity_decimals: int(0,3)
3 changes: 2 additions & 1 deletion env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export CONFIG_HUMIDITY_ENTITY_ID=$(bashio::config 'humidity_entity_id')
export CONFIG_HUMIDITY_FRIENDLY_NAME=$(bashio::config 'humidity_friendly_name')
export CONFIG_SEND_INTERVAL=$(bashio::config 'send_interval')
export CONFIG_SAMPLES=$(bashio::config 'samples')

export CONFIG_TEMPERATURE_DECIMALS=$(bashio::config 'temperature_decimals')
export CONFIG_HUMIDITY_DECIMALS=$(bashio::config 'humidity_decimals')
3 changes: 3 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ source ./env.sh

bashio::log.info "Testing SHT30 reading..."
python3 test.py

bashio::log.info "Starting SHT30 updater..."
bashio::log.info "Temperature entity ID: ${CONFIG_TEMPERATURE_ENTITY_ID}"
bashio::log.info "Temperature friendly name: ${CONFIG_TEMPERATURE_FRIENDLY_NAME}"
bashio::log.info "Humidity entity ID: ${CONFIG_HUMIDITY_ENTITY_ID}"
bashio::log.info "Humidity friendly name: ${CONFIG_HUMIDITY_FRIENDLY_NAME}"
bashio::log.info "Send interval (seconds): ${CONFIG_SEND_INTERVAL}"
bashio::log.info "Samples: ${CONFIG_SAMPLES}"
bashio::log.info "Temperature decimals: ${CONFIG_TEMPERATURE_DECIMALS}"
bashio::log.info "Humidity decimals: ${CONFIG_HUMIDITY_DECIMALS}"
python3 sendvalues.py
21 changes: 15 additions & 6 deletions sendvalues.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def roundPartial(val, res):
logger.warning('CONFIG_SEND_INTERVAL not defined!')
if os.environ.get('CONFIG_SAMPLES','') == '':
logger.warning('CONFIG_SAMPLES not defined!')
if os.environ.get('CONFIG_TEMPERATURE_DECIMALS','') == '':
logger.warning('CONFIG_TEMPERATURE_DECIMALS not defined!')
if os.environ.get('CONFIG_HUMIDITY_DECIMALS','') == '':
logger.warning('CONFIG_HUMIDITY_DECIMALS not defined!')


TEMP_ENTITY_ID = os.environ.get('CONFIG_TEMPERATURE_ENTITY_ID','sensor.sht30_temperature')
TEMP_FNAME = os.environ.get('CONFIG_TEMPERATURE_FRIENDLY_NAME','Local Temperature (SHT30)')
Expand All @@ -37,6 +42,10 @@ def roundPartial(val, res):
logger.info('Humidity id: "%s", name: "%s"...', HUM_ENTITY_ID, HUM_FNAME)
SEND_INTERVAL = int(os.environ.get('CONFIG_SEND_INTERVAL','60'))
SAMPLES = int(os.environ.get('CONFIG_SAMPLES','20'))
logger.info('%d samples with interval of %d seconds', SAMPLES, SEND_INTERVAL)
TEMPERATURE_DECIMALS = int(os.environ.get('CONFIG_TEMPERATURE_DECIMALS','2'))
HUMIDITY_DECIMALS = int(os.environ.get('CONFIG_HUMIDITY_DECIMALS','1'))
logger.info('temperature decimals: %d, humidity decimals: %d', TEMPERATURE_DECIMALS, HUMIDITY_DECIMALS)
URL = 'http:https://supervisor/core/api/states/'

TOKEN = os.environ.get('SUPERVISOR_TOKEN','?')
Expand Down Expand Up @@ -69,8 +78,8 @@ def roundPartial(val, res):
statHumSamples.append(sampleHum)
time.sleep(1)

cTemp = round(statistics.harmonic_mean(tempSamples), 1)
humidity = round(statistics.harmonic_mean(humSamples), 1)
cTemp = round(statistics.harmonic_mean(tempSamples), TEMPERATURE_DECIMALS)
humidity = round(statistics.harmonic_mean(humSamples), HUMIDITY_DECIMALS)

now = datetime.now(timezone.utc)
timestamp = now.isoformat()
Expand All @@ -80,7 +89,7 @@ def roundPartial(val, res):
if lastTemp != cTemp or tempTimeDelta.total_seconds() > 300:
url = URL + TEMP_ENTITY_ID
logger.info('Sending %s = %f to %s...', TEMP_ENTITY_ID, cTemp, url)
data = {'attributes': { 'unit_of_measurement': '\N{DEGREE SIGN}C'}}
data = {'attributes': { 'unit_of_measurement': '\N{DEGREE SIGN}C' }}
data['entity'] = TEMP_ENTITY_ID
data['state'] = cTemp
data['last_updated'] = timestamp
Expand Down Expand Up @@ -108,14 +117,14 @@ def roundPartial(val, res):
if lastHum != humidity or humTimeDelta.total_seconds() > 300:
url = URL + HUM_ENTITY_ID
logger.info('Sending %s = %f to %s...', HUM_ENTITY_ID, humidity, url)
data = {'attributes': { 'unit_of_measurement': '%'}}
data = {'attributes': { 'unit_of_measurement': '%' }}
data['entity'] = HUM_ENTITY_ID
data['state'] = humidity
data['last_updated'] = timestamp
data['attributes']['friendly_name'] = HUM_FNAME
data['attributes']['stdev'] = round(statistics.stdev(statHumSamples), 3)
data['attributes']['min'] = round(min(statHumSamples), 3)
data['attributes']['max'] = round(max(statHumSamples), 3)
data['attributes']['min'] = round(min(statHumSamples), 2)
data['attributes']['max'] = round(max(statHumSamples), 2)
data['attributes']['samples'] = len(statHumSamples)
if lastHum >= 0 and lastHum <= 100:
data['attributes']['delta'] = humidity - lastHum
Expand Down
8 changes: 7 additions & 1 deletion translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ configuration:
description: How often new values will be sent to HomeAssistant
samples:
name: Amount of samples taken
description: How many times values are measured inside one send round
description: How many times values are measured inside one send round
temperature_decimals:
name: Amount of decimals in temperature
description: Eg. 2 will result values like 23.45 C
humidity_decimals:
name: Amount of decimals in humidity
description: Eg. 1 will result values like 23.4 %
8 changes: 7 additions & 1 deletion translations/fi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ configuration:
description: Kuinka usein add-on lähettää uuden arvon minimissään
samples:
name: Kuinka monta mittausta jokaiselle lähetykselle
description: Kuinka monta mittausta minimissään suoritetaan
description: Kuinka monta mittausta minimissään suoritetaan
temperature_decimals:
name: Desimaalien lukumäärä lämpötilassa
description: Esim. arvo 2 tuottaa arvoja kuten 23.45 C
humidity_decimals:
name: Desimaalien lukumäärä ilmankosteudessa
description: Esim. arvo 1 tuottaa arvoja kuten 23.4 %

0 comments on commit bae6efc

Please sign in to comment.