Backup Luxtronik *.dta files with ftp using telnet and expect

Hello Community, I’m sharing here my project to backup my luxtronik *.dta files with ftp using telnet and expect.

Prerequisite in this project

  • You have Luxtronik 2.0 and you have inserted a thumb usb-stick to your luxtronik control panel and enabled automated logging in control panel to this usb stick. Writing logs will happens automatically every 48 hour.
  • I’m using Home Assistant OS in this case. if you have your own OS, perhaps you do not need to install expect on every restart of HA.
  • We have 4 houses and therefor 4 heatpumps.

Let’s start on luxtronik. Create a script to transfer the files to a ftp server of your choice. Here you have an example. This script also moves transfered files to a backup folder on the stick:

#!/bin/sh
# Returncode 
RCA=0
RCB=0
RCC=0
RCD=0
I=0
# Serial Ordner lesen
SERIAL=`find /mnt/usb/* -type d -maxdepth 0 2> /dev/null`
RCA=$?
# Seriel dem Haus zuordnen
if [ $RCA -eq 0 ]
then
    case $SERIAL in 
    /mnt/usb/wwwwww-F43)
        HAUS=19
        RCB=0
    ;;
    /mnt/usb/xxxxxx-00D)
        HAUS=21
        RCB=0
    ;;
    /mnt/usb/yyyyyy-00E)
        HAUS=23
        RCB=0
    ;;
    /mnt/usb/zzzzzz-001)
        HAUS=25
        RCB=0
    ;;
    *)
        HAUS=XX
        RCB=1
        RCC=999
        RCD=999
    ;;
    esac
fi

# Jedes File transferieren
if [ $RCB -eq 0 ]
then
    for FILE in $SERIAL/*
    do 
        if [ -f "$FILE" ] && [ $RCC -eq 0 ]
        then 
            ftpput -v -u user -p password ip_of_destination /share/luxtronik/dta/$HAUS/${FILE##*/} $FILE 1>/dev/null 2>/dev/null
            RCC=$?
            I=$((I+1))
        fi
    done
    if [ $RCC -ne 0 ]
    then
        RCD=999
    fi
fi

# Daten in Archiv Ordner verschieben
if [ $I -gt 0 ] && [ $RCC -eq 0 ]
then 
    DATUM=`date +"%y%m%d-%H%M" 2>/dev/null`
    RCD=$?
    if [ $RCD -eq 0 ]
    then
        mkdir ${SERIAL}/${DATUM} 1>/dev/null 2>/dev/null
        RCD=$?
    fi

    # DTA Files in das Archiv transferieren
    if [ $RCD -eq 0 ]
    then
        for FILE in ${SERIAL}/*.dta
        do
            if [ -f "$FILE" ] && [ $RCD -eq 0 ]
            then 
                mv ${FILE} ${SERIAL}/${DATUM}/ 1>/dev/null 2>/dev/null
                RCD=$?
            fi
        done
    fi    
    # CSV Files in das Archiv transferieren
    if [ $RCD -eq 0 ]
    then
        for FILE in ${SERIAL}/*.csv
        do
            if [ -f "$FILE" ] && [ $RCD -eq 0 ]
            then 
                mv ${FILE} ${SERIAL}/${DATUM}/ 1>/dev/null 2>/dev/null
                RCD=$?
            fi
        done
    fi    
    # ERR Files in das Archiv transferieren
    if [ $RCD -eq 0 ]
    then
        for FILE in ${SERIAL}/*.err
        do
            if [ -f "$FILE" ] && [ $RCD -eq 0 ]
            then 
                mv ${FILE} ${SERIAL}/${DATUM}/ 1>/dev/null 2>/dev/null
                RCD=$?
            fi
        done
    fi    
fi

echo "Return:$RCA-$RCB-$RCC-$RCD-$I"

put this script to you ftp-destination, and log in to luxtronik with telnet. standard user is root, standard password is empty. From here, dive to /mnt/usb location and invoke ftpget to get the script from your ftp server.

ftpget -v -u user -p password ip_of_destination transfer2ha.sh /share/luxtronik/dta/transfer2ha.sh

Before we go to Home Assistant, test if your script works locally on Luxtronik. Now we start implementation in Home Assistant.
First, you need telnet and expect in your Home Assistant Container. You have to install that on every Upgrade and Restart.
Create a Script /config/scripts/setup_apk.sh:

#!/bin/sh
apk update
apk add -u busybox
apk add busybox-extras
apk add expect

Create an entry in your configuration.yaml

shell_command:
  setup_apk: !secret shell_setup_apk

Create an entry in your secrets.yaml. It’s up to you, if you want to define script without secrets.yaml. I have many scripts with information as parameter, I do not want to have public in github and therefore I put every script to secrets.yaml.

shell_setup_apk: /config/scripts/setup_apk.sh

Now, we also create an Automation, that starts on every restart.

alias: System Startup Automation (Install apk)
trigger:
  - platform: homeassistant
    event: start
action:
  - service: shell_command.setup_apk

Next step is to create you shell script, invoking the script on your luxtronik with expect.
Create a Script /config/scripts/luxtronik_to_ha.sh:

#!/bin/bash
luxtronik_ip=$1
luxtronik_user=$2
luxtronik_password=$3
expect <<END
spawn telnet ${luxtronik_ip}
expect "(none) login:"
send "${luxtronik_user}\r"
expect "Password:"
send "${luxtronik_password}\r"
expect
send "/mnt/usb/transfer2ha.sh\r"
expect
send "exit\r"
expect eof
END

Create an entry in your configuration.yaml

shell_command:
  luxtronik_to_ha: !secret shell_luxtronik_to_ha

Create an entry in your secrets.yaml. It’s up to you, if you want to define script without secrets.yaml. I have many scripts with information as parameter, I do not want to have public in github and therefore I put every script to secrets.yaml.

shell_luxtronik_to_ha: /config/scripts/luxtronik_to_ha.sh ip_of_destination root ""

Now, we also create an Automation, that starts your backup. here, I pasted only the actions. You also find a example to create a notification.

  - service: shell_command.luxtronik_to_ha
    data: {}
    response_variable: scriptanswer
  - if:
      - condition: template
        value_template: "{{ scriptanswer['stdout'] | regex_search('Return:0-0-0-0-[0-9]*') }}"
    then:
      - service: notify.your_user
        data:
          message: "{{scriptanswer['stdout']}}"
          title: >-
            HA-Info Heatpump - Dateien gesichert mit {{
            (scriptanswer['stdout'] |
            regex_findall('Return:[0]-[0]-[0]-[0]-[0-9]*'))[0] }}
    else:
      - service: notify.your_user
        data:
          message: "{{scriptanswer['stdout']}}"
          title: >-
            HA-Alert Heatpump - Dateien sichern FEHLGESCHLAGEN mit {{
            (scriptanswer['stdout'] |
            regex_findall('Return:[0-9]*-[0-9]*-[0-9]*-[0-9]*-[0-9]*'))[0] }}

Have fun … and if you do not know this software to visualize your luxtronik dta files, here is a link: OpenDTA download | SourceForge.net