# Home Assistant

It is possible to integrate energy data from your **batterX h10** system into your **Home Assistant** by simply adding the following code snippet to your [configuration.yaml](https://www.home-assistant.io/docs/configuration/) file and following the steps below.

```yaml
rest:
  resource: http://LIVEX_IP_ADDRESS/api.php?get=currentstate
  method: GET
  sensor:
    - name: bx_battery_soc
      device_class: battery
      state_class: measurement
      unit_of_measurement: "%"
      value_template: "{{ value_json['1074']['1'] }}"
      force_update: true
    - name: bx_power_from_battery
      device_class: power
      state_class: measurement
      unit_of_measurement: "W"
      value_template: "{{ max([-1 * value_json['1121']['1'], 0]) }}"
      force_update: true
    - name: bx_power_to_battery
      device_class: power
      state_class: measurement
      unit_of_measurement: "W"
      value_template: "{{ max([value_json['1121']['1'], 0]) }}"
      force_update: true
    - name: bx_power_from_grid
      device_class: power
      state_class: measurement
      unit_of_measurement: "W"
      value_template: "{{ max([value_json['2913']['0'], 0]) }}"
      force_update: true
    - name: bx_power_to_grid
      device_class: power
      state_class: measurement
      unit_of_measurement: "W"
      value_template: "{{ max([-1 * value_json['2913']['0'], 0]) }}"
      force_update: true
    - name: bx_power_from_solar
      device_class: power
      state_class: measurement
      unit_of_measurement: "W"
      value_template: "{{ value_json['1634']['0'] }}"
      force_update: true
    - name: bx_power_from_external_solar
      device_class: power
      state_class: measurement
      unit_of_measurement: "W"
      value_template: "{{ value_json['2913']['3'] }}"
      force_update: true

sensor:
  - platform: integration
    source: sensor.bx_power_from_battery
    name: bx_energy_from_battery
    unit_prefix: k
    round: 2
  - platform: integration
    source: sensor.bx_power_to_battery
    name: bx_energy_to_battery
    unit_prefix: k
    round: 2
  - platform: integration
    source: sensor.bx_power_from_grid
    name: bx_energy_from_grid
    unit_prefix: k
    round: 2
  - platform: integration
    source: sensor.bx_power_to_grid
    name: bx_energy_to_grid
    unit_prefix: k
    round: 2
  - platform: integration
    source: sensor.bx_power_from_solar
    name: bx_energy_from_solar
    unit_prefix: k
    round: 2
  - platform: integration
    source: sensor.bx_power_from_external_solar
    name: bx_energy_from_external_solar
    unit_prefix: k
    round: 2
```

*The provided code utilizes Home Assistant's* [*RESTful*](https://www.home-assistant.io/integrations/rest/) *integration to fetch data (CurrentState) from the local batterX API. Subsequently, it employs the* [*Riemann sum integral*](https://www.home-assistant.io/integrations/integration/) *to transform power data (W) into energy data (Wh), suitable for visualization on the Home Assistant Energy Dashboard.*

{% hint style="info" %}
Make sure to change the `LIVEX_IP_ADDRESS` to the actual IP address of your liveX.
{% endhint %}

The REST integration adds the following sensors to your Home Assistant:

* `bx_battery_soc` - Battery level, state of charge (Unit: `%`)
* `bx_power_from_battery` - Battery discharging power (Unit: `W`)
* `bx_power_to_battery` - Battery charging power (Unit: `W`)
* `bx_power_from_grid` - Power imported from the grid (Unit: `W`)
* `bx_power_to_grid` - Power exported to the grid (Unit: `W`)
* `bx_power_from_solar` - Solar power of the batterX h10 (Unit: `W`)
* `bx_power_from_external_solar` - Solar power of the external solar devices (Unit: `W`)

{% hint style="info" %}
The `bx_power_from_external_solar` sensor is optional, and if you do not have an external solar energy meter installed you can simply remove this sensor from the code.
{% endhint %}

Subsequently, the Riemann sum integral integration adds the following energy sensors, that can be easily added to the Home Assistant Energy Dashboard:

* `bx_energy_from_battery` - Energy discharged from the battery
* `bx_energy_to_battery` - Energy charged to the battery
* `bx_energy_from_grid` - Energy imported from the grid
* `bx_energy_to_grid` - Energy exported to the grid
* `bx_energy_from_solar` - Solar energy produced by the batterX h10 inverter
* `bx_energy_from_external_solar` - Solar energy produced by the external solar devices

{% hint style="info" %}
Same as above, `bx_energy_from_external_solar` is optional, and if you do not have an external solar energy meter installed you can simply remove this sensor from the code.
{% endhint %}

Once you've inserted the code into your **configuration.yaml** file and restarted your **Home Assistant**, you're ready to set up the Home Assistant Energy Dashboard.

Simply navigate to the **Energy Configuration** menu within **Home Assistant** and enter the following sensors in their respective locations:

* Electricity grid
  * Grid consumption
    * Consumed energy: `bx_energy_from_grid`
  * Return to grid
    * Energy returned to the grid: `bx_energy_to_grid`
* Solar panels
  * Solar production
    * Solar production energy: `bx_energy_from_solar`
    * Solar production energy (optional): `bx_energy_from_external_solar`
* Home battery storage
  * Battery systems
    * Energy going in to the battery: `bx_energy_to_battery`
    * Energy coming out of the battery: `bx_energy_from_battery`

<figure><img src="https://4226182563-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MRxxZZhDcEU_RUZTN6J%2Fuploads%2FyldZFqCnebaZv2Uc4n2A%2Fbx_home_assistant_energy_configuration.png?alt=media&#x26;token=95f7d8fe-b3f7-439a-808a-672a42d7fc49" alt=""><figcaption><p>Home Assistant Energy Configuration</p></figcaption></figure>
