Powerloss backup Relay switch - How to return previous state?

Helloy

I’m trying to make a relay control switch in my Raspberry Pi device. I need this device to return the state of relays after power loss. Is this possible? I’ve red all most of the previous posts but those don’t seem work for me. I’m also having some odd behaviour with my MQTT backup. Basicly, it only returns some states of the 4 switches and not all - you can not ever tell which relay it is backing up. The automation script works if there’s only 1 relay switch. Currently I dont have Raspberry at my location so I’m using other board for developing. Here’s my yaml:

#Restart & Debugging
#sudo systemctl restart home-assistant@homeassistant && sudo journalctl -f -u home-assistant@homeassistant | grep -i 'error'
#
homeassistant:
  name: !secret name_UI
  latitude: !secret home_lat
  longitude: !secret home_lon
  elevation: !secret home_elev
  time_zone: !secret home_time_zone
  unit_system: metric
frontend:
config:
http:
  api_password: !secret http_password

mqtt:
  broker: localhost
  username: !secret mqtt_username
  password: !secret mqtt_password
  protocol: 3.1
  keepalive: 60
#account details in /home/homeassistant/.homeassistant/secrets.yaml



mqtt_statestream:
  base_topic: homeassistant
  include:
    entities:
      - switch.rele1
      - switch.rele2
      - switch.rele3
      - switch.rele4

binary_sensor 11:
  - platform: mqtt
    name: rele1sensor
    state_topic: "homeassistant/switch/rele1/state"
    retain: true
    payload_on: "on"
    payload_off: "off"
    qos: 1
binary_sensor 21:
  - platform: mqtt
    name: rele2sensor
    state_topic: "homeassistant/switch/rele2/state"
    retain: true
    payload_on: "on"
    payload_off: "off"
    qos: 1
binary_sensor 31:
  - platform: mqtt
    name: rele3sensor
    state_topic: "homeassistant/switch/rele3/state"
    retain: true
    payload_on: "on"
    payload_off: "off"
    qos: 1
binary_sensor 41:
  - platform: mqtt
    name: rele4sensor
    state_topic: "homeassistant/switch/rele4/state"
    retain: true
    payload_on: "on"
    payload_off: "off"
    qos: 1

automation 0:
  hide_entity: true
  trigger:
    platform: homeassistant
    event: start
  condition:
    condition: template
    value_template: "{{ is_state('binary_sensor.rele1sensor', 'on') }}"
  action:
    - service: switch.turn_on
      entity_id: switch.rele1

automation 1:
  hide_entity: true
  trigger:
    platform: homeassistant
    event: start
  condition:
    condition: template
    value_template: "{{ is_state('binary_sensor.rele2sensor', 'on') }}"
  action:
    - service: switch.turn_on
      entity_id: switch.rele2

automation 2:
  hide_entity: true
  trigger:
    platform: homeassistant
    event: start
  condition:
    condition: template
    value_template: "{{ is_state('binary_sensor.rele3sensor', 'on') }}"
  action:
    - service: switch.turn_on
      entity_id: switch.rele3

automation 3:
  hide_entity: true
  trigger:
    platform: homeassistant
    event: start
  condition:
    condition: template
    value_template: "{{ is_state('binary_sensor.rele4sensor', 'on') }}"
  action:
    - service: switch.turn_on
      entity_id: switch.rele4

#Testting switches
switch:
  platform: command_line
  switches:
    rele1:
      command_on: "echo 1"
      command_off: "echo 0"
    rele2:
      command_on: "echo 1"
      command_off: "echo 0"
    rele3:
      command_on: "echo 1"
      command_off: "echo 0"
    rele4:
      command_on: "echo 1"
      command_off: "echo 0"
#These are only here for testing purposes
#switch:
#  - platform: rpi_gpio
#    ports:
#      17: rele1
#      27: rele2
#      22: rele3
#    invert_logic: true
#switch 12:
#  - platform: rpi_gpio
#    ports:
#      5: rele4
#    invert_logic: false

Switches start in the ‘off’ mode so I dont need the turn_off automation scripts - just the turn_on.
I have already modified Mosquitto to save db every 30s and NOT save on change. This was suggested in other forum post. Do you think that saving every change would work? I could actually test this after posting this plea for help.

Could you push me in the right direction? Any help is appreciated. Thanks!

My mosquitto conf looks like this:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d


autosave_interval 30
autosave_on_changes true

Autosave seems to have zero effect on the persistence/retainability of relay switches. Regardless of how you setup interval or whether changesave is true or false.

I got this working finally.

Here’s the config file that did it:

#root@pine64:/home/homeassistant/.homeassistant# cat configuration.yaml
#Restart & Debugging
#sudo systemctl restart home-assistant@homeassistant && sudo journalctl -f -u home-assistant@homeassistant | grep -i 'error'
#
homeassistant:
  name: !secret name_UI
  latitude: !secret home_lat
  longitude: !secret home_lon
  elevation: !secret home_elev
  time_zone: !secret home_time_zone
  unit_system: metric
  customize:
    switch.rele1:
      hidden: true
    switch.rele2:
      hidden: true
    switch.rele3:
      hidden: true
    switch.rele4:
      hidden: true
frontend:
config:
http:
  api_password: !secret http_password
recorder:
  include:
    domains:
#     - switch
      - input_boolean
mqtt:
  broker: localhost
  username: !secret mqtt_username
  password: !secret mqtt_password
  protocol: 3.1
  keepalive: 60
#account details in /home/homeassistant/.homeassistant/secrets.yaml

automation:
  - alias: rele1naa
    hide_entity: true
    trigger:
      platform: state
      entity_id: input_boolean.rele1ohjaus
      to: 'on'
    action:
      - service: switch.turn_on
        entity_id: switch.rele1
  - alias: rele1nuu
    hide_entity: true
    trigger:
      platform: state
      entity_id: input_boolean.rele1ohjaus
      to: 'off'
    action:
      - service: switch.turn_off
        entity_id: switch.rele1
  - alias: rele2naa
    hide_entity: true
    trigger:
      platform: state
      entity_id: input_boolean.rele2ohjaus
      to: 'on'
    action:
      - service: switch.turn_on
        entity_id: switch.rele2
  - alias: rele2nuu
    hide_entity: true
    trigger:
      platform: state
      entity_id: input_boolean.rele2ohjaus
      to: 'off'
    action:
      - service: switch.turn_off
        entity_id: switch.rele2
  - alias: rele3naa
    hide_entity: true
    trigger:
      platform: state
      entity_id: input_boolean.rele3ohjaus
      to: 'on'
    action:
      - service: switch.turn_on
        entity_id: switch.rele3
  - alias: rele3nuu
    hide_entity: true
    trigger:
      platform: state
      entity_id: input_boolean.rele3ohjaus
      to: 'off'
    action:
      - service: switch.turn_off
        entity_id: switch.rele3
  - alias: rele4naa
    hide_entity: true
    trigger:
      platform: state
      entity_id: input_boolean.rele4ohjaus
      to: 'on'
    action:
      - service: switch.turn_on
        entity_id: switch.rele4
  - alias: rele4nuu
    hide_entity: true
    trigger:
      platform: state
      entity_id: input_boolean.rele4ohjaus
      to: 'off'
    action:
      - service: switch.turn_off
        entity_id: switch.rele4
  - alias: rele1auton
    hide_entity: true
    trigger:
      platform: homeassistant
      event: start
    condition:
      condition: state
      entity_id: input_boolean.rele1ohjaus
      state: 'on'
    action:
      - service: switch.turn_on
        entity_id: switch.rele1
  - alias: rele1autoff
    hide_entity: true
    trigger:
      platform: homeassistant
      event: start
    condition:
      condition: state
      entity_id: input_boolean.rele1ohjaus
      state: 'off'
    action:
      - service: switch.turn_off
        entity_id: switch.rele1
  - alias: rele2auton
    hide_entity: true
    trigger:
      platform: homeassistant
      event: start
    condition:
      condition: state
      entity_id: input_boolean.rele2ohjaus
      state: 'on'
    action:
      - service: switch.turn_on
        entity_id: switch.rele2
  - alias: rele2autoff
    hide_entity: true
    trigger:
      platform: homeassistant
      event: start
    condition:
      condition: state
      entity_id: input_boolean.rele2ohjaus
      state: 'off'
    action:
      - service: switch.turn_off
        entity_id: switch.rele2
  - alias: rele3auton
    hide_entity: true
    trigger:
      platform: homeassistant
      event: start
    condition:
      condition: state
      entity_id: input_boolean.rele3ohjaus
      state: 'on'
    action:
      - service: switch.turn_on
        entity_id: switch.rele3
  - alias: rele3autoff
    hide_entity: true
    trigger:
      platform: homeassistant
      event: start
    condition:
      condition: state
      entity_id: input_boolean.rele3ohjaus
      state: 'off'
    action:
      - service: switch.turn_off
        entity_id: switch.rele3
  - alias: rele4auton
    hide_entity: true
    trigger:
      platform: homeassistant
      event: start
    condition:
      condition: state
      entity_id: input_boolean.rele4ohjaus
      state: 'on'
    action:
      - service: switch.turn_on
        entity_id: switch.rele4
  - alias: rele1autoff
    hide_entity: true
    trigger:
      platform: homeassistant
      event: start
    condition:
      condition: state
      entity_id: input_boolean.rele4ohjaus
      state: 'off'
    action:
      - service: switch.turn_off
        entity_id: switch.rele4

input_boolean:
  rele1ohjaus:
    name: "ALAKERTA"
    icon: mdi:thermometer-lines #https://materialdesignicons.com/cheatsheet
  rele2ohjaus:
    name: "YLÄKERTA"
    icon: mdi:temperature-celsius
  rele3ohjaus:
    name: "SCHUKO"
    icon: mdi:power-settings
  rele4ohjaus:
    name: "ULKOVALOT"
    icon: mdi:lightbulb-on-outline
  
switch:
  platform: command_line
  switches:
    rele1:
      command_on: "echo 1"
      command_off: "echo 0"
    rele2:
      command_on: "echo 1"
      command_off: "echo 0"
    rele3:
      command_on: "echo 1"
      command_off: "echo 0"
    rele4:
      command_on: "echo 1"
      command_off: "echo 0"

#switch 2:
#  platform: rpi_gpio
#  ports:
#    17: rele1
#    27: rele2
#    5: rele4
#  invert_logic: true
#switch 3:
#  platform: rpi_gpio
#  ports:
#    22: rele3
#  invert_logic: false

Thank you for your moral support :wink: