# Home automation for your enclosure



## Magnus Boden (Aug 17, 2016)

Humidifer System

I put in some time to set this up so I thought I would share it in case anyone else wants to do the same.
IT is compromised of these components. It might seem like a lot of work but I had most of this running already since I control other stuff in my home with it.

One Raspberry PI V2 + case





One RFXrec433E USB





Two Proove TSS320 (Thermometer and Hygrometer)





Two Magic Fog (One was not enough to get enough humidity)




Two NEYC-3 (Remote controllable power outlet, only two in the three pack)





Software

Domoticz


There is a image including the linux operating system available from their site. Linux knowledge is beneficial but you get away with mostly just configuring an ip address and small stuff like that.

There are apps for Android and IOS to interface with domoticz from your phone.

In the domoticz webinterface you can control your switches and watch your temperatures. Also there is a dashboard where you can make a blueprint of your house or enclosure in our case.







Setup

1 Power outlet to control the lights
1 Power outlet to control the foggers
1 Thermometer in the cool end
1 Thermometer in the hot end


The foggers both let the steam in at the cold side.

With domoticz it is easy to turn outlets on and off at scheduled intervals but I wanted my lights to follow the sunup and sunsets in argentine with one twist. Sunset is always at 23.00 but the average amount of hours of sun in each month is the same so sunup will be later in the summer months compared to the winter month. This is controlled by an array in my control script. Also the sunup and sunset doesn't just switch from one day to the other (on monthly breaks) it will gradually shift during the month as it gets get closer to next month.

Also the script will check with the Hygrometer from the cold side and turn the fogger on when it is below my humidity target. The humidity target also follows average humidity in argentine - 10 percent (Might increase to actual humidity later if it doesn't seem to hurt the enclosure).

Humidity targets are also entered in two arrays in the script so you specify daytime and night time humidity values for each month. I made the humidity follow a sine curve so it isn't exactly the same the whole day until night time sets.

Domoticz will automatically graph your temp and humidity in graphs like this visible in its web interface so you can check that you always have had a good climate for you Tegu.


Graphs


Only the daily graph is from my current setup the thermometers used to be in other places and my script wasn't in place earlier.

The thick blue part in the montly and yearly graphs are for the min and max at that time.

Cool side


 


 

 

Hot side


----------



## Magnus Boden (Aug 17, 2016)

Script


```
--
-- Domoticz passes information to scripts through a number of global tables
--
-- otherdevices, otherdevices_lastupdate and otherdevices_svalues are arrays for all devices:
--  otherdevices['yourotherdevicename'] = "On"
--  otherdevices_lastupdate['yourotherdevicename'] = "2015-12-27 14:26:40"
--  otherdevices_svalues['yourotherthermometer'] = string of svalues
--
-- uservariables and uservariables_lastupdate are arrays for all user variables:
--  uservariables['yourvariablename'] = 'Test Value'
--  uservariables_lastupdate['yourvariablename'] = '2015-12-27 11:19:22'
--
-- other useful details are contained in the timeofday table
--  timeofday['Nighttime'] = true or false
--  timeofday['SunriseInMinutes'] = number
--  timeofday['Daytime'] = true or false
--  timeofday['SunsetInMinutes'] = number
--  globalvariables['Security'] = 'Disarmed', 'Armed Home' or 'Armed Away'
--
-- To see examples of commands see: http://www.domoticz.com/wiki/LUA_commands#General
-- To get a list of available values see: http://www.domoticz.com/wiki/LUA_commands#Function_to_dump_all_variables_supplied_to_the_script
--
-- Based on your logic, fill the commandArray with device commands. Device name is case sensitive.
--

--  Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
sunup = {  '06:00', '07:30', '08:50', '10:00', '10:45', '11:30', '11:30', '10:45', '10:00', '08:50', '07:30', '06:00' }
sunset = { '23:00', '23:00', '23:00', '23:00', '23:00', '23:00', '23:00', '23:00', '23:00', '23:00', '23:00', '23:00' }

-- Montly humidity targets
--  Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
months =  {  40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  40 }
months_nightly_humidity = {  60,  62,  64,  66,  68,  70,  70,  68,  66,  64,  62,  60 }

min_run_time = 900
max_humidity = 80
min_humidity = 0
humidity_diff = 1
sensor = 'Ciara cool side'
switch = 'CiaraFogger'
lamp_switch = 'Ciara'

function isday()
  dnow = os.date("*t")
  days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }

  if (dnow.month == 12) then
  next_month = 1
  else
  next_month = dnow.month + 1
  end

  c_sunup_hour = tonumber(string.sub(sunup[dnow.month], 1, 2))
  c_sunup_min = tonumber(string.sub(sunup[dnow.month], 4, 5))
  c_sunset_hour = tonumber(string.sub(sunset[dnow.month], 1, 2))
  c_sunset_min = tonumber(string.sub(sunset[dnow.month], 4, 5))

  c_sunup = c_sunup_hour * 60 + c_sunup_min
  c_sunset = c_sunset_hour * 60 + c_sunset_min
   
  c_next_sunup_hour = tonumber(string.sub(sunup[next_month], 1, 2))
  c_next_sunup_min = tonumber(string.sub(sunup[next_month], 4, 5))
  c_next_sunset_hour = tonumber(string.sub(sunset[next_month], 1, 2))
  c_next_sunset_min = tonumber(string.sub(sunset[next_month], 4, 5))

  c_next_sunup = c_next_sunup_hour * 60 + c_next_sunup_min
  c_next_sunset = c_next_sunset_hour * 60 + c_next_sunset_min

  sunup_diff = c_next_sunup - c_sunup
  sunset_diff = c_next_sunset - c_sunset
   
  p = math.floor(dnow.day / days_in_month[dnow.month])
  c_todays_sunup = c_sunup + math.floor(sunup_diff * p)
  c_todays_sunset = c_sunset + math.floor(sunset_diff * p)

  for i = 1, days_in_month[dnow.month] do
  p = i / days_in_month[dnow.month]
  c_todays_sunup = c_sunup + math.floor(sunup_diff * p)
  c_todays_sunset = c_sunset + math.floor(sunset_diff * p)
  end

  -- (Sunup hour, check min) or now > sunup hour (min check not needed)
  if ((dnow.hour == c_sunup_hour and dnow.min >= c_sunup_min) or dnow.hour > c_sunup_hour) then
  if ((dnow.hour == c_sunset_hour and dnow.min <= c_sunset_min) or dnow.hour < c_sunset_hour) then
  return true
  end
  end
  return false
end

function isnight()
  return not isday()
end

function minutes_of_sun_today()
  dnow = os.date("*t")
   
  c_sunup_hour = tonumber(string.sub(sunup[dnow.month], 1, 2))
  c_sunup_min = tonumber(string.sub(sunup[dnow.month], 4, 5))
  c_sunset_hour = tonumber(string.sub(sunset[dnow.month], 1, 2))
  c_sunset_min = tonumber(string.sub(sunset[dnow.month], 4, 5))
   
  sunup_min = c_sunup_hour * 60 + c_sunup_min
  sunset_min = c_sunset_hour * 60 + c_sunset_min
   
  return (sunset_min - sunup_min)
end

function hours_of_sun_today()
  return minutes_of_sun_today() / 60
end

function humidity_hourly_target()
  dnow = os.date("*t")
   
  c_sunup_hour = tonumber(string.sub(sunup[dnow.month], 1, 2))
  c_sunup_min = tonumber(string.sub(sunup[dnow.month], 4, 5))
  c_sunset_hour = tonumber(string.sub(sunset[dnow.month], 1, 2))
  c_sunset_min = tonumber(string.sub(sunset[dnow.month], 4, 5))

  -- sunup minutes from 00:00
  sunup_min = c_sunup_hour * 60 + c_sunup_min
  -- sunset minutes from 00:00
  sunset_min = c_sunset_hour * 60 + c_sunset_min

  day = 60 * 24

  daylight_length = sunset_min - sunup_min
  night_length = sunup_min + (day - sunset_min)
   
  mid_daylight_min = sunup_min + ((sunset_min - sunup_min) / 2)
  mid_night_min = (sunset_min + (night_length / 2)) % day
  half_day = day / 2
   
  hum_range = months_nightly_humidity[dnow.month] - months[dnow.month]
   
  -- now_min == minutes from 00:00
  now_min = dnow.hour * 60 + dnow.min   

  min_values = {}

  for i = 0,half_day do
  degree = math.floor(i / (half_day / 180))
  p1 = math.floor((mid_daylight_min + i) % day)
  p2 = math.floor((mid_night_min + i) % day)
  min_values[p1] = (math.sin(math.rad(degree)) * hum_range) + months[dnow.month]
  min_values[p2] = (math.sin(math.rad(180 - degree)) * hum_range) + months[dnow.month]
  end

  return math.floor(min_values[now_min])
end

function last_update(device)
  s = otherdevices_lastupdate[device]
  if (s == nil) then
  return nil
  end
  year = tonumber(string.sub(s, 1, 4))
  month = tonumber(string.sub(s, 6, 7))
  day = tonumber(string.sub(s, 9, 10))
  hour = tonumber(string.sub(s, 12, 13))
  minutes = tonumber(string.sub(s, 15, 16))
  seconds = tonumber(string.sub(s, 18, 19))
  t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}

  return t2
end

commandArray = {}

if (isday() and otherdevices[lamp_switch] == 'Off') then
  print("Daytime, turning on lights")
  commandArray[lamp_switch] = 'On'
end

if (isnight() and otherdevices[lamp_switch] == 'On') then
  print("Nighttime, turning off lights")
  commandArray[lamp_switch] = 'Off'
end

dnow = os.date("*t")
tnow = os.time()

lu = last_update(sensor)
lu_switch = last_update(switch)

difference = (os.difftime (tnow, lu))
difference_on = (os.difftime(tnow, lu_switch))

if ((lu == nil or difference > 900) and difference_on > min_run_time) then
  print("ERROR: Turning CiaraFogger Off cause no update from sensor and has been running for " .. difference_on .. " seconds")
  print("ERROR: Last update from humidity sensor = " .. lu)
  commandArray[switch] = 'Off'
end

target_humidity = humidity_hourly_target()
target_humidity_min = target_humidity - humidity_diff
target_humidity_max = target_humidity + humidity_diff
if (target_humidity_max > max_humidity) then
  print("Target humidity max = " .. target_humidity_max)
  target_humidity = target_humidity_max
end

if (target_humidity_min < min_humidity) then
  print("Target humidity min = " .. target_humidity_min)
  target_humidity = target_humidity_min
end

print("Humidifier range = " .. target_humidity_min .. " - " .. target_humidity_max)

if (otherdevices_humidity[sensor] >= target_humidity_max and otherdevices[switch] == 'On') then
  print("Turning " .. switch .." Off cause humidity => " .. target_humidity_max .. " has been running for " .. difference_on .. " seconds")
  commandArray[switch] = 'Off'
end

if (otherdevices_humidity[sensor] <= target_humidity_min and otherdevices[switch] == 'Off') then
  print("Turning " .. switch .. " On cause humidity => " .. target_humidity_min)
  commandArray[switch] = 'On'
end

return commandArray
```


----------



## DangerousDann (Aug 17, 2016)

Magnus Boden said:


> Humidifer System
> 
> I put in some time to set this up so I thought I would share it in case anyone else wants to do the same.
> IT is compromised of these components. It might seem like a lot of work but I had most of this running already since I control other stuff in my home with it.
> ...


Very nice! about how much was the total cost and which sites did you use? I really like this idea and I have 3 large enclosures I would consider this for.


----------



## CameronJayBauer (Aug 17, 2016)

Yeah this is an awesome idea, my commendations to you.
But what exactly do you do with the first 2 parts on the list?


----------



## Magnus Boden (Aug 18, 2016)

DangerousDann said:


> Very nice! about how much was the total cost and which sites did you use? I really like this idea and I have 3 large enclosures I would consider this for.



A quick guesstimate is $120 rfxtrx, $100 for the raspberry pi, $30 for three nexa outlets, $25 for each thermometer + the foggers which cost about $100 each.

Swedish prices tends to be higher that in the states so perhaps 15-30% lower I would guess.

I bought my stuff from swedish websites but here is a link to a complete kit of the raspberry pi with case and power supply, there is a wifi adapter included so if you don't need that there is probably another kit without it.

https://www.amazon.com/CanaKit-Raspberry-Complete-Starter-9-Items/dp/B008XVAVAW

http://www.rfxcom.com/RFXtrx433E-USB-43392MHz-Transceiver/en

The outlets seems to be swedish byt on www.rfxcom.com site it lists a lot of supported brands. There are a lot of different brands that support the 433 mhz stuff. If you already have a wireless thermometer chances are pretty big that you already have one that works with this.

Tellstick is another variant of the rfxtrx though domoticz doesn't support the tellstick so you would have to use another software for that.

From the site:

Will receive for example these 433.92MHz sensors and remotes:


Cent-a-Meter,Electrisave,OWL power meters,
Alecto,Cresta,FineOffset,Hideki,Honeywell,LaCrosse,Meade,Oregon,Proove,Rubicson,TFA,Viking weather sensors,
Alecto,Avidsen,Chacon,NEXA,Flamingo,Blyss,Proove smoke detectors,
Atlantic,Chuango,Eminent,Meiantech,Visonic,X10 alarm sensors,
Oregon body weight scale,
Maverick,Rubicson BBQ sensors,
Chacon,HomeEasy,KlikAanKlikUit,NEXA,X10 remotes.
And can be used to control for example these 433.92MHz devices:


ANSLUT, Avantek, BBSB, Blyss, Brennenstuhl, Chacon, COCO, Conrad RSL, DI.O, ELRO, Energenie, Eurodomest, Everflourish, FA500, Flamingo, HomeConfort, HomeEasy, Impuls, Intertechno, Intertek, Kambrook, KlikAanKlikUit, LightwaveRF, Livolo, Mercury, NEXA, OTIO, Phenix, PROmax, Proove, RisingSun, Sartano, Siemens, X10, XDOM dimmers/switches,
1byOne, Byron SX, KlikAanKlikUit, Profiles, SelectPlus chime,
A-OK, ASA, BOFU, Brel, Chamberlain, Confexx, Ematronic, ESMO, Hasta, Louvolite, Quotidom, RAEX, Rohrmotor24, Rollertrol, Simu, Somfy, Yooda blinds/awning motors,
Harrison, Forest curtain motors,
Kingpin,Media Mount,Proluxx projection screens,
Avidsen,Chacon,NEXA,Flamingo,Blyss,Proove smoke detectors with siren,
Lucci Air fan,
Mertik Maxitrol fire place,
Aoke relays,
MDREMOTE LED strip controllers,
Smartwares radiator valve.


----------



## Magnus Boden (Aug 18, 2016)

CameronJayBauer said:


> Yeah this is an awesome idea, my commendations to you.
> But what exactly do you do with the first 2 parts on the list?



The raspberry pi is a normal computer just a small one with no screen. It runs the software that controls the devices. The rfxtrx box is a wireless (not wifi) receiver and transmitter that receives the wireless signal from the thermometers and sends the on/off command to the outlets.

Regards
Magnus


----------



## ag3nt 0rang3 (Oct 3, 2016)

*Brilliant idea. Much appreciated. This idea is awesome. Thank you for your time and knowledge. Linux and my tegu are my world albeit an uncertified one. Thanks again !!! *


----------



## ag3nt 0rang3 (Oct 3, 2016)

Would this work as well ? 
https://www.amazon.com/gp/aw/d/B01C6EQNNK?psc=1


----------



## Magnus Boden (Oct 5, 2016)

Hello,

Yes according to this site it should work.

https://www.domoticz.com/wiki/Raspberry_Pi

Don't forget this unit also or an equialent. If you use another check that domoticz supports it:
http://www.rfxcom.com/epages/78165469.sf/en_GB/?ViewObjectPath=/Shops/78165469/Products/14103

Regards
Magnus


----------

