# ¶ Overview

This page details a temporary work-around to configure a CoFyBox running the glue component version `2bf8dfc` or previous to work together with CoFy Cookies and/or Shelly devices. This version of glue has an issue as its default config sums the energy values of all Shelly energy meters connected to the CoFyBox and publishes this sum to the `utility_meter`. It also registers the utility meter in Home Assistant. This results in data conflicts and unexpected behaviour when a P1 cookie is present, and/or Shelly energy meters that measure individual appliances.

This information is meant for installers, as it requires a balena account with dev rights.

# ¶ 1. Setting up the CoFyBox

Note: this step can be prepared before the on-site installation

1. Set up a CoFyBox following the instructions here. Wait for it to finish updating.
2. Locate the device in the balena fleet.
3. Open a terminal to the `coyfbox-config` service.
4. Input the following commands (followed by enter after each line):
   `cd /cofybox-config`
   `nano glue_keys_blank.json`
5. Copy paste the following config into the editor:

    {
        "version": "20220621",
        "ingredients": {
        },
        "recipes": {
        }
    }

6. Close the editor by pressing `CTRL+X`, confirm with `Y` and enter.
7. Execute the following command:
   `mv -f glue_keys_blank.json glue_keys.json`
8. Close the terminal.

# ¶ 2. Installing the hardware

Note: these instructions must be performed on-site

1. Install the CoFyBox, making sure it has fully booted and the WiFi access point is up.
2. Install the CoFyCookies:

- P1-cookie
- SGR-cookie

3. Install the Shellies

If no Shellies have been installed, you are done here. If Shellies are installed, proceed to the next paragraph.

# ¶ 3. Setting up the glue component

This step is only required if Shelly energy meters have been installed.

1. Open up the Home Assistant instance on the CoFyBox. Navigate to `Settings` > `Devices & Services` > `Devices`. You should see the installed Shelly devices displayed with a uniqe code here, e.g. `Shelly EM 98CDAC1F2B1A`.
2. Identify which Shelly measures which device on which channel (Shellies have 2 or more measurement channels).
3. In another browser tab, open balena. Locate the device in the balena fleet.
4. Open a terminal to the `coyfbox-config` service.
5. Execute the following commands:
   `cd /cofybox-config`
   `nano glue_keys_new.json`
6. Copy paste the following config into the editor, depending on the type of installation:

- If a P1 cookie and a Shelly measuring a heat pump are installed

    {
        "version": "20220621",
        "ingredients": {
            "heatpump_internal_total_consumption": {
                "friendly_name": "Heatpump internal Total energy consumed",
                "topic_in": "shellies/shellyem-98CDAC1F2B1A/emeter/0/total",
                "json_template": null
            },
            "heatpump_internal_total_power": {
                "friendly_name": "Heatpump internal Total power",
                "topic_in": "shellies/shellyem-98CDAC1F2B1A/emeter/0/power",
                "json_template": null
            },
            "heatpump_internal_voltage": {
                "friendly_name": "Heatpump internal Voltage",
                "topic_in": "shellies/shellyem-98CDAC1F2B1A/emeter/0/voltage",
                "json_template": null
            },
            "heatpump_external_total_consumption": {
                "friendly_name": "Heatpump external Total energy consumed",
                "topic_in": "shellies/shellyem-98CDAC1F2B1A/emeter/1/total",
                "json_template": null
            },
            "heatpump_external_total_power": {
                "friendly_name": "Heatpump external Total power",
                "topic_in": "shellies/shellyem-98CDAC1F2B1A/emeter/1/power",
                "json_template": null
            }
        },
        "recipes": {
            "heatpump_total_energy_consumed": {
                "friendly_name": "Heatpump Total energy consumption",
                "topic_out": "data/devices/heatpump/total_energy_consumed",
                "recipe": "{{heatpump_internal_total_consumption + heatpump_external_total_consumption}}",
                "entity": "heatpump",
                "channel": "total_energy_consumed",
                "unit": "Wh",
                "metricKind": "cumulative",
                "metric": "HpElectricityConsumption",
                "ignore_after": 300,
                "ignore": false
            },
            "heatpump_total_power": {
                "friendly_name": "Heatpump Total active power",
                "topic_out": "data/devices/heatpump/total_active_power",
                "recipe": "{{heatpump_internal_total_power + heatpump_external_total_power}}",
                "entity": "heatpump",
                "channel": "total_active_power",
                "unit": "W",
                "metricKind": "gauge",
                "metric": "HpElectricityPower",
                "ignore_after": 300,
                "ignore": false
            },
            "heatpump_voltage_phase_l1": {
                "friendly_name": "Heatpump Voltage phase 1",
                "topic_out": "data/devices/heat_pump/voltage_phase_l1",
                "recipe": "{{heatpump_internal_voltage}}",
                "entity": "heatpump",
                "channel": "voltage_phase_l1",
                "unit": "V",
                "metricKind": "gauge",
                "metric": "HpElectricityVoltage",
                "ignore_after": 300,
                "ignore": false
            }
        }
    }

- If a Shelly acting as utility meter (P1) and a Shelly measuring a heat pump are installed

    {
        "version": "20220621",
        "ingredients": {
            "shellyem_0_total_consumption": {
                "friendly_name": "Total energy consumed",
                "topic_in": "shellies/shellyem-98CDAC1F44D3/emeter/0/total",
                "json_template": null
            },
            "shellyem_0_total_injection": {
                "friendly_name": "Total energy injected",
                "topic_in": "shellies/shellyem-98CDAC1F44D3/emeter/0/total_returned",
                "json_template": null
            },
            "shellyem_0_total_power": {
                "friendly_name": "Total power",
                "topic_in": "shellies/shellyem-98CDAC1F44D3/emeter/0/power",
                "json_template": null
            },
            "shellyem_0_voltage": {
                "friendly_name": "Voltage",
                "topic_in": "shellies/shellyem-98CDAC1F44D3/emeter/0/voltage",
                "json_template": null
            },
            "heatpump_internal_total_consumption": {
                "friendly_name": "Heatpump internal Total energy consumed",
                "topic_in": "shellies/shellyem-C45BBE6030BC/emeter/0/total",
                "json_template": null
            },
            "heatpump_internal_total_power": {
                "friendly_name": "Heatpump internal Total power",
                "topic_in": "shellies/shellyem-C45BBE6030BC/emeter/0/power",
                "json_template": null
            },
            "heatpump_internal_voltage": {
                "friendly_name": "Heatpump internal Voltage",
                "topic_in": "shellies/shellyem-C45BBE6030BC/emeter/0/voltage",
                "json_template": null
            },
            "heatpump_external_total_consumption": {
                "friendly_name": "Heatpump external Total energy consumed",
                "topic_in": "shellies/shellyem-C45BBE6030BC/emeter/1/total",
                "json_template": null
            },
            "heatpump_external_total_power": {
                "friendly_name": "Heatpump external Total power",
                "topic_in": "shellies/shellyem-C45BBE6030BC/emeter/1/power",
                "json_template": null
            }
        },
        "recipes": {
            "total_energy_consumed": {
                "friendly_name": "Total energy consumed",
                "topic_out": "data/devices/utility_meter/total_energy_consumed",
                "recipe": "{{shellyem_0_total_consumption}}",
                "entity": "utility_meter",
                "channel": "total_energy_consumed",
                "unit": "Wh",
                "metricKind": "cumulative",
                "metric": "GridElectricityImport",
                "ignore_after": 300,
                "ignore": false
            },
            "total_energy_injected": {
                "friendly_name": "Total energy injected",
                "topic_out": "data/devices/utility_meter/total_energy_injected",
                "recipe": "{{shellyem_0_total_injection}}",
                "entity": "utility_meter",
                "channel": "total_energy_injected",
                "unit": "Wh",
                "metricKind": "cumulative",
                "metric": "GridElectricityExport",
                "ignore_after": 300,
                "ignore": false
            },
            "total_active_power": {
                "friendly_name": "Total active power",
                "topic_out": "data/devices/utility_meter/total_active_power",
                "recipe": "{{shellyem_0_total_power}}",
                "entity": "utility_meter",
                "channel": "total_active_power",
                "unit": "W",
                "metricKind": "gauge",
                "metric": "GridElectricityPower",
                "ignore_after": 300,
                "ignore": false
            },
            "voltage_phase_l1": {
                "friendly_name": "Voltage phase 1",
                "topic_out": "data/devices/utility_meter/voltage_phase_l1",
                "recipe": "{{shellyem_0_voltage}}",
                "entity": "utility_meter",
                "channel": "voltage_phase_l1",
                "unit": "V",
                "metricKind": "gauge",
                "metric": "GridElectricityVoltage",
                "ignore_after": 300,
                "ignore": false
            },
            "heatpump_total_energy_consumed": {
                "friendly_name": "Heatpump Total energy consumption",
                "topic_out": "data/devices/heatpump/total_energy_consumed",
                "recipe": "{{heatpump_internal_total_consumption + heatpump_external_total_consumption}}",
                "entity": "heatpump",
                "channel": "total_energy_consumed",
                "unit": "Wh",
                "metricKind": "cumulative",
                "metric": "HpElectricityConsumption",
                "ignore_after": 300,
                "ignore": false
            },
            "heatpump_total_power": {
                "friendly_name": "Heatpump Total active power",
                "topic_out": "data/devices/heatpump/total_active_power",
                "recipe": "{{heatpump_internal_total_power + heatpump_external_total_power}}",
                "entity": "heatpump",
                "channel": "total_active_power",
                "unit": "W",
                "metricKind": "gauge",
                "metric": "HpElectricityPower",
                "ignore_after": 300,
                "ignore": false
            },
            "heatpump_voltage_phase_l1": {
                "friendly_name": "Heatpump Voltage phase 1",
                "topic_out": "data/devices/heat_pump/voltage_phase_l1",
                "recipe": "{{heatpump_internal_voltage}}",
                "entity": "heatpump",
                "channel": "voltage_phase_l1",
                "unit": "V",
                "metricKind": "gauge",
                "metric": "HpElectricityVoltage",
                "ignore_after": 300,
                "ignore": false
            }
        }
    }

7. Edit the `topic_in` values under the `ingredients` section of the config file with the unique Shelly IDs and channel nummbers. You can navigate in the editor with the arrow keys.

- Example of the `topic_in` value for `heatpump_internal_total_consumption`:
  shellies/shellyem-98CDAC1F2B1A/emeter/0/total
  This means that the consumption of the indoor heatpump unit is measured by the Shelly device with ID 98CDAC1F2B1A on channel 0.
- Example of the `topic_in` value for `shellyem_0_total_consumption`:
  shellies/shellyem-98CDAC1F44D3/emeter/0/total
  This means that the Shelly device acting as the utility meter has ID 98CDAC1F2B1A and measures the consumption on channel 0.

You need to change the values in bold to the unique IDs and channels of the Shellies installed.

8. When the config file has been updated, close the editor by pressing `CTRL+X`, confirm with `Y` and enter.
9. Execute the following command:
   `mv -f glue_keys_new.json glue_keys.json`
10. Close the terminal.

The installation is now complete.

# ¶ 4. Veryfing the installation

On the CoFyBox app device page, you should see the configured sensors appear (might take a few minutes).
On the Home Assistant instance, you should see the installed P1 cookies appear as MQTT devices.