RPi Zero (rpi_gpio) + 8 channel relay: how to configure the default state of relays after a power outage?

Hello,

I use RPi Zero (rpi_gpio) and a multi channel relay to control some devices. It is configured as follows:

switch:
  - platform: rpi_gpio
    ports:
      17: aquarium_lights
      18: aquarium_oxygen_pump

Everything works nice so far. The only problem I face is the state of the system after a power outage. Right now after a power outage all GPIOs are turned ON after the boot. How can I configure which GPIOs I would like to be ON and which OFF after an power outage reboot? Like in my example above - I would like RPi to turn aquarium_lights OFF and aquarium_oxygen_pump ON by default on each power outage reboot.

Thank you!

Use the home assistant start event to trigger an automation that sets the relays how you want.

  trigger:
    platform: homeassistant
    event: start

And where/how do I set the relays in the state I want? Can you give an example for the above mentioned GPIOs 17 and 18?

  trigger:
    platform: homeassistant
    event: start
  action:
    service: switch.turn_off
    entity_id: switch.aquarium_lights

The O2 pump is on when home assistant starts so no need to use the switch.turn_on service for that.

1 Like

Do you have these lights on a schedule?

You can check if you should turn them off using a condition. For example if the lights are normally on between 8am and 6pm:

  trigger:
    platform: homeassistant
    event: start
  condition:
    condition: time
    after: '18:00:00'
    before: '08:00:00'
  action:
    service: switch.turn_off
    entity_id: switch.aquarium_lights

This way the lights won’t turn off if they are supposed to be on.

1 Like

I actually have several automatons defined for all my relays. Is there such a thing as launch all automations on start event? This way I will not have to mention my conditions twice and I will be able to edit them (once needed) in one place instead of two. Does it make sense?

Two ways,

  1. Add this extra trigger to all the automations you want to run on start:
  trigger:
    - platform: homeassistant
      event: start
    - platform: <your existing triggers...>

OR

  1. Add a list of automations to trigger in the start detection automation.
  trigger:
    platform: homeassistant
    event: start
  action:
    service: automation.trigger
    data:
      skip_condition: false
      entity_id: 
        - automation.one_of your_automations_you_want_to_run_on_start
        - automation.and_another
        - automation.and_yet_another
        - etc...

You might want to break up the triggering a bit with delays if there are lots of automations:

  trigger:
    platform: homeassistant
    event: start
  action:
  - service: automation.trigger
    data:
      skip_condition: false
      entity_id: automation.one_of your_automations_you_want_to_run_on_start
  - delay: 1
  - service: automation.trigger
    data:
      skip_condition: false
      entity_id: automation.and_another
  - delay: 1
  - service: automation.trigger
    data:
      skip_condition: false
      entity_id: automation.and_yet_another
  - delay: 1
  - service: automation.trigger
    data:
      skip_condition: false
      entity_id: etc...
1 Like

If those automations have conditions inside - will the executions of those automations be conditioned or just executed without checking the conditions?

1 Like