Smart home – now with the gecko

Recently, under strange circumstances, I had this beast:

eublefar
eublefar

In general, he is quite unpretentious, but I wanted to provide him with the most pleasant conditions for his cold-blooded existence. Not that these were some non-trivial tasks, it just seemed interesting to me that technologies also have such sudden applications.

What needed to be done:

  1. Keep him at a minimum temperature at night, but so that he does not overheat

  2. Maintain minimum humidity

  3. Turn on an ultraviolet lamp for him at 5 in the morning, when he likes to crawl out to lie on a pebble.

It seems to be easy. I had a bag of relays, sensors, Home Assistant and an Arduino. But once you start building a smart home, it becomes hard to stop. The only thing I was worried about was the Arduino. There is nothing more helpless, irresponsible and corrupt than the Arduino. I knew that sooner or later I would switch to this rubbish.

It would seem that everything is simple – stick the sensors there, connect it to the ESP, and click the relays. But I wanted accurate sensors, small and autonomous, and stable enough to not cook or freeze the animal. And there was a lot of room for different questions and work.

Disclaimer

Before reading any further, let me clarify that I am neither a herpetologist nor an electrician. I am a simple backend developer. And I would never entrust the life of an animal to automation if I could not constantly control it with my eyes. Please be careful not to mindlessly copy what I have written here. I warned you, and I won’t feel guilty if you boil your aquarium fish, for example.

Sensors

So, back to the topic. To measure temperature and humidity, I chose Aqara Temperature and Humidity Sensor on Zigbee – they are small, beautiful, and work autonomously from tablet batteries. Yes, it is “they” – since the temperature must be measured both in a cold and in a hot corner. In addition, I also had a similar Mija sensor, only with LCD and working via BLE, not Zigbee. It hung from above, and I left it there as a backup – there are never many sensors!

In order to throw all this into the Home Assistant, I used Xiaomi Gateway 3, specially purchased for this purpose. We bind sensors to it, put the required custom component in Home Assistant, and we get all the necessary data! Just a fairy tale.

Heating

For heating, I liked the ceramic light bulb. It does not produce excess light and, in theory, lives forever. True, I got excited and took it to 100W. Even through the dimmer it gives out too much heat, so now I’m waiting for the same 50 to come to me.

Moisturizing

To do this, I installed a conventional ultrasonic humidifier, pointing it with a spout into the terrarium.

Ultraviolet

To do this, put special lamps for reptiles. I don’t know how much this is marketing, but several herpetologists recommended it, and I decided to take their word for it.

Relay

The devices are prepared, the sensors too. Now we need to turn it all on. Offhand, I couldn’t find three-channel smart relays for sane money, so I just took standard five-volt relays for arduinos from Ali. It’s a little inconvenient that I don’t know how to drain current from them, and I’m also thinking about replacing them with solid state ones … But so far this has not caused any problems, and I try to put in effort as needed.

Controller

At home I have a bag of NodeMCU, Wemos D1 R1 and Wemos 32, so I took the good old D1 R1 on the ESP8266 and decided to finally feel the ESPHome. Before that, I flashed controllers via the Arduino IDE via USB and used the topics configured by MQTT with my hands, but then I decided that it was enough to endure it – there is a suitable reason to figure it out.

In general, ESPHome turned out to be just a magical thing. You write simple YAML, everything is flashed through the air, all topics and other stuff are created for you, and the device itself is detected in Home Assistant. Simply magic! In fact, the entire relay setup looked like this:

switch:
  - platform: gpio
    pin: D2
    name: Relay1
    id: relay1
    inverted: true
  - platform: gpio
    pin: D3
    name: Relay2
    id: relay2
    inverted: true
  - platform: gpio
    pin: D4
    name: Relay3
    id: relay3
    inverted: true

triggers

Great, now there is all the hardware and software, you can turn everything on and off through the Home Assistant, however … Nothing is automated yet.

And Automations from Home Assistant help with this. In general, they came out pretty trivial:

  • If the temperature is below x, then turn on the heating

  • if the temperature is above x, then turn off the heating

  • same for humidity

  • at 5 in the morning turn on the lamp, at 17 turn off

  • if the temperature is critically low or high, then send a telegram notification

Hooray, it seems that our MVP is ready for this. Everything works and draws beautiful useful graphics:

But there is a nuance!

However… So far this doesn’t look safe enough. What if the relay sticks while I’m sleeping and I don’t hear the telegram notification? I don’t want a fried gecko for breakfast at all!

Then I realized that I needed an additional way of alerting. It would be possible to reuse smart speakers with Alice for this, but only children have them (the toad presses to put them in every room), and I would not want to scare them at night, if anything.

Well then. If you have an arduin in your hands, then everything around seems like a nail. We take it, stick a squeaker into it, and set it up as a switchable siren with a polyphonic melody. We add the inclusion of this siren as a switch in the Home Assistant. Along the way, I also threw in a service for Home Assistant, which allows you to play any melody on it. Well, because I could. This is probably the only thing that was not completely trivial, so I’ll even throw in the code:

esphome:
  name: alam
  on_loop:
    then:
      if:
        condition:
          - switch.is_on: alarm_sensor
          - not:
             rtttl.is_playing
        then:
          - rtttl.play: 'MissionImp:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d'

switch:
  - platform: template
    name: "Alarm Switch"
    optimistic: true
    id: alarm_sensor
    internal: false
    
output:
  - platform: ledc
    pin: GPIO18
    inverted: true
    id: rtttl_out

rtttl:
  output: rtttl_out
  on_finished_playback:
    - logger.log: 'Song ended!'

api:
  services:
    - service: play_rtttl
      variables:
        song_str: string
      then:
        - rtttl.play:
            rtttl: !lambda 'return song_str;'
            

So, in 40 lines of programming on yamls, a completely tolerable siren is obtained, which is heard throughout the house.

Other than that, I thought of a case where a relay turned on and at that my Home Assistant server died and didn’t turn it off. I still don’t want a fried gecko, so I added a simple test – the heater relay turns off 20 minutes after being turned on. And if it’s really cold around and it still needs to be turned on – it doesn’t matter, Home Assistant will turn it back on!

I couldn’t write the code for such a check in yamls, and I used the so-called lambda in ESPHome. Who knows why it’s called that – it’s executed on the controller…

  on_loop:
    then:
      lambda: |-
          static unsigned long last_update = 0;
          static unsigned long enabledForTime = 0;
          int iteration = 5000; // every 5 sec
          if((last_update + iteration) < millis()){
             last_update = millis();
             if(id(relay2).state){
               enabledForTime+=iteration;
             }
             else{
               enabledForTime = 0;
             }
             ESP_LOGD("ERM", "Heat relay enabled for: %lu", enabledForTime);
             if(enabledForTime> 1000*60*20){ // 20 min, safety measure
               ESP_LOGD("ERM", "Turning off heat relay");
               id(relay2).turn_off();
               enabledForTime = 0; // avoid confusion when HA turns on relay back
             }
          }
        

Now, that seems to be it. There is an idea to add another Yandex socket in front of the relay (which I also manage locally, since it is thuya) in order to restart the controller through it, if anything … But I don’t want to add points of failure unnecessarily. And in extreme cases, you can always call a neighbor to check the beast. In the end, I don’t need five nines in stability, I’m not doing Yandex.gecko.

Questions

In the process, I asked myself questions about what scenarios I could come up with and how to avoid them. Perhaps this will also be interesting, as it helps to understand how the model was built in my head.

Q: What if the internet goes down?

A: Nothing. Everything will continue to work as it did. Only from the outside, it will be impossible to find out the readings of the sensors and receive notifications in telegrams. If the Internet fell often and it would be super critical, it would be possible to set up a backup channel. But… And so it will do.

Q: What if there is a power outage?

A: It will not be worse than without a smart home. As soon as electricity appears, everything will be restored. In general, the house is warm, but if you wanted guarantees of heating, you could put an uninterruptible power supply on the router, the HA server and relays with consumers.

Q: And if the battery in the sensor dies?

A: Triggers rely on readings from three sensors. Three at the same time – do not die.

Q: And yet. What if the relay sticks or the microcontroller hangs?

A: Relay sticking can be treated by remote reset of the microcontroller. Microcontroller freezing – an option with an external smart socket. But in general, this seems to be a negligibly rare case that a notification, a siren, and, at worst, a visit from a friend can handle.

Results

  1. I definitely liked how it turned out. A smart home for a gecko has made life more pleasant and comfortable for me and for him.

  2. All this machinery is definitely not worth the candle if you don’t have anything from a smart home at home. It’s probably easier to put a ready-made solution for a terrarium. But if you already have an ecosystem, then you get a good solution for the price of a pair of sensors, one or two controllers and a tweeter.

  3. Of course, there is nothing technically difficult in the work done, and I foresee the questions “why is this here?”. However, it seems to me that it is the synergistic effect of simple things that, together, makes it possible to do pretty cool things, is interesting. Previously, I have already described the opening of the gate, watering the flowers, managing the Christmas tree and children’s games on the smart home. It seems that over time more and more such solutions will appear, and not for bearded nerds, but for normal people, and they will work cool and beautiful out of the box.

  4. Could it have been done better and easier? Perhaps I didn’t learn something? Yes for sure. I hope you can help me in the comments.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *