Skip to content

Carbon Intensity UK Sensor for Home Assistant - Forked from excellent original work by jscruz and changes from jfparis

License

Notifications You must be signed in to change notification settings

alanmcgore/sensor.carbon_intensity_uk

 
 

Repository files navigation

Carbon Intensity UK sensor (forked)

GitHub Release

GitHub Activity License

hacs Project Maintenance

Component to integrate with Carbon Intensity UK.

This component will set up the following platforms.

Platform Description
service Provides sensors with info from Carbon Intensity UK API.

Available sensors are:

Sensor Description
Local CO2 intensity forecast This has attributes for the next 48 hours co2 intensity forecast which can be used for forecasting and optimal window work.
Local Grid CO2 Itensity Current local grid co2 intensity in gCO2eq/kwh
Local Grid CO2 Levels String identifying current CO2 intensity, e.g. 'Low'
Local Grid Fossil Fuel % Percentage of local grid electricity from Gas, Coal, Oil
Local Grid Low Carbon % Percentage of local grid electricity from Hydro, Solar, Wind, Biomass
Local Optimal Window From Time when the next 4h optimal window for low carbon runs from
Local Optimal Window To End time of the 4h optimal window.

The sensor retrieves information using Carbon Intensity UK API library (forked) - It adds in additional data based on work by jfparis in his fork as well as a percentage of the grid in low carbon energy and potentially additional entities.

Installation

  1. Using the tool of choice open the directory (folder) for your HA configuration (where you find configuration.yaml).
  2. If you do not have a custom_components directory (folder) there, you need to create it.
  3. In the custom_components directory (folder) create a new folder called carbon_intensity_uk.
  4. Download all the files from the custom_components/carbon_intensity_uk/ directory (folder) in this repository.
  5. Place the files you downloaded in the new directory (folder) you created.
  6. Restart Home Assistant
  7. In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Carbon Intensity UK"

Using your HA configuration directory (folder) as a starting point you should now also have this:

custom_components/carbon_intensity_uk/.translations/en.json
custom_components/carbon_intensity_uk/__init__.py
custom_components/carbon_intensity_uk/config_flow.py
custom_components/carbon_intensity_uk/const.py
custom_components/carbon_intensity_uk/manifest.json
custom_components/carbon_intensity_uk/sensor.py

Example Dashboard

alt Example Dashboard

title: Home
views:
  - theme: Backend-selected
    path: default_view
    title: Home
    badges: []
    cards:
      - type: vertical-stack
        cards:
          - type: markdown
            content: >-
              {% set t_start = ( 
              as_timestamp(states('sensor.grid_co2_uk_local_optimal_window_from'))
              - as_timestamp(now()))%} 

              {% set t_end = ( 
              as_timestamp(states('sensor.grid_co2_uk_local_optimal_window_to'))
              - as_timestamp(now())) %}


              The best 4 hour window to run energy hungry devices in the next 24
              hours is between {{
              as_timestamp(states('sensor.grid_co2_uk_local_optimal_window_from'))|
              timestamp_custom('%I:%M %p')  }} and {{
              as_timestamp(states('sensor.grid_co2_uk_local_optimal_window_to'))
              | timestamp_custom('%I:%M %p')  }}.

              Starting in {{ t_start | timestamp_custom('%-H hr %-M min' if
              t_start > 3600 else '%-M min', false) }} and finishing {{ t_end |
              timestamp_custom('%-H hr %-M min' if t_end > 3600 else '%-M min',
              false) }} from now.
            title: Energy Usage Guide
          - type: custom:apexcharts-card
            experimental:
              color_threshold: true
            graph_span: 2d
            header:
              show: true
              title: Local Electricity Grid Carbon Intensity and Optimal Windows
            span:
              start: minute
            update_interval: 1min
            series:
              - entity: sensor.grid_co2_uk_local_co2_intensity_forecast_48h
                unit: g/kWh
                name: Carbon Intensity g/kWh
                show:
                  legend_value: false
                data_generator: |
                  return entity.attributes.forecast.map(obj => {
                    return [new Date(obj.from).getTime(), obj.intensity];
                  });
                color_threshold:
                  - value: 25
                    color: darkgreen
                    opacity: 1
                  - value: 95
                    color: green
                  - value: 180
                    color: gold
                  - value: 279
                    color: red
                  - value: 330
                    color: darkred
              - entity: sensor.grid_co2_uk_local_co2_intensity_forecast_48h
                type: area
                unit: g/kWh
                color: green
                name: Optimal Window
                stroke_width: 0
                show:
                  legend_value: false
                data_generator: |
                  return entity.attributes.forecast.map(obj => {
                    return [new Date(obj.from).getTime(), (obj.optimal ? obj.intensity : 0 )];
                  });
          - type: gauge
            entity: sensor.grid_co2_uk_local_grid_low_carbon
            segments:
              - from: 0
                color: '#db4437'
              - from: 35
                color: '#ffa600'
              - from: 65
                color: '#43a047'
          - type: gauge
            entity: sensor.grid_co2_uk_local_grid_fossil_fuel
            needle: false
            severity:
              green: 20
              yellow: 40
              red: 50
      - type: vertical-stack
        cards:
          - type: custom:html-template-card
            title: Energy Intensity Forecast
            ignore_line_breaks: true
            content: >
              <style> table {    width: 100%;   } tr:nth-child(even) {    
              background-color: #222222;   } td, th {     text-align: left;   }
              </style>  <table> <thead> <tr> <th>Period</th> <th>Index</th> 
              <th>Intensity</th> </tr></thead> <tbody>  {% for state in  
              states.sensor.grid_co2_uk_local_co2_intensity_forecast_48h.attributes.forecast
              -%}  <tr {% if state.optimal %} 
              style="background-color:#336600;"{%endif%}>
                <td> {{ as_timestamp(state.from) | timestamp_custom('%I:%M %p') }} - {{ as_timestamp(state.to) | timestamp_custom('%I:%M %p')}}</td>
                <td {% if state.index=='high' or state.index=='very high' %} style="color:#cc3300"{%endif%}
                {% if state.index=='moderate' %} style="color:#ff9900"{%endif%} >  {{state.index}}</td>
                <td {% if state.index=='high' or state.index=='very high' %} style="color:#cc3300"{%endif%}
                {% if state.index=='moderate' %} style="color:#ff9900"{%endif%} >  {{state.intensity}}</td>
              </tr>  {%- endfor %}   </tbody> </table> 
      - show_name: true
        show_icon: true
        type: button
        tap_action:
          action: toggle
        entity: zone.home
        show_state: false
      - type: vertical-stack
        cards:
          - type: history-graph
            entities:
              - entity: sensor.grid_co2_uk_local_grid_co2_levels
              - entity: sensor.grid_co2_uk_local_grid_co2_intensity
              - entity: sensor.grid_co2_uk_local_grid_fossil_fuel
              - entity: sensor.grid_co2_uk_local_grid_low_carbon
            title: Local Grid CO2 Intensity
      - type: logbook
        entities:
          - sensor.grid_co2_uk_local_grid_co2_levels

Configuration is done in the UI

Contributions are welcome!

If you want to contribute to this please read the Contribution guidelines


About

Carbon Intensity UK Sensor for Home Assistant - Forked from excellent original work by jscruz and changes from jfparis

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%