Hello,
I am new to AppDaemon and Python. I thought that building home automations in Python would be a great way to learn Python, as I learn by doing and real-world home automation cases are a good way to funnel my enthusiasm. So the user case is I have 8 soil sensors on tropical plants. Each plant has a minimum “dryness” that it must reach before watering, but it must be at that dry level for x number of days before watering. So I wanted to write a script that would take a list of 8 sensors, each of the sensors has a unique “dryness level”/trigger level and days that it has to be dry before being watered.
I wasn’t sure how to tackle this so I asked “chat.openai.com/chat” for help. I keep receiving an error on initialization “TypeError: SensorMonitor.init() takes 3 positional arguments but 8 were given”
Here is the python script it came up with and apps.yaml file:
apps.yaml:
sensor_monitor:
module: sensor_monitor
class: SensorMonitor
notification_service: telegram
sensor_configs:
sensor.gw2000b_v2_1_8_soil_moisture_1:
trigger: 25
days: 5
sensor.gw2000b_v2_1_8_soil_moisture_2:
trigger: 30
days: 7
sensor.gw2000b_v2_1_8_soil_moisture_3:
trigger: 15
days: 3
sensor.gw2000b_v2_1_8_soil_moisture_4:
trigger: 20
days: 4
sensor.gw2000b_v2_1_8_soil_moisture_5:
trigger: 18
days: 2
sensor.gw2000b_v2_1_8_soil_moisture_6:
trigger: 22
days: 6
sensor.gw2000b_v2_1_8_soil_moisture_7:
trigger: 28
days: 1
sensor.gw2000b_v2_1_8_soil_moisture_8:
trigger: 26
days: 8
sensor_monitor.py:
import appdaemon.plugins.hass.hassapi as hass
import datetime
class SensorMonitor(hass.Hass):
def __init__(self, notification_service, sensor_configs):
# Initialize the parent class
super().__init__()
# Store the name of the notification service to use
self.notification_service = notification_service
# Set up a dictionary to store timestamps for each sensor
self.timestamps = {}
# Store the sensor configurations in a dictionary
self.sensor_configs = sensor_configs
# Define a constant for the frequency of the periodic task
self.HOURS_1 = "1h"
def initialize(self):
# Set up the list of sensors to monitor
sensors = list(self.sensor_configs.keys())
# Set up listeners for state changes for each sensor
for sensor in sensors:
self.listen_state(self.state_callback, sensor)
# Set up a timer to run the periodic task
self.run_hourly(self.check_sensors, self.HOURS_1)
self.log("SensorMonitor initialized!")
def state_callback(self, entity, attribute, old, new, kwargs):
# Handle state changes
# Get the trigger value from the sensor configuration
trigger = self.sensor_configs[entity]["trigger"]
# Check if the new state is below the trigger value
if float(new) < trigger:
# If so, store the current timestamp for the sensor
self.timestamps[entity] = datetime.datetime.now().timestamp()
def check_sensors(self, kwargs):
# Iterate through each sensor and check its timestamp
for sensor, config in self.sensor_configs.items():
# Get the trigger and days values from the sensor configuration
trigger = config["trigger"]
days = config["days"]
# Check if the sensor has a timestamp
if sensor in self.timestamps:
# Calculate the number of seconds that have elapsed since the timestamp
elapsed_seconds = datetime.datetime.now().timestamp() - self.timestamps[sensor]
# Calculate the number of days that have elapsed
elapsed_days = elapsed_seconds / 86400
# Check if the elapsed time is greater than or equal to the required number of days
if elapsed_days >= days:
# If so, send a notification
self.call_service(self.notification_service, title="Sensor Alert", message=f"{sensor} has been below the trigger value for {days} days or longer")
# Clear the timestamp for the sensor
del self.timestamps[sensor]
Is anyone able to see what the error is?
Also if you think there is a better way to achieve the same result please let me know.
Thank you in advance for any help.
Here is a log snippet containing the error message:
2022-12-19 22:56:06.918380 INFO AppDaemon: Initializing app sensor_monitor using class SensorMonitor from module sensor_monitor
2022-12-19 22:56:06.922123 WARNING sensor_monitor: ------------------------------------------------------------
2022-12-19 22:56:06.922838 WARNING sensor_monitor: Unexpected error initializing app: sensor_monitor:
2022-12-19 22:56:06.923520 WARNING sensor_monitor: ------------------------------------------------------------
2022-12-19 22:56:06.926970 WARNING sensor_monitor: Traceback (most recent call last):
File "/usr/lib/python3.10/site-packages/appdaemon/app_management.py", line 1026, in check_app_updates
await self.init_object(app)
File "/usr/lib/python3.10/site-packages/appdaemon/app_management.py", line 336, in init_object
"object": app_class(
TypeError: SensorMonitor.__init__() takes 3 positional arguments but 8 were given
2022-12-19 22:56:06.927519 WARNING sensor_monitor: ------------------------------------------------------------
2022-12-19 22:56:06.928567 WARNING AppDaemon: Unable to find module sensor_monitor - initialize() skipped
2022-12-19 22:56:06.930136 INFO AppDaemon: App initialization complete