Skip to content

Commit

Permalink
feat: past month data points (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
geertmeersman authored Jun 20, 2023
2 parents c6eb156 + 8f70c39 commit fe5c9de
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 11 deletions.
137 changes: 129 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ A Home Assistant integration allowing to monitor your EV charging and manage you
The Pull request is still pending merge for the hacs-default repository. So until that time, add my repository as a custom repository in hacs and the integration will show up.

Explanation: https://hacs.xyz/docs/faq/custom_repositories/

```
Repository: geertmeersman/nexxtmove
Category: Integration
Expand Down Expand Up @@ -74,14 +75,17 @@ If you want to contribute to this please read the [Contribution guidelines](CONT

## Troubleshooting

1. You can enable logging for this integration specifically and share your logs, so I can have a deep dive investigation. To enable logging, update your `configuration.yaml` like this, we can get more information in Configuration -> Logs page
### ENABLING DEBUG LOGGING

```
logger:
default: warning
logs:
custom_components.nexxtmove: debug
```
To enable debug logging, go to Settings -> Devices & Services and then click the triple dots for the Nexxtmove integration and click Enable Debug Logging.

![enable-debug-logging](https://raw.githubusercontent.com/geertmeersman/nexxtmove/main/images/screenshots/enable-debug-logging.gif)

### DISABLE DEBUG LOGGING AND DOWNLOAD LOGS

Once you enable debug logging, you ideally need to make the error happen. Run your automation, change up your device or whatever was giving you an error and then come back and disable Debug Logging. Disabling debug logging is the same as enabling, but now you will see Disable Debug Logging. After you disable debug logging, it will automatically prompt you to download your log file. Please provide this logfile.

![disable-debug-logging](https://raw.githubusercontent.com/geertmeersman/nexxtmove/main/images/screenshots/disable-debug-logging.gif)

## Lovelace examples

Expand All @@ -106,7 +110,6 @@ entities:
- ${sensor.charges}
card:
type: vertical-stack
title: Geert Meersman
cards:
- type: custom:apexcharts-card
apex_config:
Expand Down Expand Up @@ -302,6 +305,124 @@ card:

</details>

![Last Month Period Graphs](https://github.com/geertmeersman/nexxtmove/raw/main/images/screenshots/lovelace_past_month_consumption.png)

<details><summary>Show markdown code</summary>

**Replace &lt;username&gt; by your Nexxtmove username and &lt;deviceid&gt; by your Nexxtmove charging device id**

```
type: custom:config-template-card
variables:
sensor:
cost: sensor.nexxtmove_<username>_charging_device_<deviceid>_month_cost
energy: sensor.nexxtmove_<username>_charging_device_<deviceid>_month_energy
charges: sensor.nexxtmove_<username>_charging_device_<deviceid>_month_charges
entities:
- ${sensor.cost}
- ${sensor.energy}
- ${sensor.charges}
card:
type: vertical-stack
cards:
- type: custom:apexcharts-card
apex_config:
tooltip:
enabled: true
followCursor: true
x:
show: false
format: dd MMMM yyyy
'y':
show: true
graph_span: 1month
header:
standard_format: false
show: true
show_states: false
title: ${'Nexxtmove costs for the past month €'}
now:
show: true
label: Today
series:
- entity: ${sensor.cost}
name: Home
type: column
color: 73C56C
show:
legend_value: false
float_precision: 2
data_generator: |
return entity.attributes.dates.map((day, index) => {
return [new Date(day), entity.attributes.values[index].home];
});
- type: custom:apexcharts-card
apex_config:
tooltip:
enabled: true
followCursor: true
x:
show: false
format: dd MMMM yyyy
'y':
show: true
graph_span: 1month
header:
standard_format: false
show: true
show_states: false
title: ${'Nexxtmove consumption for the past month kWh'}
now:
show: true
label: Today
series:
- entity: ${sensor.energy}
name: Home
type: column
color: 73C56C
show:
legend_value: false
float_precision: 2
data_generator: |
return entity.attributes.dates.map((day, index) => {
return [new Date(day), entity.attributes.values[index].home/1000];
});
- type: custom:apexcharts-card
apex_config:
tooltip:
enabled: true
followCursor: true
x:
show: false
format: dd MMMM yyyy
'y':
show: true
graph_span: 1month
header:
standard_format: false
show: true
show_states: false
title: ${'Nexxtmove charges for the past month \#'}
now:
show: true
label: Today
series:
- entity: ${sensor.charges}
name: Home
type: column
color: 73C56C
show:
legend_value: false
float_precision: 2
data_generator: |
return entity.attributes.dates.map((day, index) => {
return [new Date(day), entity.attributes.values[index].home];
});
```

</details>

![Latest charges](https://github.com/geertmeersman/nexxtmove/raw/main/images/screenshots/latest_charges.png)

<details><summary>Show markdown code</summary>
Expand Down
122 changes: 119 additions & 3 deletions custom_components/nexxtmove/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import datetime

from dateutil.relativedelta import relativedelta
from requests import Session

from .const import (
Expand Down Expand Up @@ -245,14 +246,15 @@ def fetch_data(self):
extra_attributes=charging_device,
)

end_date = datetime.datetime.now().strftime("%Y%m%d")
extra_attributes = {
"start_date": GRAPH_START_DATE,
"end_date": datetime.datetime.now().strftime("%Y%m%d"),
"end_date": end_date,
}
graph_data = self.charging_device_graph(
charging_device_id,
GRAPH_START_DATE,
datetime.datetime.now().strftime("%Y%m%d"),
end_date,
)

suffix = "period reimbursed"
Expand Down Expand Up @@ -353,6 +355,120 @@ def fetch_data(self):
extra_attributes=extra_attributes
| {"dates": monthly_date, "values": monthly_charges},
)
# Montly charge sessions
start_date = (
datetime.datetime.now()
- relativedelta(months=1)
+ relativedelta(days=1)
).strftime("%Y%m%d")
extra_attributes = {
"start_date": start_date,
"end_date": end_date,
}
graph_data = self.charging_device_graph(
charging_device_id,
start_date,
end_date,
)

suffix = "month reimbursed"
key = format_entity_name(
f"{self.username} charging device {charging_device_id} {suffix}"
)
data[key] = NexxtmoveItem(
name=f"{charging_device.get('name')} {suffix}",
key=key,
type="euro",
sensor_type="sensor",
device_key=device_key,
device_name=device_name,
device_model=device_model,
state=graph_data.get("totals").get("totalReimbursed"),
extra_attributes=extra_attributes,
)
monthly_date = []
monthly_cost = []
monthly_energy = []
monthly_charges = []
period_charges = 0
for record in graph_data.get("records"):
monthly_date.append(record.get("date"))
cost = {}
energy = {}
charges = {}
for category in ["home", "payment", "guest", "work"]:
cost |= {
category: record.get("detailsPerAccount")
.get(category.upper())
.get("cost")
}
energy |= {
category: record.get("detailsPerAccount")
.get(category.upper())
.get("energyWh")
}
charges |= {
category: record.get("detailsPerAccount")
.get(category.upper())
.get("charges")
}
period_charges += (
record.get("detailsPerAccount")
.get(category.upper())
.get("charges")
)
monthly_cost.append(cost)
monthly_energy.append(energy)
monthly_charges.append(charges)

suffix = "month cost"
key = format_entity_name(
f"{self.username} charging device {charging_device_id} {suffix}"
)
data[key] = NexxtmoveItem(
name=f"{charging_device.get('name')} {suffix}",
key=key,
type="euro",
sensor_type="sensor",
device_key=device_key,
device_name=device_name,
device_model=device_model,
state=graph_data.get("totals").get("totalCost"),
extra_attributes=extra_attributes
| {"dates": monthly_date, "values": monthly_cost},
)
suffix = "month energy"
key = format_entity_name(
f"{self.username} charging device {charging_device_id} {suffix}"
)
data[key] = NexxtmoveItem(
name=f"{charging_device.get('name')} {suffix}",
key=key,
type="consumption",
sensor_type="sensor",
device_key=device_key,
device_name=device_name,
device_model=device_model,
state=graph_data.get("totals").get("totalEnergyWh") / 1000,
extra_attributes=extra_attributes
| {"dates": monthly_date, "values": monthly_energy},
)
suffix = "month charges"
key = format_entity_name(
f"{self.username} charging device {charging_device_id} {suffix}"
)
data[key] = NexxtmoveItem(
name=f"{charging_device.get('name')} {suffix}",
key=key,
type="counter",
sensor_type="sensor",
device_key=device_key,
device_name=device_name,
device_model=device_model,
state=period_charges,
extra_attributes=extra_attributes
| {"dates": monthly_date, "values": monthly_charges},
)

"""
tokens = self.charging_device_tokens(charging_device_id)
Expand Down Expand Up @@ -581,7 +697,7 @@ def charge_latest(self):
"""Fetch charges."""
log_debug("[NexxtmoveClient|charge_latest] Fetching charges from Nexxtmove")
response = self.request(
f"{self.environment.api_endpoint}/charge/latest?maxRows=20&offset=0",
f"{self.environment.api_endpoint}/charge/latest?maxRows=200&offset=0",
"[NexxtmoveClient|charge_latest]",
None,
200,
Expand Down
Binary file added images/screenshots/disable-debug-logging.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/screenshots/enable-debug-logging.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fe5c9de

Please sign in to comment.