# ¶ Header

This is very incomplete.

## ¶ Connecting the Sonnen battery to CoFyCloud

The documentation page for Sonnen products is here. To integrate a Sonnen battery with CoFyCloud, it's necessary to define its states in Home Assistant, using its web based API. An example sensor.yaml is given here.

Once the states have been defined, we can publish them over MQTT using an automation, for example:

    alias: Sonnen MQTT broadcast
    description: Takes Sonnen data from HA states and publishes it on MQTT.
    mode: single
    trigger:
      - platform: time_pattern
        hours: '*'
        minutes: /1
    action:
      - service: mqtt.publish
        data_template:
          topic: cb/battery/apparent_output
          payload: '{{states(''sensor.sonnenapiv2_apparent_output'')}}'
      - service: mqtt.publish
        data_template:
          topic: cb/battery/battery_voltage
          payload: '{{states(''sensor.sonnenapiv2_ubat'')}}'
      - service: mqtt.publish
        data_template:
          topic: cb/battery/rsoc
          payload: '{{states(''sensor.sonnenapiv2_rsoc'')}}'
      - service: mqtt.publish
        data_template:
          topic: cb/battery/consumption
          payload: '{{states(''sensor.sonnenapiv2_consumption_w'')}}'

Finally the necessary additions can be made to the glue keys, for example:

            "battery_apparent_output": {
                "friendly_name": "Battery discharge power",
                "topic_in": "cb/battery/apparent_output",
                "json_template": null
            },
            "battery_battery_voltage": {
                "friendly_name": "Battery internal voltage",
                "topic_in": "cb/battery/battery_voltage",
                "json_template": null
            },
            "battery_rsoc": {
                "friendly_name": "Battery relative state of charge",
                "topic_in": "cb/battery/rsoc",
                "json_template": null
            },
            "battery_consumption": {
                "friendly_name": "Battery power to loads",
                "topic_in": "cb/battery/consumption",
                "json_template": null
            }

            "battery_apparent_output": {
                "friendly_name": "Battery discharge power",
                "topic_out": "data/devices/battery/battery_apparent_output",
                "recipe": "{{battery_apparent_output}}",
                "entity": "battery",
                "channel": "apparent_output",
                "unit": "W",
                "metricKind": "gauge",
                "metric": "BatteryElectricityPower",
                "ignore_after": 300,
                "ignore": false
            },
            "battery_battery_voltage": {
                "friendly_name": "Battery internal voltage",
                "topic_out": "data/devices/battery/battery_battery_voltage",
                "recipe": "{{battery_battery_voltage}}",
                "entity": "battery",
                "channel": "battery_voltage",
                "unit": "V",
                "metricKind": "gauge",
                "metric": "BatteryElectricityVoltage",
                "ignore_after": 300,
                "ignore": false
            },
            "battery_rsoc": {
                "friendly_name": "Battery relative state of charge",
                "topic_out": "data/devices/battery/battery_rsoc",
                "recipe": "{{battery_rsoc}}",
                "entity": "battery",
                "channel": "rsoc",
                "unit": "%",
                "metricKind": "gauge",
                "metric": "BatteryElectricitySoc",
                "ignore_after": 300,
                "ignore": false
            },
            "battery_consumption": {
                "friendly_name": "Battery power to loads",
                "topic_out": "data/devices/battery/battery_consumption",
                "recipe": "{{battery_consumption}}",
                "entity": "battery",
                "channel": "consumption",
                "unit": "W",
                "metricKind": "gauge",
                "metric": "BatteryElectricityPower",
                "ignore_after": 300,
                "ignore": false
            }