Hello Home Assistant Users!
I’am building a new thermostat module to my HAOS.
But I have a problem.
A created an average temperature value from my indoor temperature sensors, and used it to check my indoor temp, and turn on/off heater.
but, I need to create selectable temperature sensors, because if I use my main desktop computer, in my office, I have larger temperature than usually, and modify the indoor average temperature. I will create an input bool for my temperature sensors(for all rooms) and turn on and off the sensors for average calculation. How can I create it? I tried to generate pytohn script with chatgpt, but it is not working.
I attach my bools:
"input_booleans": {
"add_to_home_avg_temperature_kitchen": "input_boolean.add_to_home_avg_temperature_kitchen",
"add_to_home_avg_temperature_bedroom": "input_boolean.add_to_home_avg_temperature_bedroom",
"add_to_home_avg_temperature_bathroom": "input_boolean.add_to_home_avg_temperature_bathroom",
"add_to_home_avg_temperature_freetime_room": "input_boolean.add_to_home_avg_temperature_freetime_room",
"add_to_home_avg_temperature_living_room": "input_boolean.add_to_home_avg_temperature_living_room",
"add_to_home_avg_temperature_feri_office": "input_boolean.add_to_home_avg_temperature_feri_office",
"add_to_home_avg_temperature_dori_office": "input_boolean.add_to_home_avg_temperature_dori_office",
"add_to_home_avg_temperature_windscreen": "input_boolean.add_to_home_avg_temperature_windscreen"
}
Sensors:
"temperature_sensors": {
"kitchen": "sensor.ble_temperature_ef5e_kitchen_ble",
"bedroom": "sensor.ble_temperature_a4c1381bf0a2",
"bathroom": "sensor.ble_temperature_a4c13882aefa",
"freetime_room": "sensor.ble_temperature_a4c1381429f6",
"living_room": "sensor.ble_temperature_a4c1385873ae",
"feri_office": "sensor.ble_temperature_a4c1380e4769",
"dori_office": "sensor.ble_temperature_a4c138300708",
"windscreen": "sensor.ble_temperature_a4c13836d2ba"
},
full py script:
import voluptuous as vol
from homeassistant.components.python_script import Script, async_run_script
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.dispatcher import dispatcher_send
DOMAIN = "average_temperature"
config = {
"name": "Average Temperature",
"temperature_sensors": {
"kitchen": "sensor.ble_temperature_ef5e_kitchen_ble",
"bedroom": "sensor.ble_temperature_a4c1381bf0a2",
"bathroom": "sensor.ble_temperature_a4c13882aefa",
"freetime_room": "sensor.ble_temperature_a4c1381429f6",
"living_room": "sensor.ble_temperature_a4c1385873ae",
"feri_office": "sensor.ble_temperature_a4c1380e4769",
"dori_office": "sensor.ble_temperature_a4c138300708",
"windscreen": "sensor.ble_temperature_a4c13836d2ba"
},
"input_booleans": {
"add_to_home_avg_temperature_kitchen": "input_boolean.add_to_home_avg_temperature_kitchen",
"add_to_home_avg_temperature_bedroom": "input_boolean.add_to_home_avg_temperature_bedroom",
"add_to_home_avg_temperature_bathroom": "input_boolean.add_to_home_avg_temperature_bathroom",
"add_to_home_avg_temperature_freetime_room": "input_boolean.add_to_home_avg_temperature_freetime_room",
"add_to_home_avg_temperature_living_room": "input_boolean.add_to_home_avg_temperature_living_room",
"add_to_home_avg_temperature_feri_office": "input_boolean.add_to_home_avg_temperature_feri_office",
"add_to_home_avg_temperature_dori_office": "input_boolean.add_to_home_avg_temperature_dori_office",
"add_to_home_avg_temperature_windscreen": "input_boolean.add_to_home_avg_temperature_windscreen"
}
}
class AverageTemperatureSensor(Entity):
def __init__(self):
self._state = None
self._average_temperature = None
@property
def name(self):
return config["name"]
@property
def state(self):
if self._average_temperature is not None:
return self._average_temperature
else:
return None
@property
def unit_of_measurement(self):
return '°C'
async def update(self):
average_temperature_sum = 0
sensor_count = 0
for sensor_id, entity_id in config["temperature_sensors"].items():
if await async_run_script(DOMAIN, "get_temperature", entity_id):
temperature = float(await async_run_script(DOMAIN, "get_temperature", entity_id))
average_temperature_sum += temperature
sensor_count += 1
if sensor_count > 0:
average_temperature = average_temperature_sum / sensor_count
input_number.weighted_average_temperature.set(average_temperature)