Help with an Aquarium light cycle script

Hello to all,

As a beginner in HA, i have a currently many separate automations to control the light cycle on my aquarium. At this stage, i have a separate automation triggered based on time to turn the lights on or off in different times per day or to activate different scenes. I would like to replace all these automations with a single script that i wrote which i would like to validate with you. Before i paste the script, let me give you some information about the light set up of the aquarium.

The aquarium has 2 lights as follows:

  • Aquarium Main Light (this is the normal “bright” light) and it is controlled by a socket

  • Aquarium Night Light (this is a less bright RGB strip that is used to simulate different times of the day using scenes).

Explanation of the scenes:

  • Sunset (a yellowish colour scene at full brightness simulating the sunset)

  • Evening (an orange colour scene at 80% brightness simulating the golden hour)

  • Late Evening (a light blue colour scene at 50% brightness simulating just before dark conditions)

  • Midnight (a dark blue colour at 30% brightness simulating the night)

There are also some automations created using Assistant_Relay to fade in and out the night light. Here is the sequence of events i would like to happen:

  • At 7:00 the Night Light starts to fade in (using Assistant_Relay automation)
  • At 7:30 the Night Light activates the scene: Sunset (in this case it is used as sunrise)
  • At 8:00 the Night Light turns off
  • At 15:30 (in 7,5 hours from the previous event) the Night Light starts to fade in (using Assistan_Relay automation)
  • At 16:00 the Main Light turns on
  • At 16:01 the Night Light turns off
  • At 21:59 The Night Light activates the scene: Sunset
  • At 22:00 the Main Light turns off
  • At 22:30 the Night Light activates the scene: Evening
  • At 23:00 the Night Light activates the scene: Late Evening
  • At 23:30 the Night Light activates the scene: Midnight
  • At 23:31 the Night Light start to fade out (using Assistant_Relay automation)

Here is the script:

alias: Aquarium Light Sequence
sequence:
  - service: automation.trigger
    target:
      entity_id: automation.aquarium_wake_up
  - delay:
      hours: 0
      minutes: 30
      seconds: 0
      milliseconds: 0
  - scene: scene.sunset
  - delay:
      hours: 0
      minutes: 30
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      entity_id: light.aquarium_night_light
  - delay:
      hours: 7
      minutes: 30
      seconds: 0
      milliseconds: 0
  - service: automation.trigger
    target:
      entity_id: automation.aquarium_wake_up
  - delay:
      hours: 0
      minutes: 30
      seconds: 0
      milliseconds: 0
  - type: turn_on
    device_id: d9a64eb42220a161d1dd761ae689ac46
    entity_id: switch.aquarium_main_light
    domain: switch
  - service: light.turn_off
    target:
      entity_id: light.aquarium_night_light
  - delay:
      hours: 5
      minutes: 59
      seconds: 0
      milliseconds: 0
  - scene: scene.sunset
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: d9a64eb42220a161d1dd761ae689ac46
    entity_id: switch.aquarium_main_light
    domain: switch
  - delay:
      hours: 0
      minutes: 30
      seconds: 0
      milliseconds: 0
  - scene: scene.evening
  - delay:
      hours: 0
      minutes: 30
      seconds: 0
      milliseconds: 0
  - scene: scene.late_evening
  - delay:
      hours: 0
      minutes: 30
      seconds: 0
      milliseconds: 0
  - scene: scene.midnight
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: automation.trigger
    target:
      entity_id: automation.aquarium_sleep
mode: single

Could i kindly ask you to check the script and let me know if there is any error?

Thank you in advance
M

This is a terrible way to automate your aquarium lights.

If you restart home assistant or reload scripts the script stops and does not resume.

If however you create 12 separate automations, each with a time trigger, (or one automation with 12 time triggers and 12 choose actions) your lights will not fail to automate after a restart or reload of automations.

Wow Tom, you are so right… I didn’t think of that possibility… I am a beginner and i come from Google Home and the routine function in which you can set a full day routine based on time something like:

at 7:00
do this
at 8:00
do that

I do not know how to create one automation with 12 triggers and 12 choose actions. Could you please help with this?

Thank you

trigger:
  - platform: time
    at: '07:00'
    id: '7'
  - platform: time
    at: '07:30'
    id: '7.5'
  - platform: time
    at: '08:00'
    id: '8'
  - platform: time
    at: '15:30'
    id: '15.5'
...etc

action:
  - choose:
      - conditions:
          - "{{ trigger.id == '7' }}"
        sequence:
          - service: automation.trigger
            target:
              entity_id: automation.aquarium_wake_up
      - conditions:
          - "{{ trigger.id == '7.5' }}"
        sequence:
          - scene: scene.sunset
      - conditions:
          - "{{ trigger.id == '8' }}"
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.aquarium_night_light
      - conditions:
          - "{{ trigger.id == '15.5' }}"
        sequence:
          - service: automation.trigger
            target:
              entity_id: automation.aquarium_wake_up
...etc

https://www.home-assistant.io/docs/scripts#choose-a-group-of-actions

https://www.home-assistant.io/docs/scripts/conditions/#template-condition

https://www.home-assistant.io/docs/automation/templating/#available-trigger-data

1 Like

In addition to tom_l’s advice, don’t do this:

  - service: automation.trigger
    target:
      entity_id: automation.aquarium_wake_up

It’ll work but it’s not a recommended practice; don’t “force” an automation to be triggered. Ideally you should be calling a script there.

Post that automation’s code so we can understand what is involved in “aquarium wake up”.

Hi Tom,

You are again right, I implemented the code forcing the automation to start and it didn’t simply because it is also triggered on time. So the time in the automation acts as condition. Instead of the automation trigger I used the actions need to be performed in Assistant_Relay

This now works correctly but I need a full day to see that all the actions are performed. I will confirm in a couple of days!

Thank you very much for the support!!!

Kind regards
M

It doesn’t.

When you use the automation.trigger service call to trigger an automation, the automation’s trigger (and condition if it exists) is skipped. The service call simply executes the automation’s action and disregards the rest.

There’s a way to not skip the condition but the trigger is always skipped.

service: automation.trigger
data:
  skip_condition: false
target:
  entity_id: automation.example

However, like I said previously, it’s preferable to redesign the application so that there’s no need to employ the automation.trigger service call.