Sometimes sensor data is only available in Home Assistant, or can't be transformed by the glue logic into the appropriate MQTT message to be picked up by the COFYY-cloud component. This page will show you how to set up automations in Home Assistant to push data to the COFY-cloud.

# ¶ Setting up the automation

## ¶ 1. Finding the sensor IDs

You first need to find the entity IDs of the sensors/states which you want to forward (these will be our 'ingredients'). There are several ways to find these.

1. Go to `Settings`> `Devices and services`> `Devices`. Open the device and click on the sensor. A pop-up should appear displaying the entity ID, e.g. `sensor.shelly_em_98cdac1f2b1a_meter_energy_1`.
2. Alternatively, go to `Developer tools`> `States`. Locate the required entity ID(s) in this list.

## ¶ 2. Writing the automation

1. Go to `Settings`> `Automation & Scenes`. In the tab `Automations`, click on the `Create automation` button. In the pop-up window, select `Start with an empty automation`.
2. In the top right corner of the automation editor, click on the three vertical dots and select `Edit in YAML mode`. We can now write our 'recipe'.

A basic recipe, which takes the energy (kWh) value of a Shelly, encapsulates it in the COFY payload format and sends it to the appropriate MQTT topic to be picked up by the COFY cloud component looks like this:

    alias: Shelly-to-utility-meter energy
    description: ''
    trigger:
      - platform: state
        entity_id:
          - sensor.shelly_em_98cdac1f2b1a_meter_energy_1
    condition: []
    action:
      - service: mqtt.publish
        data:
          topic: data/devices/utility_meter/total_energy_consumed
          payload: >
            {"entity":"utility_meter",
            "metric":"GridElectricityImport", "metricKind":"cumulative",
            "unit":"kWh", "friendly_name":"Utility meter total energy consumed",
            "value": {{ states.sensor.shelly_em_98cdac1f2b1a_meter_energy_1.state | float | string }},
            "timestamp": {{ now().timestamp() | int | string }},
            "sensorId":"utility_meter.total_energy_consumed"}

This recipe is triggered by a 'state change' (new value) of the `sensor.shelly_em_98cdac1f2b1a_meter_energy_1` entity. It then calls the `mqtt.publish` service and sends the `payload` to the configured `topic`.

3. After you have written the automation, click on the `Save` button and return to the automation overview by clicking the arrow in the top left corner. You should see your new automation appearing there. Check if the automation is enabled (slider to the right).

# ¶ Testing automations

You can test your automation recipe before writing the actual automation.

1. Go to `Developer tools`> `Services`. Select the `MQTT: Publish` service from the drop-down list.
2. Below the automation editor, click `Go to YAML mode`.
3. To test the recipe from the example above, paste the following code:

    service: mqtt.publish
    data:
      topic: test
      payload: >
            {"entity":"utility_meter",
            "metric":"GridElectricityImport", "metricKind":"cumulative",
            "unit":"kWh", "friendly_name":"Utility meter total energy consumed",
            "value": {{ states.sensor.shelly_em_98cdac1f2b1a_meter_energy_1.state | float | string }},
            "timestamp": {{ now().timestamp() | int | string }},
            "sensorId":"utility_meter.total_energy_consumed"}

4. In another tab, go to the device on balena, open a shell to the mosquitto container and execute the following command: `mosquitto_sub -v -t test`.
5. If you execute the recipe by clicking on `Call service`, you should see the MQTT message appearing in the shell. If not, you should see an error message in Home Assistant, allowing you to debug your recipe.

# ¶ Automation templating

Automation recipes can be templated, allowing mathematical operations on the sensor values, combining or substracting values, ...

Example of a recipe combining two energy values into one

    alias: Shelly-to-utility-meter energy
    description: ''
    trigger:
      - platform: state
        entity_id:
          - sensor.shelly_em_98cdac1f2b1a_meter_energy_1
          - sensor.shelly_em_98cdac1f2b1a_meter_energy_2
    condition: []
    action:
      - service: mqtt.publish
        data:
          topic: data/devices/utility_meter/total_energy_consumed
          payload: >
            {"entity":"utility_meter",
            "metric":"GridElectricityImport", "metricKind":"cumulative",
            "unit":"kWh", "friendly_name":"Utility meter total energy consumed",
            "value": {{ (states.sensor.shelly_em_98cdac1f2b1a_meter_energy_1.state | float + states.sensor.shelly_em_98cdac1f2b1a_meter_energy_2.state | float) | string }},
            "timestamp": {{ now().timestamp() | int | string }},
            "sensorId":"utility_meter.total_energy_consumed"}

Example of a recipe converting Wmin to kWh (and rounding it to 2 decimal places)

    alias: Shelly-to-utility-meter energy conversion
    description: ''
    trigger:
      - platform: state
        entity_id:
          - sensor.shelly_em_98cdac1f2b1a_meter_energy_1
    condition: []
    action:
      - service: mqtt.publish
        data:
          topic: data/devices/utility_meter/total_energy_consumed
          payload: >
            {"entity":"utility_meter",
            "metric":"GridElectricityImport", "metricKind":"cumulative",
            "unit":"kWh", "friendly_name":"Utility meter total energy consumed",
            "value": {{ ((states.sensor.shelly_em_98cdac1f2b1a_meter_energy_1.state | float) / 60000) | round(2) | string }},
            "timestamp": {{ now().timestamp() | int | string }},
            "sensorId":"utility_meter.total_energy_consumed"}

Example of a recipe taking a sensor indicating the active power from a PV inverter and forwarding it to the COFY-cloud.

    alias: "MQTT: push solar active power"
    description: ""
    trigger:
      - platform: state
        entity_id:
          - sensor.solar_active_power
    condition: []
    action:
      - service: mqtt.publish
        data:
          topic: data/devices/solar/total_active_power
          payload: >
            {"entity":"solar_inverter",
            "metric":"PvElectricityPower", "metricKind":"gauge", "unit":"W",
            "friendly_name":"Solar active power injection", "value": {{
             states.sensor.solar_active_power.state | float | string }},
            "sensorId":"solar.total_active_power"}
    mode: single

# ¶ Payload formats

The following are the standard COFY topics and payload formats for the `utility_meter` device.
Total energy consumed

    action:
      - service: mqtt.publish
        data:
          topic: data/devices/utility_meter/total_energy_consumed
          payload: >
            {"entity":"utility_meter",
            "metric":"GridElectricityImport", "metricKind":"cumulative",
            "unit":"kWh", "friendly_name":"Utility meter total energy consumed",
            "value": {{ states.sensor.shelly_em_98cdac1f2b1a_meter_energy_0.state | float | string }},
            "timestamp": {{ now().timestamp() | int | string }},
            "sensorId":"utility_meter.total_energy_consumed"}

Total energy injected

    action:
      - service: mqtt.publish
        data:
          topic: data/devices/utility_meter/total_energy_injected
          payload: >
            {"entity":"utility_meter",
            "metric":"GridElectricityExport", "metricKind":"cumulative",
            "unit":"kWh", "friendly_name":"Utility meter total energy injected",
            "value": {{ states.sensor.shelly_em_98cdac1f2b1a_meter_energy_1.state | float | string }},
            "timestamp": {{ now().timestamp() | int | string }},
            "sensorId":"utility_meter.total_energy_injected"}

Voltage phase 1

    action:
      - service: mqtt.publish
        data:
          topic: data/devices/utility_meter/voltage_phase_1
          payload: >
            {"entity":"utility_meter",
            "metric":"GridElectricityVoltage", "metricKind":"gauge",
            "unit":"V", "friendly_name":"Utility meter voltage phase 1",
            "value": {{ states.sensor.shelly_em_98cdac1f2b1a_meter_voltage_0.state | float | string }},
            "timestamp": {{ now().timestamp() | int | string }},
            "sensorId":"utility_meter.voltage_phase_1"}

The following are the standard COFY topics and payload formats for the `solar_inverter` device.
Total energy

    action:
      - service: mqtt.publish
        data:
          topic: data/devices/solar/total_energy_injected
          payload: >
            {"entity":"solar_inverter",
            "metric":"PvElectricityProduction", "metricKind":"cumulative",
            "unit":"kWh", "friendly_name":"Solar total energy injected", "value":
            {{ states.sensor.solar_total_energy_injected.state | float | string }},
            "sensorId":"solar.total_energy_injected"}
    mode: single

Total active power

    action:
      - service: mqtt.publish
        data:
          topic: data/devices/solar/total_active_power
          payload: >
            {"entity":"solar_inverter",
            "metric":"PvElectricityPower", "metricKind":"gauge", "unit":"W",
            "friendly_name":"Solar active power injection", "value": {{
             states.sensor.solar_active_power.state | float | string }},
            "sensorId":"solar.total_active_power"}
    mode: single