Controling Dehumidifier

Hi, everyone,

I would like to regulate the humidity via a PID controller.

For this I got myself a humidifier and a dehumidifier.

My problem now is that the dehumidifier has a push button to which I always have to send an impulse that it goes on or off.

My consideration now was that I should use a relay or transistor which I can then control with Mycodo.

But now I can’t find the function how to do this in Mycodo.

So if my humidity is too high, Mycodo should send a 2s pulse to switch the dehumidifier on. Once the desired value has been reached, Mycodo should send a 2s pulse at the same output switch the dehumidifier of.

I hope you can understand what I mean: D

The Button locks like this

1 Like

The immediate issue I see with what you described is this: how will you know if the humidifier is already on or off when you’re assessing whether to turn it on or off?

I can not say that.
The dehumidifier does not have its own logic to switch itself on or off, it is connected to 230v and then switched on and off using the button. What I could do to get a safe state is to switch the 230V via a relay and then send an impulse to switch on the dehumidifier via a second output with 5sec delay, to switch off the dehumidifier I only need to turne off the 230V relay.

In that case, then you could use a Conditional Controller Function that runs periodically to check the humidity and then manipulate the outputs accordingly. Here is some pseudocode for the logic of the controller (not actual code):

import time
humidity_high = 80  # percent
humidity_low = 60  # percent

humidity_value_present = get_humidity()

if humidity_value_present > humidity_high:
    output_on("dehumidifier_main")
    time.sleep(1)
    output_on("dehumidifier_button", duration=2)
elif humidity_value_present < humidity_low:
    output_off("dehumidifier_main")

Thanks for your help, I’m not the best at programming now, but I’ll try to get it done with your example code.

Check out the manual for info on Conditional Controllers: Functions - Mycodo

Make an attempt at writing the Conditional Controller code and if you need any assistance, share your code and I can give advice.

Is that something that could work? Because I get the error message
warning: pylint returned with status : 127
when i save my code in mycodo.

#Desired range for humidity
humidity_high = 80 # percent
humidity_low = 60 # percent

#Time
import time

#Conditions and Actions
humidity_value_present = self.condition("{e65b252e}") # Condition: measurement, last, humidity
dehumidifier_main_on = self.run_action("{cba3e286}") # Action: dehumidifier_main on
dehumidifier_main_off = self.run_action("{ec981596}") # Action: dehumidifier_main off
dehumidifier_button = self.run_action("{a91100f0}") # Action: dehumidifier_button

#Logik
if humidity_value_present > humidity_high:
self.run_action("{dehumidifier_main_on}")
time.sleep(5)
self.run_action("{dehumidifier_button}", duration=2)
elif humidity_value_present < humidity_low:
self.run_action("{dehumidifier_main_off}")

That’s not an error, it’s a warning, and it’s merely telling you the pylint score was less than 10/10. The pylint output appears below the code review that’s returned when you save.

Duration is set in the Action settings, not in the function. You would still just execute self.run_action() with only the action ID as the sole parameter.

run_action() does not return anything. To do this would execute them all at once, which I don’t believe is desirable. You only execute run_action() when you want that particular action to run.

run_action() only accepts the action ID. You have the proper syntax in the first part you use run_action(), but you only want to call it when you want that action to actually run.

I also don’t see the proper indentations on the if/elif section. You can enclose the code in triple-bactics to indicate it’s code. You can put the language after the top backtics (e.g. “```python”) to have it use Python syntax hilighting.

if I understand you correctly, it should look like this?


import time
humidity_value_present = self.condition("{d7ea0965}")
if humidity_value_present > humidity_high:
	run_action("{b946ce6c}")        # Action: dehumidifier_main on				
	time.sleep(5)							
	run_action("{6578dbde}")        # Action: dehumidifier_button			    
elif humidity_value_present < humidity_low:
	run_action("{e7200dfe}") 	    # Action: dehumidifier_main off}

I also built in the indentations

Correct, but you also need humidity_high and humidity_low defined if you’re going to reference them.

yes the whole code now looks like this:



 19:         humidity_high = 80 # percent
 20:         humidity_low = 60 # percent
 21:         import time
 22:         humidity_value_present = self.condition("d7ea0965-eb3e-4bae-aedc-5e3014567138")
 23:         if humidity_value_present > humidity_high:
 24:         	run_action("b946ce6c-bfed-4d91-809f-0a8df76e4958")				
 25:         	time.sleep(5)							
 26:         	run_action("6578dbde-4aa9-45ad-a3b5-8b33897327d0")			    
 27:         elif humidity_value_present < humidity_low:
 28:         	run_action("e7200dfe-8ef3-411b-bc56-22793fe636a6")

but unfortunately it doesn’t work yet in the logs I get the following error

2022-01-10 02:09:20,473 - ERROR - mycodo.controllers.controller_conditional_2f24eb4b - Exception executing Conditional Statement code
Traceback (most recent call last):
File “/var/mycodo-root/mycodo/controllers/controller_conditional.py”, line 184, in check_conditionals
self.conditional_run.conditional_code_run()
File “/home/pi/Mycodo/mycodo/user_python_code/conditional_2f24eb4b-4866-4103-a9bd-9706e86225b9.py”, line 28, in conditional_code_run
run_action(“e7200dfe-8ef3-411b-bc56-22793fe636a6”)
NameError: name ‘run_action’ is not defined

It needs to be self.run_action(). Look below the Conditions and Actions and you will see the exact function call that needs to be made to query the Condition or execute the Action from the Conditional Statement. Also, don’t paste the entire pylint output, only the code in the Conditional Statement text box is needed. Pasting anything else will only serve to confuse other users, as they may copy the entire code into their Conditional Statement trying to mimic what they see here.

Hey, thanks for the help! I’ve got it all working now. Now I just have the next problem. At the moment a measuring cycle is 60sec for, if the humidity is too high and mycodo should turn the air dehumidifier on, it switches it on and off every 60sec.
I could now increase the 60sec but I hope that it can be done differently.

For this, you will need to store the state of the humidifier. Here is how I would do it:

import time

humidity_high = 80 # percent
humidity_low = 60 # percent
humidity_value_present = self.condition("{d7ea0965}")

if not hasattr(self, "dehumidifier_state"):  # Initialize variable
    self.run_action("{e7200dfe}")  # Action: dehumidifier_main off
    self.dehumidifier_state = "off"

if humidity_value_present > humidity_high and self.dehumidifier_state == "off":
    self.run_action("{b946ce6c}")  # Action: dehumidifier_main on
    time.sleep(5)
    self.run_action("{6578dbde}")  # Action: dehumidifier_button
    self.dehumidifier_state = "on"
elif humidity_value_present < humidity_low and self.dehumidifier_state == "on":
    self.run_action("{e7200dfe}")  # Action: dehumidifier_main off
    self.dehumidifier_state = "off"

Thank you very much i will try your code now

It works like a charm thx for the help!

1 Like

But now I still have the question of whether I could do the whole thing with a custom output as I would like to control my VPD via a PID controller and need the dehumidifire as an output.

You’re probably not going to have much success making an output work properly since your On command is not instantaneous, and thus will experience issues the moment an Off command occurs while your On command is in the sleep period. This is why you need to have specific logic controlling both On and Off at the level of the controller handling its operation (i.e. the controller operates at a pre-defined interval and will only either turn it On or Off according to your specifications).