Skip to content

Commit

Permalink
v3.0.4-beta - Energy dashboard fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
MickMake committed Dec 27, 2022
1 parent fbbaf2f commit 5acee08
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 96 deletions.
26 changes: 12 additions & 14 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 50 additions & 33 deletions cmd/cmd_mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type CmdMqtt struct {
Client *mmHa.Mqtt
endpoints MqttEndPoints
points getDevicePointAttrs.PointsMap
previous map[string]*api.DataEntries

optionLogLevel int
optionSleepDelay time.Duration
Expand All @@ -67,6 +68,7 @@ func NewCmdMqtt() *CmdMqtt {
optionLogLevel: LogLevelInfo,
optionSleepDelay: time.Second * 40, // Takes up to 40 seconds for data to come in.
optionFetchSchedule: time.Minute * 5,
previous: make(map[string]*api.DataEntries, 0),
}
}

Expand All @@ -84,8 +86,8 @@ func (c *CmdMqtt) AttachCommand(cmd *cobra.Command) *cobra.Command {
var cmdMqtt = &cobra.Command{
Use: "mqtt",
Aliases: []string{""},
Short: fmt.Sprintf("Connect to a HASSIO broker."),
Long: fmt.Sprintf("Connect to a HASSIO broker."),
Short: "Connect to a HASSIO broker.",
Long: "Connect to a HASSIO broker.",
DisableFlagParsing: false,
DisableFlagsInUseLine: false,
PreRunE: nil,
Expand All @@ -99,8 +101,8 @@ func (c *CmdMqtt) AttachCommand(cmd *cobra.Command) *cobra.Command {
var cmdMqttRun = &cobra.Command{
Use: "run",
Aliases: []string{""},
Short: fmt.Sprintf("One-off sync to a HASSIO broker."),
Long: fmt.Sprintf("One-off sync to a HASSIO broker."),
Short: "One-off sync to a HASSIO broker.",
Long: "One-off sync to a HASSIO broker.",
DisableFlagParsing: false,
DisableFlagsInUseLine: false,
PreRunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -124,8 +126,8 @@ func (c *CmdMqtt) AttachCommand(cmd *cobra.Command) *cobra.Command {
var cmdMqttSync = &cobra.Command{
Use: "sync",
Aliases: []string{""},
Short: fmt.Sprintf("Sync to a HASSIO MQTT broker."),
Long: fmt.Sprintf("Sync to a HASSIO MQTT broker."),
Short: "Sync to a HASSIO MQTT broker.",
Long: "Sync to a HASSIO MQTT broker.",
DisableFlagParsing: false,
DisableFlagsInUseLine: false,
PreRunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -150,13 +152,13 @@ func (c *CmdMqtt) AttachCommand(cmd *cobra.Command) *cobra.Command {

func (c *CmdMqtt) AttachFlags(cmd *cobra.Command, viper *viper.Viper) {
for range Only.Once {
cmd.PersistentFlags().StringVarP(&c.MqttUsername, flagMqttUsername, "", "", fmt.Sprintf("HASSIO: mqtt username."))
cmd.PersistentFlags().StringVarP(&c.MqttUsername, flagMqttUsername, "", "", "HASSIO: mqtt username.")
viper.SetDefault(flagMqttUsername, "")
cmd.PersistentFlags().StringVarP(&c.MqttPassword, flagMqttPassword, "", "", fmt.Sprintf("HASSIO: mqtt password."))
cmd.PersistentFlags().StringVarP(&c.MqttPassword, flagMqttPassword, "", "", "HASSIO: mqtt password.")
viper.SetDefault(flagMqttPassword, "")
cmd.PersistentFlags().StringVarP(&c.MqttHost, flagMqttHost, "", "", fmt.Sprintf("HASSIO: mqtt host."))
cmd.PersistentFlags().StringVarP(&c.MqttHost, flagMqttHost, "", "", "HASSIO: mqtt host.")
viper.SetDefault(flagMqttHost, "")
cmd.PersistentFlags().StringVarP(&c.MqttPort, flagMqttPort, "", "", fmt.Sprintf("HASSIO: mqtt port."))
cmd.PersistentFlags().StringVarP(&c.MqttPort, flagMqttPort, "", "", "HASSIO: mqtt port.")
viper.SetDefault(flagMqttPort, "")
}
}
Expand Down Expand Up @@ -392,9 +394,22 @@ func (c *CmdMqtt) Update(endpoint string, data api.DataMap, newDay bool) error {
c.LogInfo("Syncing %d entries with HASSIO from %s.\n", len(data.Map), endpoint)

for o := range data.Map {
refreshConfig := newDay

entries := data.Map[o]
r := entries.GetEntry(api.LastEntry) // Gets the last entry

if strings.Contains(r.EndPoint, "pv_to_grid_energy") {
fmt.Printf("EMPTY[%s] -> %s\n", r.EndPoint, r.Value.String())
}
if _, ok := c.previous[o]; ok {
previous := c.previous[o].GetEntry(api.LastEntry)
if r.Value.String() != previous.Value.String() {
refreshConfig = true
}
}
c.previous[o] = entries

if !r.Point.Valid {
// Any point that shouldn't be passed through to MQTT is ignored
// - includes child points of an aggregate of several points.
Expand Down Expand Up @@ -425,15 +440,15 @@ func (c *CmdMqtt) Update(endpoint string, data api.DataMap, newDay bool) error {
name = mmHa.JoinStringsForName(" - ", r.Parent.Key, r.Point.Id, r.Point.GroupName, r.Point.Description)
}

if r.Point.Unit == "" {
r.Point.Unit = r.Point.ValueType
}
if r.Point.Unit == "Bool" {
r.Point.Unit = mmHa.LabelBinarySensor
}
if r.Point.ValueType == "Bool" {
r.Point.Unit = mmHa.LabelBinarySensor
}
// if r.Point.Unit == "" {
// r.Point.Unit = r.Point.ValueType
// }
// if r.Point.Unit == "Bool" {
// r.Point.Unit = mmHa.LabelBinarySensor
// }
// if r.Point.ValueType == "Bool" {
// r.Point.Unit = mmHa.LabelBinarySensor
// }

re := mmHa.EntityConfig {
Name: name, // mmHa.JoinStringsForName(" - ", id), // r.Point.Name, // PointName,
Expand All @@ -454,28 +469,21 @@ func (c *CmdMqtt) Update(endpoint string, data api.DataMap, newDay bool) error {
Icon: r.Current.PointIcon(),
UpdateFreq: r.Current.DataStructure.PointUpdateFreq,

LastReset: r.Point.WhenReset(),
// LastReset: "",
// LastResetValueTemplate: "",
}

// if strings.Contains(r.EndPoint, "grid_to_load_energy") {
// fmt.Printf("EMPTY[%s] -> %s\n", r.EndPoint, r.Value.String())
// }
// if r.Current.DataStructure.PointUpdateFreq != "" {
// fmt.Printf("EMPTY[%s] -> %s\n", r.EndPoint, r.Value.String())
// }
re.FixConfig()

switch {
case r.Point.IsTotal():
re.StateClass = "total"
default:
re.StateClass = "measurement"
if re.LastResetValueTemplate != "" {
re.LastReset = r.Point.WhenReset(r.Date)
}

// if strings.Contains(r.EndPoint, "pv_to_grid_energy") {
// fmt.Printf("EMPTY[%s] -> %s\n", r.EndPoint, r.Value.String())
// }
// fmt.Printf("UNIT[%s] -> %s / %s / %s / %s /\n", id, r.Point.GroupName, r.Point.Unit, r.Point.ValueType, r.Point.Description)

if newDay {
if refreshConfig {
c.LogPlainInfo("C")
c.LogDebug("Config: [%s]\n", r.EndPoint)
c.Error = c.Client.BinarySensorPublishConfig(re)
Expand Down Expand Up @@ -584,6 +592,15 @@ func (c *CmdMqtt) UpdatePoint(entry *api.DataEntry) error {
entry.Point.Unit = p.Unit.String()
// fmt.Printf("[%s] -> %s\n", entry.EndPoint, entry.Value.String())
}
if entry.Point.Unit == "" {
entry.Point.Unit = entry.Point.ValueType
}
if entry.Point.Unit == "Bool" {
entry.Point.Unit = mmHa.LabelBinarySensor
}
if entry.Point.ValueType == "Bool" {
entry.Point.Unit = mmHa.LabelBinarySensor
}

// Parent
if len(entry.Point.Parents.Map) == 0 {
Expand Down
20 changes: 10 additions & 10 deletions iSolarCloud/AppService/getPsDetail/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,25 +237,25 @@ type ResultData struct {
UUID valueTypes.Integer `json:"uuid"`
EnergyFlow []valueTypes.Integer `json:"energy_flow" PointName:"Energy Flow" PointIgnoreZero:"false" PointArrayFlatten:"true"`

P13003Map valueTypes.UnitValue `json:"p13003_map" PointId:"p13003" PointName:"PV Power To Load" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreqInstant" PointVirtual:"true" PointVirtualShift:"3"`
P13003Map valueTypes.UnitValue `json:"p13003_map" PointId:"p13003" PointName:"PV Power To Load" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreq5Mins" PointVirtual:"true" PointVirtualShift:"3"`
P13003MapVirgin valueTypes.UnitValue `json:"p13003_map_virgin" PointIgnore:"true"`
P13011Map valueTypes.UnitValue `json:"p13011_map" PointId:"p13011" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreqInstant" PointVirtual:"true" PointVirtualShift:"3"`
P13011Map valueTypes.UnitValue `json:"p13011_map" PointId:"p13011" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreq5Mins" PointVirtual:"true" PointVirtualShift:"3"`
P13011MapVirgin valueTypes.UnitValue `json:"p13011_map_virgin" PointIgnore:"true"`
P13115Map valueTypes.UnitValue `json:"p13115_map" PointId:"p13115" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreqInstant" PointVirtual:"true" PointVirtualShift:"3"`
P13115Map valueTypes.UnitValue `json:"p13115_map" PointId:"p13115" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreq5Mins" PointVirtual:"true" PointVirtualShift:"3"`
P13115MapVirgin valueTypes.UnitValue `json:"p13115_map_virgin" PointIgnore:"true"`
P13119Map valueTypes.UnitValue `json:"p13119_map" PointId:"p13119" PointName:"Load Power" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreqInstant" PointVirtual:"true" PointVirtualShift:"3"`
P13119Map valueTypes.UnitValue `json:"p13119_map" PointId:"p13119" PointName:"Load Power" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreq5Mins" PointVirtual:"true" PointVirtualShift:"3"`
P13119MapVirgin valueTypes.UnitValue `json:"p13119_map_virgin" PointIgnore:"true"`
P13121Map valueTypes.UnitValue `json:"p13121_map" PointId:"p13121" PointName:"PV Power To Grid" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreqInstant" PointVirtual:"true" PointVirtualShift:"3"`
P13121Map valueTypes.UnitValue `json:"p13121_map" PointId:"p13121" PointName:"PV Power To Grid" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreq5Mins" PointVirtual:"true" PointVirtualShift:"3"`
P13121MapVirgin valueTypes.UnitValue `json:"p13121_map_virgin" PointIgnore:"true"`
P13126Map valueTypes.UnitValue `json:"p13126_map" PointId:"p13126" PointName:"PV Power To Battery" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreqInstant" PointVirtual:"true" PointVirtualShift:"3"`
P13126Map valueTypes.UnitValue `json:"p13126_map" PointId:"p13126" PointName:"PV Power To Battery" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreq5Mins" PointVirtual:"true" PointVirtualShift:"3"`
P13126MapVirgin valueTypes.UnitValue `json:"p13126_map_virgin" PointIgnore:"true"`
P13141 valueTypes.Float `json:"p13141" PointName:"Battery Charge Percent" PointUnit:"%" PointIcon:"mdi:battery" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreq5Mins" PointVirtual:"true" PointVirtualShift:"3"`
P13142 valueTypes.Float `json:"p13142" PointName:"Battery Health" PointUnit:"%" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreqInstant" PointVirtual:"true" PointVirtualShift:"3"`
P13149Map valueTypes.UnitValue `json:"p13149_map" PointId:"p13149" PointName:"Grid Power To Load" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreqInstant" PointVirtual:"true" PointVirtualShift:"3"`
P13142 valueTypes.Float `json:"p13142" PointName:"Battery Health" PointUnit:"%" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreq5Mins" PointVirtual:"true" PointVirtualShift:"3"`
P13149Map valueTypes.UnitValue `json:"p13149_map" PointId:"p13149" PointName:"Grid Power To Load" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreq5Mins" PointVirtual:"true" PointVirtualShift:"3"`
P13149MapVirgin valueTypes.UnitValue `json:"p13149_map_virgin" PointIgnore:"true"`
P13150Map valueTypes.UnitValue `json:"p13150_map" PointId:"p13150" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreqInstant" PointVirtual:"true" PointVirtualShift:"3"`
P13150Map valueTypes.UnitValue `json:"p13150_map" PointId:"p13150" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreq5Mins" PointVirtual:"true" PointVirtualShift:"3"`
P13150MapVirgin valueTypes.UnitValue `json:"p13150_map_virgin" PointIgnore:"true"`
P13155 valueTypes.Float `json:"p13155" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreqInstant" PointVirtual:"true" PointVirtualShift:"3"`
P13155 valueTypes.Float `json:"p13155" PointDeviceFrom:"PsKey" PointUpdateFreq:"UpdateFreq5Mins" PointVirtual:"true" PointVirtualShift:"3"`
} `json:"storage_inverter_data" DataTable:"false"`
}

Expand Down

0 comments on commit 5acee08

Please sign in to comment.