DHT22 humidity > 50 switch on ?! I am giving up

Moderator: leecollings

Post Reply
badabustyhh
Posts: 7
Joined: Monday 06 July 2020 14:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

DHT22 humidity > 50 switch on ?! I am giving up

Post by badabustyhh »

Hey Guys,

Im not sure where the problem is, i already played around for hours but im kinda stuck.
I did a little project on raspberry a few years ago, but seems like i forgot almost everything.


I am tryin to set a switch on or off at a certain humidity, i tried it with blockly and it once triggert the script and turned it on but only this one time :?:
I can turn it on or off at the dashboard but over the script no chance

i put in a screenshot of the dashboard with name of the sensor and switch, the log of the switch, the blockly thing and a lua script. It was ment to work with a fancontroller and i shortened it down. I get a trigger event in the log but switch activity.

would be nice if somebody can take a look and give a tip. Thanks in advance you guys !!!!!!!!

Code: Select all

--[[
 
This script controls the humidity in a typical bathroom setting by detecting
relative rises in humidity in a short period.
Of course it requires a humidity sensor and a binary switch controlling a fan/ventilator.
(there is no provision for variable speed ventilators here!)
 
How it works (assuming the default constants as defined below):
Every 5 minutes a reading is done. Every reading is stored together
with the previous reading and is stored in two user variables (humidityTmin5 and humidityTmin10).
So it has two reading over the past 10 minutes.
It then takes the lowest of the two and compares it with the latest reading and
calculates a delta.
If the delta is 3 or higher (see constants) then the fan will be turned
on, it calculates the target humidity and the 'humidity-decrease program' is started (fanFollowsProgram=1).
From then on, every 5 minutes the current humidity is compared to the
stored target humidity. Basically if that target is reached, the fan is turned off
and the 'program' is ended.
Of course, it is possible that the target is never reached (might start raining outside
or whatever). Then there is a failsafe (FAN_MAX_TIME) after which the ventilator
will be turned off.
 
Also, it will detect if the ventilator is manually switched off during a program
or when it is switched on before the program starts.
 
Along the lines it prints to the log and sends notifications
but of course you can turn that off by removing those lines.
 
--]]
 
commandArray = {}
 
-- declare some constants
-- adjust to your specific situation
SAMPLE_INTERVAL = 5                 -- time in minutes when a the script logic will happen
FAN_DELTA_TRIGGER = 3               -- rise in humidity that will trigger the fan
FAN_MAX_TIME = 3                   --  maximum amount of sample cycles the fan can be on, 
                                    -- in case we never reach the target humidity
TARGET_OFFSET = 2                   -- ventilator goes off if target+offset is reached 
                                    -- (maybe it takes too long to reach the true target due to wet towels etc)
FAN_NAME = 'Add new 433 Switch - New RF switch #2'    -- exact device name of the switch turning on/off the ventilator
SENSOR_NAME = 'Temp'                -- exact device name of the humidity sensor
 
TEST_MODE = false                   -- when true TEST_MODE_HUMVAR is used instead of the real sensor

PRINT_MODE = true					-- when true wil print output to log and send notifications

if PRINT_MODE == true then
print('Fan control')
end

-- get the global variables:
-- this script runs every minute, humCounter is used to create SAMPLE_INTERVAL periods
humCounter = tonumber(uservariables['humCounter'])
humidityTmin5 = tonumber(uservariables['humidityTmin5'])                -- youngest reading
humidityTmin10 = tonumber(uservariables['humidityTmin10'])              -- oldest reading
targetFanOffHumidity = tonumber(uservariables['targetFanOffHumidity'])  -- target humidity
fanMaxTimer = tonumber(uservariables['fanMaxTimer'])
fanFollowsProgram = tonumber(uservariables['fanFollowsProgram'])        -- marker indicating that the 
                                                                        -- decrease program is started
 
target = 50 -- will hold the target humidity when the program starts

-- get the current humidity value
if (TEST_MODE) then
    current = tonumber(uservariables[TEST_MODE_HUMVAR])
else
    current = otherdevices_humidity[SENSOR_NAME]
end
 
-- check if the sensor is on or has some weird reading
if (current == 0 or current == nil) then
    print('current is 0 or nil. Skipping this reading')
    return commandArray
end

if PRINT_MODE == true then
        print('Current humidity:' .. current)
		print('targetFanOffHumidity:' .. targetFanOffHumidity)
		print('humidityTmin5: ' .. humidityTmin5)
		print('humidityTmin10: ' .. humidityTmin10)
		print('fanFollowsProgram:' .. fanFollowsProgram)
end

-- increase cycle counter
humCounter = humCounter + 1
 
if (humCounter >= SAMPLE_INTERVAL) then
 
    if (humidityTmin5 == 0) then
        -- initialization, assume this is the first time
        humidityTmin5 = current
        humidityTmin10 = current
    end
 
    humCounter = 0 -- reset the cycle counter
 
    -- pick the lowest history value to calculate the delta
    -- this also makes sure that two relative small deltas in the past 2*interval minutes are treated as one larger rise
    -- and therefore will still trigger the ventilator
    -- I don't want to use a longer interval instead because I want the ventilator to start as soon as possible
    -- (so rather after 5 minutes instead of after 15 minutes because the mirrors in the bathroom become kinda useless ;-)
    delta = current - math.min(humidityTmin10, humidityTmin5)
if PRINT_MODE == true then
	print('Delta: ' .. delta)
end
 
    -- pick the lowest history value
    target = math.min(humidityTmin10, humidityTmin5) + TARGET_OFFSET
 
    -- shift the previous measurements
    humidityTmin10 = humidityTmin5
    -- and store the current
    humidityTmin5 = current
    
    
 -- see if we have to turn it on
    if 
        
         (humidityTmin5 >= 50) then
            -- time to start the fan
            commandArray[FAN_NAME] = 'On'
 
            fanFollowsProgram = 1
 
            -- set the safety stop
        end
 
    else
 
 

            -- first see if it can be turned off
            if (humidityTmin5 <= 45) then
                commandArray[FAN_NAME] = 'Off'
                
                msg = ''
 
                
        
                    msg = 'Target humidity reached'
                    if PRINT_MODE == true then
					print(msg)
					end
                
 
				if PRINT_MODE == true then
                print('Turning off the ventilator')
                msg = msg .. '\nTurning off the ventilator'
				end
				fanFollowsProgram = 0
            
                -- reset history in this case.. we start all over
                -- Tmin10 is still in the 'ventilator=On'-zone
                humidityTmin10 = humidityTmin5
                 if PRINT_MODE == true then
				commandArray['SendNotification'] = 'Ventilator is off#' .. msg .. '#0'
				end
 
            
            end
        end
    

if PRINT_MODE == true then
    print('New values >>>>>>>>>>>')
    print('humidityTmin5: ' .. humidityTmin5)
    print('humidityTmin10: ' .. humidityTmin10)

end

 
-- save the globals

commandArray['Variable:humidityTmin10'] = tostring(humidityTmin10)
commandArray['Variable:humidityTmin5'] = tostring(humidityTmin5)
commandArray['Variable:fanFollowsProgram'] = tostring(fanFollowsProgram)


 
return commandArray
Attachments
dashboarddd.png
dashboarddd.png (255.94 KiB) Viewed 927 times
script_trigger.png
script_trigger.png (143.54 KiB) Viewed 927 times
blockly.png
blockly.png (186.96 KiB) Viewed 927 times
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: DHT22 humidity > 50 switch on ?! I am giving up

Post by freijn »

The bathroom example script works perfectly. Why did you shorten it?

Perhaps list your requirements/conditions and then make the script?
badabustyhh
Posts: 7
Joined: Monday 06 July 2020 14:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT22 humidity > 50 switch on ?! I am giving up

Post by badabustyhh »

Thanks 4 helpin out.

I shortend/change it cause it has a "relative startpoint"it checks humidity now and if there is a special amount of raise it switches on. but its not possible to say at 60% start and under 50% stop. so i tried to break it down to the "at least needed functions".

i just got another idea/workaround i use a script to trigger the switch
can i put

script:///home/pi/domoticz/scripts/433send.sh

i somewhere after

fanFollowsProgram = 1

so it does the send command directly ? or bad idea )

br
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: DHT22 humidity > 50 switch on ?! I am giving up

Post by freijn »

Is this script helpfull ?

Run it every minute.

Code: Select all

--[[
 
Solar Fan On Off Switch
--]]
 
commandArray = {}
 

SENSOR_NAME = 'SolarEdge Temperature'     -- exact device name of the humidity sensor
FAN_NAME= 'SolarFanInverter'
MaxTemp = 45
MinTemp = 42
Debug = false					-- when true wil print output to log and send notifications


-- get the current Temp value
current = otherdevices_temperature[SENSOR_NAME]

if (current > MaxTemp ) then
    if otherdevices[FAN_NAME] == 'Off' then 
            commandArray[FAN_NAME] = 'On'
    end
end

if (current < MinTemp ) then
    if otherdevices[FAN_NAME] == 'On' then
            commandArray[FAN_NAME] = 'Off'
    end
end

if Debug == true then
        print('Fan control')
        print('Current Temp SolarInverter ' .. current)
        print('Fan Status = ' ..otherdevices[FAN_NAME] )
end

return commandArray
badabustyhh
Posts: 7
Joined: Monday 06 July 2020 14:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT22 humidity > 50 switch on ?! I am giving up

Post by badabustyhh »

it worked, it turned the switch on 1 time. i changed values and saved it again. turned the lamp off manually restarted, no action. cronjob was created. when i run the script in cmd

~/domoticz/scripts/lua $ lua solar.lua
lua: solar.lua:17: attempt to index global 'otherdevices_temperature' (a nil value)
stack traceback:
solar.lua:17: in main chunk
[C]: ?

i compared the scripcts in the orignal it was otherdevices_humidity, so i changed it but same i treid to find it out but dont unstersant the solutions :roll:

pi@raspberrypi:~/domoticz/scripts/lua $ lua solar.lua
lua: solar.lua:17: attempt to index global 'otherdevices_humidity' (a nil value)
stack traceback:
solar.lua:17: in main chunk
[C]: ?
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: DHT22 humidity > 50 switch on ?! I am giving up

Post by freijn »

Why do you run it in a cron job?

Just past the script in the scripts gui of domoticz in and select the trigger as a device.

In this way the script will run every min and check the values.
User avatar
jvdz
Posts: 2330
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: DHT22 humidity > 50 switch on ?! I am giving up

Post by jvdz »

freijn wrote: Tuesday 07 July 2020 17:31select the trigger as a device.
In this way the script will run every min and check the values.
Did you meant "trigger TIME" in stead of device? ;)

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
badabustyhh
Posts: 7
Joined: Monday 06 July 2020 14:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT22 humidity > 50 switch on ?! I am giving up

Post by badabustyhh »

f** yeah it works :D :D :D :D :D

thank you so much guys, Jos u gave the final Tip man, think this was the hook i was hangin on for a week.

muchas gracias

and to you freijn too for the script man.

BIG Up !!!

should load this up to github as a project to help others?

Peace out one love
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: DHT22 humidity > 50 switch on ?! I am giving up

Post by freijn »

jvdz wrote: Tuesday 07 July 2020 17:35
Did you meant "trigger TIME" in stead of device? ;)

Jos
Thanks Jos.

Multitasking has never been my greatest skill :-)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest