# Synchronize Buildings and Devices

##

## Synchronize Buildings

Make sure all buildings, which shall be taken into consideration by the MOOST Recommender Platform, are synchronized to MOOST. I.e. new buildings shall be created, and changed building states shall be updated (incl. deactivation and reactivation) via MOOST API.

### Building Data Structure

Following example depicts a data structure of a building located in Dresden, with a couple of connected devices, with dual tariff.

```json
{
  "id": "66d07a7fd51e3528aa5673ba",
  "customerId": "656730be2bc9719e6e5ed51d",
  "customerBuildingId": "99900000C048ADA6",
  "zip": "01067",
  "city": "Dresden",
  "countryCode": "DE",
  "timeZoneId": "Europe/Berlin",
  "geolocation": {
    "lat": 47.2383918762207,
    "lon": 8.284913063049316
  },
  "registrationTimestamp": 1645630735,
  "activationTimestamp": 1645640000,
  "isEarlyAdopter": false,
  "userIds": [
    "user2547865"
  ],
  "disabledNotifications": [
    {
      "ruleId": "63fb7ac54978916150dc5572",
      "userId": "user2547865"
    }
  ],
  "inactiveRules": [],
  "devices": [
    {
      "id": "656830efc4515778b1ce4f2f",
      "type": "CAR",
      "name": "John's Car",
      "product_name": "Device Link Car",
      "createdAt": 1701327087,
      "updatedAt": 1724896211,
    },
    {
      "id": "621660e410c1012648d7bed2",
      "type": "CAR_CHARGER",
      "name": "easee Home",
      "product_name": "easee Home",
      "createdAt": 1645633761,
      "updatedAt": 1724936865
    }
  ],
  "settings": {
    "tariff": {
      "type": "DUAL",
      "minPrice": "0.296",
      "maxPrice": "0.385",
      "currency": "EUR",
      "dual": {
        "lowTariff": {
          "mondayStartTime": "19:00",
          "mondayEndTime": "07:00",
          "tuesdayStartTime": "19:00",
          "tuesdayEndTime": "07:00",
          "wednesdayStartTime": "19:00",
          "wednesdayEndTime": "07:00",
          "thursdayStartTime": "19:00",
          "thursdayEndTime": "07:00",
          "fridayStartTime": "19:00",
          "fridayEndTime": "07:00",
          "saturdayStartTime": "00:00",
          "saturdayEndTime": "07:00",
          "sundayStartTime": "00:00",
          "sundayEndTime": "07:00"
        }
      }
    }
  },
  "properties": {}
}
```

#### Data Structure Attributes

<table><thead><tr><th width="271">Attribute Name</th><th>Description</th></tr></thead><tbody><tr><td><code>id</code></td><td>The building ID on MOOST side (is generated when inserting a new building)</td></tr><tr><td><code>customerId</code></td><td>The ID of the customer (cannot be set or changed)</td></tr><tr><td><code>customerBuildingId</code></td><td>The building ID on customer side</td></tr><tr><td><code>zip</code></td><td>Postal code</td></tr><tr><td><code>city</code></td><td>Name of the city</td></tr><tr><td><code>countryCode</code></td><td>Country Code, <a href="https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2</a></td></tr><tr><td><code>timeZoneId</code></td><td>The time zone id of the building, <a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones">https://en.wikipedia.org/wiki/List_of_tz_database_time_zones</a><br><br>This is automatically added by MOOST if not provided (and if provided, it would be verified).</td></tr><tr><td><code>geolocation</code></td><td><p>The geo location of the building. </p><p><a href="https://en.wikipedia.org/wiki/ISO_6709">https://en.wikipedia.org/wiki/ISO_6709</a></p><p></p><p>This is automatically added by MOOST if not provided.</p></td></tr><tr><td><code>registrationTimestamp</code></td><td>Time (Epoch Time in seconds (UTC)) when the building was registered on customer side. </td></tr><tr><td><code>activationTimestamp</code></td><td>Time (Epoch Time in seconds (UTC)) when the building was added to MOOST Recommender Platform.<br>This time is automatically added by MOOST when inserting a new building.</td></tr><tr><td><code>deactivatedTimestamp</code></td><td>If building has been deregistered, and shall no longer receive notifications, then this attribute is set, and contains Epoch Time in seconds (UTC)</td></tr><tr><td><code>isEarlyAdopter</code></td><td>If building is marked as early adopters, then he/she also receives notifications of rules marked for "Early Adopter" (these are typically new designed rules which shall be tested on a small user (aka building) base.</td></tr><tr><td><code>devices</code></td><td><p>List of devices. Each device mainly contains following attributes </p><ul><li><code>id</code>,</li><li><code>type</code> (see <a data-mention href="synchronize-buildings-and-devices/device-types">device-types</a>)</li><li><code>product_name</code></li></ul><p></p></td></tr><tr><td><code>userIds</code></td><td><p>A list of users may be attached to a building.<br></p><p>If present, push notifications can be can be disabled for individual users instead of for the entire building.</p></td></tr><tr><td><code>disabledNotifications</code></td><td>If <code>userIds</code> are attached to the building, this attribute may contain a list of disabled rules for a set us users, </td></tr><tr><td><code>inactiveRules</code></td><td>This attribute contains a list of disabled rules for this building.</td></tr><tr><td><code>settings.tariff</code></td><td>You may add tariff data of a building. If dual or dynamic tariff settings are defined, then MOOST is able to generate additional tariff related events. The tariff sub-structures are explained in table cells below.<br>Remark: attributes "currency", "minPrice" and "maxPrice" are optional, and are "<em>per kWh</em>" based.</td></tr><tr><td></td><td><p>Single tariff settings</p><pre class="language-json"><code class="lang-json">"settings": {
    "tariff": {
      "type": "SINGLE",
      "minPrice": "34.95",
      "maxPrice": "34.95",
      "currency": "CHF"
    }
  }
</code></pre></td></tr><tr><td></td><td><p>Dual tariff settings: defines the low-tariff time for a whole week.</p><pre class="language-json"><code class="lang-json">"settings": {
    "tariff": {
      "type": "DUAL",
      "minPrice": "0.296",
      "maxPrice": "0.385",
      "currency": "EUR",
      "dual": {
        "lowTariff": {
          "mondayStartTime": "19:00",
          "mondayEndTime": "07:00",
          "tuesdayStartTime": "19:00",
          "tuesdayEndTime": "07:00",
          "wednesdayStartTime": "19:00",
          "wednesdayEndTime": "07:00",
          "thursdayStartTime": "19:00",
          "thursdayEndTime": "07:00",
          "fridayStartTime": "19:00",
          "fridayEndTime": "07:00",
          "saturdayStartTime": "00:00",
          "saturdayEndTime": "07:00",
          "sundayStartTime": "00:00",
          "sundayEndTime": "07:00"
        }
      }
</code></pre></td></tr><tr><td></td><td><p></p><p>Dynamic tariff settings: defines the dynamic tariff slices for the next couple of hours (typically for 1 - 2 days)</p><pre class="language-json"><code class="lang-json">"settings": {
  "tariff": {
    "type": "DYNAMIC",
    "minPrice": "0.1231",
    "maxPrice": "0.3334",
    "currency": "EUR",
    "dynamic": {
      "tariffs": [
        {
          "price": "0.1546",
          "from": 1732212000,
          "to": 1732212900
        },
        {
          "price": "0.1546",
          "from": 1732212900,
          "to": 1732213800
        },
        {
          "price": "0.1546",
          "from": 1732213800,
          "to": 1732214700
        },
        ...
      ]
    }
  }
}
</code></pre></td></tr></tbody></table>

Remark: when reading building data, there might be additional attributes set, such as `profile` or `geolocation`. These fields are calculated by MOOST and is not to be modified by the customer.

### Load Buildings

{% openapi src="<https://doc.api.moost.io/docs/openapi/openapi.json>" path="/buildings/v1" method="get" %}
<https://doc.api.moost.io/docs/openapi/openapi.json>
{% endopenapi %}

#### Example with CURL

```sh
# make sure ACCESS_TOKEN is set and valid (see "Request Access Token")
curl -X GET "https://api.moost.io/buildings/v1" \
     -H "Authorization: Bearer $ACCESS_TOKEN"
```

### Load a Building

{% openapi src="<https://doc.api.moost.io/docs/openapi/openapi.json>" path="/buildings/{customerBuildingId}/v1" method="get" %}
<https://doc.api.moost.io/docs/openapi/openapi.json>
{% endopenapi %}

#### Example with CURL

```sh
curl -X GET "https://api.moost.io/buildings/999000009C54AEDC/v1" \
     -H "Authorization: Bearer $ACCESS_TOKEN"
```

### Add a Building

{% openapi src="<https://doc.api.moost.io/docs/openapi/openapi.json>" path="/buildings/v1" method="post" %}
<https://doc.api.moost.io/docs/openapi/openapi.json>
{% endopenapi %}

#### Example with CURL

```sh
curl -X POST "https://api.moost.io/buildings/v1" \
     -H  "Authorization: Bearer $ACCESS_TOKEN" \
     -H  "Content-Type: application/json" \
     -d "{\"customerBuildingId\":\"999000009C54AAAB\",\"customerId\":\"656730be2bc9719e6e5ed51d\",\"zip\":\"8010\",\"city\":\"Graz\",\"countryCode\":\"AT\",\"devices\":[],\"settings\":{\"lowTariffHourSetting\":{\"mondayStartTime\":\"20:00\",\"mondayEndTime\":\"07:00\",\"tuesdayStartTime\":\"20:00\",\"tuesdayEndTime\":\"07:00\",\"wednesdayStartTime\":\"20:00\",\"wednesdayEndTime\":\"07:00\",\"thursdayStartTime\":\"20:00\",\"thursdayEndTime\":\"07:00\",\"fridayStartTime\":\"20:00\",\"fridayEndTime\":\"07:00\",\"saturdayStartTime\":\"13:00\",\"saturdayEndTime\":\"07:00\",\"sundayStartTime\":\"00:00\",\"sundayEndTime\":\"07:00\"}},\"registrationTimestamp\":1549971011,\"customerSubscriptionName\":\"Premium\"}"
```

### Adjust a Building

{% openapi src="<https://doc.api.moost.io/docs/openapi/openapi.json>" path="/buildings/v1" method="put" %}
<https://doc.api.moost.io/docs/openapi/openapi.json>
{% endopenapi %}

#### Example with CURL

```sh
curl -X PUT "https://api.moost.io/buildings/v1" \
     -H  "Authorization: Bearer $ACCESS_TOKEN" \
     -H  "Content-Type: application/json" \
     -d "{\"customerBuildingId\":\"999000009C54AAAA\",\"customerId\":\"656730be2bc9719e6e5ed51d\",\"zip\":\"8020\",\"city\":\"Graz\",\"countryCode\":\"AT\",\"devices\":[],\"settings\":{\"lowTariffHourSetting\":{\"mondayStartTime\":\"20:00\",\"mondayEndTime\":\"07:00\",\"tuesdayStartTime\":\"20:00\",\"tuesdayEndTime\":\"07:00\",\"wednesdayStartTime\":\"20:00\",\"wednesdayEndTime\":\"07:00\",\"thursdayStartTime\":\"20:00\",\"thursdayEndTime\":\"07:00\",\"fridayStartTime\":\"20:00\",\"fridayEndTime\":\"07:00\",\"saturdayStartTime\":\"13:00\",\"saturdayEndTime\":\"07:00\",\"sundayStartTime\":\"00:00\",\"sundayEndTime\":\"07:00\"}},\"registrationTimestamp\":1549971011,\"customerSubscriptionName\":\"Premium\"}"
```

### Delete a Building

{% openapi src="<https://doc.api.moost.io/docs/openapi/openapi.json>" path="/buildings/{customerBuildingId}/v1" method="delete" %}
<https://doc.api.moost.io/docs/openapi/openapi.json>
{% endopenapi %}

#### Example with CURL

```sh
curl -X DELETE "https://api.moost.io/buildings/999000009C54AAAB/v1" \
     -H "Authorization: Bearer $ACCESS_TOKEN"
```

## Synchronize Devices of Buildings

Make sure that devices of buildings, which shall be taken into consideration by MOOST Recommender Platform, are synchronized to MOOST via MOOST API.

Remark: it is also possible to manage the devices via the upper mentioned building API's.

### Update Devices of a Building

{% openapi src="<https://doc.api.moost.io/docs/openapi/openapi.json>" path="/buildings/{customerBuildingId}/devices/v1" method="put" %}
<https://doc.api.moost.io/docs/openapi/openapi.json>
{% endopenapi %}

#### Example with CURL

```sh
curl -X PUT "https://api.moost.io/buildings/999000009C54AAAA/devices/v1" \
    -H  "Authorization: Bearer $ACCESS_TOKEN" \
    -H  "Content-Type: application/json"  \
    -d "[{\"id\": \"17306deb97e0f94609f13e22\", \"type\": \"CAR\", \"vendor_name\": \"Device Link Car\", \"product_name\": \"Device Link Car\", \"createdAt\": 1702981099,\"updatedAt\": 1707135597}, {\"id\": \"1730da2d6e14aa3701e65105\", \"type\": \"HEAT_PUMP\", \"vendor_name\": \"SG Ready Switch\", \"product_name\": \"SG Ready Switch\", \"createdAt\": 1665915437,\"updatedAt\": 1707124897}, {\"id\": \"173042c71376cf79c2dc8bde\", \"type\": \"BATTERY\", \"vendor_name\": \"TWICE Hexagon\", \"product_name\": \"TWICE Hexagon\", \"createdAt\": 1697727175,\"updatedAt\": 1707144323}, {\"id\": \"173062ba52ae9f481a010aa9\", \"type\": \"CAR_CHARGER\", \"vendor_name\": \"easee Home\", \"product_name\": \"easee Home\", \"createdAt\": 1617388218,\"updatedAt\": 1707144356}]"
```
