How-To Guides

Smarter Aquarium with Home Assistant: Precise Control Over Your Light

Good light control ensures healthy plants growth and prevent algae bloom

Now that you have an automated water change system for your aquarium, the next step in combating algae is automating the lighting. Light is one of the most crucial components for your aquarium. Apart from showcasing the aquarium, it is also used to facilitate the growth of aquatic plants. However, excessive light can lead to algae bloom, which is something you want to avoid. Therefore, it is essential to turn on the light only when necessary and for an appropriate duration.

While you can opt for a conventional (non-smart) timer switch, it has its limitations. Imagine setting the timer to turn on at 6 PM and turn off at 11 PM every day. Suddenly, a guest visits you, or you need to clean the tank in the morning and require the light to be turned on. This would increase the total duration of light for that day.

A smarter approach is to use a smart switch integrated into Home Assistant. This way, Home Assistant can track the total duration the light remains on and automatically turn it off when it reaches the desired duration, ensuring that the light is never on longer than necessary. Additionally, you can also use voice commands to control the light via Alexa.

The settings for the light in Home Assistant

Here is the sample code for the package in Home Assistant written in YAML:

##########################################################
# Light
##########################################################
light:
  - platform: template
    lights:
      tank_1_light:
        friendly_name: "Tank 1 Light"
        value_template: "{{ states('switch.tank_1_light') }}"
        turn_on:
          service: script.tank_1_light_on
        turn_off:
          service: script.tank_1_light_off

##########################################################
# Script
##########################################################
script:
  tank_1_light_on:
    alias: "Tank 1 Light On"
    sequence:
      - condition: template
        value_template: "{{ states('sensor.tank_1_light_duration_today') < states('sensor.tank_1_light_daily_duration') }}"
      - service: switch.turn_on
        entity_id: switch.tank_1_light
        
  tank_1_light_off:
    alias: "Tank 1 Light Off"
    sequence:
      - condition: or
        conditions:
          - condition: time
            after: input_datetime.tank_1_light_off_time
            before: input_datetime.tank_1_light_on_time
          - condition: template
            value_template: "{{ states('sensor.tank_1_light_duration_today') >= states('sensor.tank_1_light_daily_duration') }}"
      - service: switch.turn_off
        entity_id: switch.tank_1_light

##########################################################
# Sensor
##########################################################
sensor:
  - platform: history_stats
    name: Tank 1 Light Duration Today
    entity_id: light.tank_1_light
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

##########################################################
# Template
##########################################################
template:
  - sensor:
      - name: "Tank 1 Light Daily Duration"
        unit_of_measurement: "h"
        state: "{{ (as_timestamp(today_at(states('input_datetime.tank_1_light_off_time'))) - as_timestamp(today_at(states('input_datetime.tank_1_light_on_time'))))/3600 }}"
        
      - name: "Tank 1 Light Duration Today Hour"
        unit_of_measurement: "h"
        state: "{{ states('sensor.tank_1_light_duration_today')|int }}"

##########################################################
# Input Datetime
##########################################################
input_datetime:
  tank_1_light_on_time:
    name: Tank 1 Light On Time
    has_date: false
    has_time: true
    
  tank_1_light_off_time:
    name: Tank 1 Light Off Time
    has_date: false
    has_time: true

##########################################################
# Automation
##########################################################
automation:
  - alias: 'Tank 1 Light Off Hourly'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.tank_1_light_duration_today_hour
      - platform: numeric_state
        entity_id: sensor.tank_1_light_duration_today
        above: sensor.tank_1_light_daily_duration
    condition:
      - condition: numeric_state
        entity_id: sensor.tank_1_light_duration_today
        above: sensor.tank_1_light_daily_duration
      - condition: state
        entity_id: light.tank_1_light
        state: 'on'
    action:
      - service: script.activate_alexa_actionable_notification
        data_template:
          text: "The Tank 1 light has been on for {{ states('sensor.tank_1_light_duration_today') }} hours. Do you want to keep them on?"
          event_id: 'actionable_notification_tank_1_lights_keep_on'
          alexa_device: "media_player.this_device"

  # Tank 1 Automation
  - alias: 'Tank 1 Light Off'
    initial_state: 'on'
    trigger:
      - platform: time
        at: input_datetime.tank_1_light_off_time
      - platform: event
        event_type: alexa_actionable_notification
        event_data:
          event_id: actionable_notification_tank_1_lights_keep_on
          event_response: ResponseNo
      - platform: event
        event_type: alexa_actionable_notification
        event_data:
          event_id: actionable_notification_tank_1_lights_keep_on
          event_response: ResponseNone
    action:
      - service: light.turn_off
        entity_id: light.tank_1_light
        
  - alias: 'Tank 1 Light On'
    initial_state: 'on'
    trigger:
      - platform: time
        at: input_datetime.tank_1_light_on_time
    condition:
      - condition: numeric_state
        entity_id: sensor.tank_1_light_duration_today
        below: sensor.tank_1_light_daily_duration
    action:
      - service: light.turn_on
        entity_id: light.tank_1_light

Bonus: Show Mode!

Since I have integrated the light into Home Assistant and have an Echo smart speaker right next to the aquarium, why not add some music to accompany me during my tranquil moments with the aquarium? Allow me to introduce Show Mode, the ultimate way to showcase the aquarium. By simply adding a wireless button switch, one press will bring the light to life, accompanied by a relaxing melody from the Echo speaker. It's an invitation to unwind, relax, and let the serene aquarium take centre stage. This feature is perfect for impressing guests or immersing myself in the therapeutic world of my aquatic companions.

Here is a video showing how it works:

Here is the sample code for the Show Mode package in Home Assistant written in YAML:

##########################################################
# Script
##########################################################
script:
  tank_1_show:
    alias: 'Tank 1 Show'
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.living_room
          volume_level: 0.5
      - service: media_player.select_source
        entity_id: media_player.spotify_syntax
        data:
          source: "Living Room Sound System"
      - service: media_player.play_media
        data:
          entity_id: media_player.spotify_syntax
          media_content_id: "spotify:track:4Yfx8KnmfYaNr58SVLV4QY"
          media_content_type: 'music'
      - service: homeassistant.turn_on
        entity_id:
          - switch.tank_1_light
          - input_boolean.tank_1_show
      - service: homeassistant.turn_off
        entity_id:
          - switch.tank_2_light
          - switch.tank_3_light
          - input_boolean.tank_2_show
          - input_boolean.tank_3_show
          
  tank_1_light_after_show:
    alias: "Tank 1 Light After Show"
    sequence:
      - condition: time
        after: input_datetime.tank_1_light_on_time
        before: input_datetime.tank_1_light_off_time
      - service: light.turn_on
        entity_id: light.tank_1_light

##########################################################
# Input Boolean
##########################################################
input_boolean:
  tank_1_show:
    name: Tank 1 Show

##########################################################
# Automation
##########################################################
automation:
  # Button short press, start tank 1 show
  - alias: 'Start Tank 1 Show'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.tank_1_button_action
        to: 'single'
    action:
      - service: script.tank_1_show
      
  - alias: 'Stop Tank 1 Show'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: media_player.spotify_syntax
        to: 'paused'
      - platform: state
        entity_id: sensor.tank_1_button_action
        to: 'hold'
    condition:
      - condition: state
        entity_id: input_boolean.tank_1_show
        state: 'on'
    action:
      - service: homeassistant.turn_off
        entity_id:
          - light.tank_1_light
          - input_boolean.tank_1_show
      - service: media_player.media_stop
        data:
          entity_id: 'media_player.living_room'
      - delay: 5
      - service: script.tank_1_light_after_show
      - service: script.tank_2_light_after_show
      - service: script.tank_3_light_after_show

Conclusion

In conclusion, automating the lighting system in your aquarium through Home Assistant by integrating a smart switch offers a multitude of benefits. It allows for precise control over light duration, preventing excessive exposure which leads to algae bloom, while the convenience of voice commands via Alexa adds flexibility. The introduction of Show Mode, accompanied by soothing music from an Echo smart speaker, creates a tranquil ambiance, perfect for unwinding and immersing oneself in the therapeutic world of aquatic companions. With these automated features, your aquarium becomes a serene haven, impressing guests and providing a seamless blend of technology and nature.

Comments

Related posts