Skip to content

Commit

Permalink
[TASK] fix avg outdoor temp sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
KartoffelToby committed Sep 22, 2022
1 parent 60b81db commit b235d9b
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.jekyll-cache/
_site/
17 changes: 11 additions & 6 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
remote_theme: just-the-docs/just-the-docs
theme: just-the-docs


# Welcome to Jekyll!
Expand All @@ -20,10 +21,12 @@ title: Better Thermostat
description: Documentation for Better Thermostat
baseurl: "/" # the subpath of your site, e.g. /blog
url: "https://bt.t-haber.de" # the base hostname & protocol for your site, e.g. http://example.com

logo: /assets/logo.png
permalink: pretty
exclude: ["node_modules/", "*.gemspec", "*.gem", "Gemfile", "Gemfile.lock", "package.json", "package-lock.json", "script/", "LICENSE.txt", "lib/", "bin/", "README.md", "Rakefile", "vendor"]

exclude: ["custom_components/", ".vscode", "node_modules/", "*.gemspec", "*.gem", "Gemfile", "Gemfile.lock", "package.json", "package-lock.json", "script/", "LICENSE.txt", "lib/", "bin/", "README.md", "Rakefile", "vendor"]
sass:
sass_dir: _sass
style: compressed
# Set a path/url to a logo that will be displayed instead of the title
#logo: "/assets/images/just-the-docs.png"

Expand Down Expand Up @@ -91,7 +94,7 @@ nav_external_links:
back_to_top: true
back_to_top_text: "Back to top"

footer_content: "Copyright &copy; 2017-2020 Patrick Marsceill. Distributed by an <a href=\"https://github.com/just-the-docs/just-the-docs/tree/main/LICENSE.txt\">MIT license.</a> <a href=\"https://www.netlify.com/\">This site is powered by Netlify.</a>"
footer_content: "Copyright &copy; 2022 Tobias Haber. Distributed by an <a href=\"https://github.com/KartoffelToby/better_thermostat/blob/master/LICENSE\">AGPL-3.0 license.</a>"

# Footer last edited timestamp
last_edit_timestamp: true # show or hide edit time - page must have `last_modified_date` defined in the frontmatter
Expand All @@ -103,15 +106,17 @@ last_edit_time_format: "%b %e %Y at %I:%M %p" # uses ruby's time format: https:/
gh_edit_link: true # show or hide edit this page link
gh_edit_link_text: "Edit this page on GitHub"
gh_edit_repository: "https://github.com/KartoffelToby/better_thermostat" # the github URL for your repo
gh_edit_branch: "main" # the branch that your docs is served from
# gh_edit_source: docs # the source that your files originate from
gh_edit_branch: "master" # the branch that your docs is served from
#gh_edit_source: docs # the source that your files originate from
gh_edit_view_mode: "tree" # "tree" or "edit" if you want the user to jump into the editor immediately

# Color scheme currently only supports "dark", "light"/nil (default), or a custom scheme that you define
color_scheme: dark

callouts_level: quiet # or loud
callouts:
link:
color: green
highlight:
color: yellow
important:
Expand Down
20 changes: 20 additions & 0 deletions _sass/custom/custom.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
a, .icon {
color: #4caf50;
}
body{
background-color: #191c1e;
}

.side-bar {
background-color: #25292c;
}

.site-header {
min-height: 120px;
}

.site-logo {
max-height: 115px;
max-width: 115px;
margin: 0 auto;
}
Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 16 additions & 5 deletions custom_components/better_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from abc import ABC
from datetime import datetime, timedelta
from random import randint

from custom_components.better_thermostat.weather import check_ambient_air_temperature
from .helpers import convert_to_float
from homeassistant.helpers import entity_platform

Expand All @@ -24,7 +26,10 @@
STATE_UNKNOWN,
)
from homeassistant.core import callback, CoreState
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.helpers.event import (
async_track_state_change_event,
async_track_time_change,
)
from homeassistant.helpers.restore_state import RestoreEntity

from . import DOMAIN
Expand Down Expand Up @@ -57,7 +62,6 @@

from .controlling import control_queue, set_hvac_mode, set_target_temperature
from .events.temperature import trigger_temperature_change
from .events.time import trigger_time
from .events.trv import trigger_trv_change
from .events.window import trigger_window_change, window_queue

Expand Down Expand Up @@ -230,6 +234,7 @@ def __init__(
self._saved_temperature = None
self._last_reported_valve_position_update_wait_lock = asyncio.Lock()
self._last_send_target_temp = None
self._last_avg_outdoor_temp = None
self.control_queue_task = asyncio.Queue()
if self.window_id is not None:
self.window_queue_task = asyncio.Queue()
Expand Down Expand Up @@ -257,6 +262,8 @@ async def async_added_to_hass(self):
self.calibration_type = 1

# Add listener
if self.outdoor_sensor is not None:
async_track_time_change(self.hass, self._trigger_time, 5, 0, 0)
async_track_state_change_event(
self.hass, [self.sensor_entity_id], self._trigger_temperature_change
)
Expand Down Expand Up @@ -287,8 +294,12 @@ def _async_startup(*_):
else:
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup)

async def _trigger_time(self, event):
await trigger_time(self, event)
async def _trigger_time(self, event=None):
_LOGGER.debug("better_thermostat %s: get last avg outdoor temps...", self.name)
await check_ambient_air_temperature(self)
if event is not None:
self.async_write_ha_state()
await self.control_queue_task.put(self)

async def _trigger_temperature_change(self, event):
await trigger_temperature_change(self, event)
Expand Down Expand Up @@ -350,7 +361,6 @@ async def startup(self):
or trv_state.attributes.get("current_heating_setpoint")
or 5
)
self._bt_hvac_mode = trv_state.state
self._trv_hvac_mode = trv_state.state
self._last_reported_valve_position = (
trv_state.attributes.get("valve_position", None) or None
Expand Down Expand Up @@ -469,6 +479,7 @@ async def startup(self):
)
self._bt_hvac_mode = HVAC_MODE_OFF
self._last_window_state = self.window_open
await self._trigger_time()

self.async_write_ha_state()
_LOGGER.info("better_thermostat %s: startup completed.", self.name)
Expand Down
28 changes: 17 additions & 11 deletions custom_components/better_thermostat/weather.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import logging
from datetime import datetime, timedelta
import homeassistant.util.dt as dt_util
from homeassistant.components.recorder import get_instance, history

# from datetime import datetime, timedelta

Expand Down Expand Up @@ -33,7 +36,7 @@ def check_weather(self) -> bool:
_LOGGER.debug(
f"better_thermostat {self.name}: checking ambient air sensor data..."
)
self.call_for_heat = check_ambient_air_temperature(self)
self.call_for_heat = self._last_avg_outdoor_temp < self.off_temperature
else:
_LOGGER.debug(
f"better_thermostat {self.name}: could not find any weather sensors... setting call_for_heat to true"
Expand Down Expand Up @@ -94,7 +97,7 @@ def check_weather_prediction(self) -> bool:
return None


def check_ambient_air_temperature(self) -> bool:
async def check_ambient_air_temperature(self):
"""Gets the history for two days and evaluates the necessary for heating.
Returns
Expand All @@ -112,14 +115,20 @@ def check_ambient_air_temperature(self) -> bool:
f"better_thermostat {self.name}: off_temperature not set or not a float."
)
return None
""" TODO

last_two_days_date_time = datetime.now() - timedelta(days=2)
start = dt_util.as_utc(last_two_days_date_time)
history_list = state_changes_during_period(
self.hass, start, dt_util.as_utc(datetime.now()), self.outdoor_sensor, no_attributes=False, descending=True, limit=100
history_list = await get_instance(self.hass).async_add_executor_job(
history.state_changes_during_period,
self.hass,
start,
dt_util.as_utc(datetime.now()),
str(self.outdoor_sensor),
)
historic_sensor_data = history_list.get(self.outdoor_sensor)
_LOGGER.debug(f"better_thermostat {self.name}: historic_sensor_data: {historic_sensor_data}")
_LOGGER.debug(
f"better_thermostat {self.name}: historic_sensor_data: {historic_sensor_data}"
)
# create a list from valid data in historic_sensor_data
valid_historic_sensor_data = []
invalid_sensor_data_count = 0
Expand Down Expand Up @@ -170,8 +179,5 @@ def check_ambient_air_temperature(self) -> bool:
_LOGGER.debug(
f"better_thermostat {self.name}: avg outdoor temp: {avg_temp}, threshold is {self.off_temperature}"
)
"""
avg_temp = convert_to_float(
str(self.hass.states.get(self.outdoor_sensor).state), self.name, "weather()"
)
return avg_temp < self.off_temperature

self._last_avg_outdoor_temp = avg_temp
9 changes: 9 additions & 0 deletions docs/setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
layout: default
title: Add new device
nav_order: 2
description: "BT."
permalink: /setup
---

# soon
1 change: 0 additions & 1 deletion docs/welcome.md

This file was deleted.

4 changes: 3 additions & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ description: "BT."
permalink: /
---

# soon
# Better Thermostat

This custom component for Home Assistant will add crucial features to your climate-controlling TRV (Thermostatic Radiator Valves) to save you the work of creating automations to make it smart. It combines a room-temperature sensor, window/door sensors, weather forecasts, or an ambient temperature probe to decide when it should call for heat and automatically calibrate your TRVs to fix the imprecise measurements taken in the radiator's vicinity.

0 comments on commit b235d9b

Please sign in to comment.