Skip to content

Commit

Permalink
HA binary_sensor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MickMake committed May 9, 2023
1 parent b0f7ad2 commit aea8674
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 74 deletions.
25 changes: 14 additions & 11 deletions cmd/cmd_mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,10 @@ func (c *CmdMqtt) Update(endpoint string, data api.DataMap, newDay bool) error {
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 strings.Contains(r.EndPoint, "active") {
// 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() {
Expand Down Expand Up @@ -478,10 +479,9 @@ func (c *CmdMqtt) Update(endpoint string, data api.DataMap, newDay bool) error {
re.LastReset = r.Point.WhenReset(r.Date)
}

// if strings.Contains(r.EndPoint, "pv_to_grid_energy") {
// if strings.Contains(r.EndPoint, "active") {
// 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 refreshConfig {
c.LogPlainInfo("C")
Expand Down Expand Up @@ -595,12 +595,15 @@ func (c *CmdMqtt) UpdatePoint(entry *api.DataEntry) error {
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
}
// if entry.Point.Unit == "Bool" {
// entry.Point.Unit = mmHa.LabelBinarySensor
// }
// if entry.Point.ValueType == "Bool" {
// entry.Point.Unit = mmHa.LabelBinarySensor
// }
// if entry.Value.TypeValue == "Bool" {
// entry.Value.UnitValue = mmHa.LabelBinarySensor
// }

// Parent
if len(entry.Point.Parents.Map) == 0 {
Expand Down
8 changes: 8 additions & 0 deletions mmHa/binary_sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ func (config *EntityConfig) IsBinarySensor() bool {
var ok bool

for range Only.Once {
if config.Value.IsBool() {
ok = true
break
}
if config.Point.IsBool() {
ok = true
break
}
if config.Units == LabelBinarySensor {
ok = true
break
Expand Down
91 changes: 28 additions & 63 deletions mmHa/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,50 +481,15 @@ func (config *EntityConfig) FixConfig() {
// mdi:check-circle-outline | mdi:arrow-right-bold
// mdi:transmission-tower

// Set ValueTemplate
// Set default ValueTemplate
switch {
// fallthrough
//
// case config.Value.TypeValue == "Energy":
// fallthrough
//
// case config.Units == "MW":
// fallthrough
// case config.Units == "kW":
// fallthrough
// case config.Units == "W":
// fallthrough
//
// case config.Units == "kWp":
// fallthrough
// case config.Units == "MWh":
// fallthrough
// case config.Units == "kWh":
// fallthrough
// case config.Units == "Wh":
// fallthrough
//
// case config.Units == "kvar":
// fallthrough
// case config.Units == "Hz":
// fallthrough
// case config.Units == "V":
// fallthrough
// case config.Units == "A":
// fallthrough
// case config.Units == "°F":
// fallthrough
// case config.Units == "F":
// fallthrough
// case config.Units == "℉":
// fallthrough
// case config.Units == "°C":
// fallthrough
// case config.Units == "C":
// fallthrough
// case config.Units == "℃":
// fallthrough
// case config.Units == "%":
case config.Point.IsBool():
fallthrough
case config.Value.IsBool():
fallthrough
case config.IsBinarySensor():
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value }}")

case config.Value.IsFloat():
if !config.Value.Valid {
config.IgnoreUpdate = true
Expand All @@ -533,44 +498,44 @@ func (config *EntityConfig) FixConfig() {
if config.Value.String() == "" {
cnv = ""
}
if config.ValueName == "" {
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.value %s }}", cnv))
} else {
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s %s }}", config.ValueName, cnv))
vj := "value"
if config.ValueName != "" {
vj = config.ValueName
}
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s %s }}", vj, cnv))

case config.Value.IsBool():
fallthrough
case config.Value.Unit() == LabelBinarySensor:
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value }}")

case config.Value.Unit() == "DateTime":
fallthrough
case config.Value.TypeValue == "DateTime":
case config.Value.IsTypeDateTime():
vj := "value"
if config.ValueName != "" {
vj = config.ValueName
}
value, _, err := valueTypes.ParseDateTime(config.Value.String())
if err == nil {
config.Value.SetString(value.Local().Format(valueTypes.DateTimeFullLayout))
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value | as_datetime }}")
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s | as_datetime }}", vj))
} else {
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value }}")
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s }}", vj))
}

case config.Value.IsInt():
fallthrough
default:
config.ValueTemplate = SetDefault(config.ValueTemplate, "{{ value_json.value }}")
vj := "value"
if config.ValueName != "" {
vj = config.ValueName
}
config.ValueTemplate = SetDefault(config.ValueTemplate, fmt.Sprintf("{{ value_json.%s | int }}", vj))
}

// Set DeviceClass & Icon
switch {
case config.Units == "Bool":
case config.Point.IsBool():
fallthrough
case config.Value.IsBool():
fallthrough
case config.Units == LabelBinarySensor:
case config.IsBinarySensor():
config.DeviceClass = SetDefault(config.DeviceClass, "power")
config.Icon = SetDefault(config.Icon, "mdi:check-circle-outline")
// if !config.Value.Valid {
// config.Value = "false"
// }

case config.Value.TypeValue == "Power":
fallthrough
Expand Down

0 comments on commit aea8674

Please sign in to comment.