markgdev / home-assistant_OctopusAgile

Octopus Agile custom component for Home Assistant
Apache License 2.0
86 stars 33 forks source link

Add statistics support for new long-term energy tracking #83

Open Floppy opened 2 years ago

Floppy commented 2 years ago

The new HomeAssistant Energy tracking looks like it might work really well with this integration. It would need to expose statistics that can be tracked long-term, as described at https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics.

The energy tracking config looks like it will work with the agile_current_price for tracking cost fine, but we need a current usage statistic to point at.

Screenshot 2021-08-05 at 12 07 06

I guess this is maybe related to grabbing the energy usage data from the API as well, which I'm not sure the integration does yet?

markgdev commented 2 years ago

I don’t believe the API will show current usage, you’d need something else to track it. Unless home assistant can use historical data?

I need to update my home assistant instance, this looks awesome!

Floppy commented 2 years ago

Sorry, that was unclear, I don't think I mean current usage, I mean the half-hourly consumption data which comes from https://developer.octopus.energy/docs/api/#consumption. I think HA Energy can use the historical stuff, from my reading of it so far, so that would I think be what we'd want a statistic for?

Floppy commented 2 years ago

Bit more info at https://developers.home-assistant.io/blog/2021/05/25/sensor_attributes/ too.

ganey commented 2 years ago

+1 for this feature

GrandadEvans commented 2 years ago

I know I'm not adding any valuable content to the issue here, but I too believe that it would be great if the half hour usage stats from the API would be able to be integrated into the new HA feature. Obviously, the current price would be a valuable addition alongside this.

Keep up the good work

B-Hartley commented 2 years ago

could you just feed the "yearly usage" or "monthly usage" into a utility meter sensor. Then feed that into the energy dashboard ?

HACS-bank commented 2 years ago

I upgraded to get the new Energy dashboard, and found that energy cost is expected to be in GBP whereas you are using Pence, so I raised a feature request for the Energy dashboard to allow for the conversion, as that seems more general.

If you have live data then you can define a sensor to collect the energy consumption each half hour, based on live data, and then use that to drive the new dashboard. You do this in configuration.yaml as follows

sensor:
- platform: integration
  source: sensor.grid_export
  name: energy_exported
  unit_prefix: k
  round: 2
- platform: integration
  source: sensor.grid_import
  name: energy_imported
  unit_prefix: k
  round: 2
- platform: integration
  source: sensor.fronius_pv_generation
  name: energy_generated
  unit_prefix: k

image

B-Hartley commented 2 years ago

I created a template sensor to change it into £. Would be nice if it existed already. Or even better if the conversion was handled by the dashboard.

townsmcp commented 2 years ago

@B-Hartley any chance you can pop you template sensor code on here?

B-Hartley commented 2 years ago

Hi,

This is a simple template to change the p to £.

template:
  sensor:
    - name: "Octopus Current Price Pounds"
      availability: "{{ states('sensor.import_price')|is_number }}"
      unique_id: octopus_current_price_pounds
      unit_of_measurement: '£'
      state: "{{ states('sensor.import_price')|float(0) / 100.0 }}"

I use this, it's a bit complex, but it handles Power Hour, Agile and Go.

rest:
  resource_template: "https://api.octopus.energy/v1/products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-L/standard-unit-rates/?period_from={{ now().year }}-{{ now().month }}-{{ now().day }}T{{ now().hour }}:{% if now().minute <30 %}00{% else %}30{% endif %}&period_to={{ now().year }}-{{ now().month }}-{% if (now().hour == 23) and (now().minute >=30) %}{{ now().day+1 }}{% else %}{{ now().day }}{% endif %}T{% if (now().hour == 23) and (now().minute >=30) %}00{% elif now().minute >=30 %}{{ now().hour+1}}{% else %}{{ now().hour}}{% endif %}:{% if now().minute >=30 %}00{% else %}30{% endif %}"
  scan_interval: 1800    
  sensor:
    - name: "Import Price"
      value_template: "{% if (now()|as_timestamp >= states('input_datetime.octopus_power_hour')|as_timestamp and now()|as_timestamp < states('input_datetime.octopus_power_hour')|as_timestamp+3600) %}0.0{% elif is_state('input_boolean.go_tariff','on') %}{{ 5.0 if is_state('input_boolean.go_cheap','on') else 16.26}}{% else %}{{ value_json.results[0].value_inc_vat|round(2) }}{% endif %}"
      unit_of_measurement: "p/kWh"

    - name: "Import Price Pounds"
      value_template: "{% if (now()|as_timestamp >= states('input_datetime.octopus_power_hour')|as_timestamp and now()|as_timestamp < states('input_datetime.octopus_power_hour')|as_timestamp+3600) %}0.0{% elif is_state('input_boolean.go_tariff','on') %}{{ 0.05 if is_state('input_boolean.go_cheap','on') else 0.1626}}{% else %}{{ value_json.results[0].value_inc_vat/100.0 }}{% endif %}"
      unit_of_measurement: "£/kWh"     

I've used this one historically for a display to 1.d.p.

template:
  sensor:
    - name: "Octopus Current Price Rounded"
      unique_id: octopus_current_price_rounded
      availability: "{{ states('sensor.import_price')|is_number }}"
      unit_of_measurement: 'p/kWh'
      state: "{{ states('sensor.import_price')|round(1,'',0) }}"

If you want to see more, my code is on github

townsmcp commented 2 years ago

Sorry to everyone as this is a bit off topic! @B-Hartley Im loving your config! You have so many similar devices/integrations etc that I have, including bits I haven't even thought of adding to HA (eg SkyQ just to name one). I tried modelling my yaml files following Frencks setup and it has become a bit unwieldy - when trying to find files for a certain integration/device it has become bit hard to track down whereas your config just makes pure sense. Quick question for you though, as Im a bit confused, in your configuration.yaml, line 150 states template: !include_dir_list templates - do you have another folder in the config root for that? From looking through your setup, it appears that you have templates in the appropriate directory for the packages integration. Is that line just a catch all in case you create a template that doesnt have its own appropriate package? Thanks in advance, sorry to scrutinize but im loving the way you have it all laid out and seriously considering doing a full reconfig

B-Hartley commented 2 years ago

Thanks for the comments. When the new "modern template" type came out you couldn't use it across packages. So I was about to update them and split them into a new folder. Then it was fixed so they worked correctly in packages so I didn't need to. Suspect that line can be removed. I'll take a look.

aircon14 commented 2 years ago

@Floppy,

Sorry, that was unclear, I don't think I mean current usage, I mean the half-hourly consumption data which comes from https://developer.octopus.energy/docs/api/#consumption. I think HA Energy can use the historical stuff, from my reading of it so far, so that would I think be what we'd want a statistic for?

I know I’m a bit late to the game, but I’ve only recently started to look at this in anger. It looks like the basis of pulling in the consumption data is already there in the code of this integration - it’d just needs exposing as a sensor and the long term stats aspects added.

However, from my attempts to pull consumption data from the Octopus API, it seems to be updated every 24 hours with 0.5 hour data for the previous day (midnight-midnight) - this isn’t quite what’s needed here. It doesn’t look like Home Assistant has a way of inserting historic data tagged with the correct date/time so I’m not sure this is possible?

I’d love to be wrong as I was really hoping to pull the “live” half-hourly data into Home Assistant :). (I’d settle for the previous day’s data correctly brought in - but I’d much prefer “live” data!)

@markgdev - Is this what you meant by “doesn’t show current usage”? I was wondering if the new long term stats attribute of “last_reset” combined with “state_class = total” could be used to pull in the last 24 hours of data. My Python and JSON handling isn’t up to that yet but I wondered if you’d tried it?