When DHT-22 sensor fails, enable auto reset using Conditional Function

Hi All,

New to this forum, but have used information in past to setup up my grow room.

I was hoping could assist with a issue i have. I’m currently using the DHT-22 temp//humidity sensors to monitor my room, but quickly found these fail quite regularly when the relays are switching. I have full separation from AC and control cables, i have used shielded cables form control wiring. all these measures and still couldn’t get reliability from these sensors.

I have just ordered some SHT40 sensors to replace the DHT-22. While i wait for these come i need to ensure these sensors are still working, so what i have done is wired the DHT-22 5v power through its own relay, and planed to use a conditional function to turn this relay on for 1 second and off again when ever the sensor fails.
I thought this would be simple function with only few lines of code, but i can’t seem to get it to work. is anyone have any help me to get this to work?

below are the screen shots of my configuration and code.

1: import os
2: import sys
3: sys.path.append(os.path.abspath(’/var/mycodo-root’))
4: from mycodo.controllers.base_conditional import AbstractConditional
5: from mycodo.mycodo_client import DaemonControl
6: control = DaemonControl(pyro_timeout=60.0)
8: class ConditionalRun(AbstractConditional):
9: def init(self, logger, function_id, message):
10: super(ConditionalRun, self).init(logger, function_id, message, timeout=60.0)
12: self.logger = logger
13: self.function_id = function_id
14: self.variables = {}
15: self.message = message
16: self.running = True
18: def conditional_code_run(self):
19: measurement = self.condition(“49819628-7c71-4887-9444-daaad8cc4ca2”)
22: if measurement == 0 or measurement is None:
23: self.run_all_actions()

To more easily read your code, please format it as follows:

  1. Enclose it in three backticks, e.g. ```code```
  2. Paste the code from the Conditional Statement, not the pylint output.

As for the Actions sequence (executed sequentially with run_all_actions()), it looks like your relay will be instructed to turn on for 1 second, then immediately turn back off (mere milliseconds after turning on). When you specify a duration, it means it will turn the relay on for that duration, meaning for it to be a duration, it must turn that relay off after the specified time period. Your use of a second action to immediately turn the relay off defeats the purpose of setting a duration in the first action.

Oh that makes sense. I thought the 1 seconds was like a time delay before executing the next line of code.

Still struggling with this though, the way I’m testing this is i disconnect the power to the sensor to simulate a failure. The code loops but doesn’t trigger the output. I’m assuming this is because my measurement condition is wrong?
I’m i correct in saying that if the sensor failed it should be returning ‘None’?

self.logger.error("This ERROR log entry will appear in the Daemon Log")

if not hasattr(self, "loop_count"):  # Initialize objects saved across executions
    self.loop_count = 1
    self.loop_count += 1

measurement = self.condition("{49819628}")

if measurement is None:

Oh Actions updated too!

Yes, if there is not a measurement in the time period specified (Max Age), None is returned. You may not be waiting long enough. Additionally, if you want to see what’s going on in your code, you should apply logging lines effectively. The log line you have in the code really isn’t doing anything useful. It would make more sense to put one after setting measurement to log what the measurement is (which can be found in the Daemon Log).