#!/bin/bash
#%# family=auto
#%# capabilities=autoconf

: << EOF
=head1 NAME

Outputs the current power, total yield, daily yield from a deye inverter.

=head1 CONFIGURATION

Tested with a "Deye SUN600G3-EU-230 600W" inverter.

Dependencies:
 - wget
 - awk
 - sed

plugin config:

  [deye]
  env.user <User>
  env.password <SECRET_PASS>
  env.ip <ip/fqdn>

=head1 AUTHOR

Michael Grote

=head1 LICENSE

GPLv3 or later

SPDX-License-Identifier: GPL-3.0-or-later

=head1 MAGIC MARKERS

 #%# family=auto

=cut

EOF



# Values in HTML
# sn = serial number
# msvn = Firmware version (main)
# ssvn = Firmware version (slave)
# pv_type = inverter _model
# rate_p = rated power
# now_p = current power
# today_e = yield today
# total_e = total yield
# alarm = alarm
# utime = last updated

# check if inverter is reachable
# inverter is off when there is not enough light to power it
# so it is safe to assume that current power etc. are zero
if ping "${ip}" -c1 -W 3 2>&1 > /dev/null ; then
    current_power=$(wget --quiet --user "${user}" --password "${password}" -O - "${ip}"/status.html | grep "var webdata_now_p" | awk 'BEGIN {FS="="}{print $2}' | sed 's/[^0-9]*//g')
    daily_yield=$(wget --quiet --user "${user}" --password "${password}" -O - "${ip}"/status.html | grep "var webdata_today_e" | awk 'BEGIN {FS="="}{print $2}' | sed 's/[^0-9]*//g')
    total_yield=$(wget --quiet --user "${user}" --password "${password}" -O - "${ip}"/status.html | grep "var webdata_total_e" | awk 'BEGIN {FS="="}{print $2}' | sed 's/[^0-9]*//g')
    reachable="1"
else
    current_power="0"
    daily_yield=U
    total_yield=U
    reachable="0"
fi



# wenn parameter = ...
if [ "$1" = "autoconf" ]; then
    echo yes
    exit 0
fi

if [ "$1" = "config" ]; then
    # setze optionen
    echo multigraph current_power
    echo graph_title Deye current Power
    echo 'graph_vlabel watt'
    echo 'graph_category sensors'
    echo 'graph_args -l 0'
    echo "graph_info Current generated power in Watt."
    echo current_power.label watt

    echo multigraph daily_yield
    echo graph_title Deye daily Yield
    echo 'graph_vlabel kWh'
    echo 'graph_category sensors'
    echo 'graph_args -l 0'
    echo "graph_info Power generated today."
    echo daily_yield.label kWh
    echo daily_yield.draw AREA

    echo multigraph total_yield
    echo graph_title Deye Total Yield
    echo 'graph_vlabel kWh'
    echo 'graph_category sensors'
    echo 'graph_args -l 0'
    echo "graph_info Total generated power."
    echo total_yield.label kWh
    echo total_yield.draw AREA

    echo multigraph reachable
    echo graph_title Deye inverter reachable
    echo 'graph_vlabel on/off'
    echo 'graph_category sensors'
    echo 'graph_args -l 0'
    echo "graph_info Is the Inverter is reachable? 1 is On, 0 is Off"
    echo reachable.label on/off
    echo reachable.draw AREA

    exit 0
fi

echo multigraph current_power
echo "current_power.value $current_power"

echo multigraph daily_yield
echo "daily_yield.value $daily_yield"

echo multigraph total_yield
echo "total_yield.value $total_yield"

echo multigraph reachable
echo "reachable.value $reachable"

exit 0
