SonnenBatterie sensors and switches

Maybe not the best code around, but quick to use for SonnenBatterie owners. It is easy to read and thus to expand using the battery’s API information. In combination with nabu casa it’s perfect before your vacation!

Cheers,
Mischa

rest:

  • resource: http://[IP address]:80/api/v2/status
    method: GET
    headers:
    User-Agent: Home Assistant
    Content-Type: application/json
    Auth-Token: [TOKEN]
    scan_interval: 10
    sensor:
    • name: “Backup Buffer”
      value_template: ‘{{ value_json[“BackupBuffer”] }}’
      unit_of_measurement: “%”
      device_class: battery
    • name: “Operating Mode (1 = API, 2 = Auto, 10 = ToU)”
      value_template: ‘{{ value_json[“OperatingMode”] }}’
    • name: “Battery Charging”
      value_template: ‘{{ value_json[“BatteryCharging”] }}’
    • name: “Battery Discharging”
      value_template: ‘{{ value_json[“BatteryDischarging”] }}’
    • name: “User State of Charge”
      value_template: ‘{{ value_json[“USOC”] }}’
      unit_of_measurement: “%”
      device_class: battery
    • name: “System Status On Grid or Off Grid”
      value_template: ‘{{ value_json[“SystemStatus”] }}’
    • name: “AC Power, positive is discharging”
      value_template: ‘{{ value_json[“Pac_total_W”] }}’
      unit_of_measurement: “Watt”
      device_class: power
  • resource: http://[IP address]:80/api/v2/latestdata
    method: GET
    headers:
    User-Agent: Home Assistant
    Content-Type: application/json
    Auth-Token: [TOKEN]
    scan_interval: 10
    sensor:
    • name: “Timestamp”
      value_template: >
      {% set timestamp = value_json[“Timestamp”] %}
      {{ as_timestamp(timestamp) | timestamp_local }}
      device_class: timestamp

rest_command:
set_operating_mode_manual:
url: “http://[IP address]:80/api/v2/configurations”
method: PUT
headers:
User-Agent: Home Assistant
Content-Type: application/json
Auth-Token: [TOKEN]
payload: ‘{“EM_OperatingMode”: “1”}’

set_operating_mode_auto:
url: “http://[IP address]:80/api/v2/configurations”
method: PUT
headers:
User-Agent: Home Assistant
Content-Type: application/json
Auth-Token: [TOKEN]
payload: ‘{“EM_OperatingMode”: “2”}’

set_discharge_9500_watts:
url: “http://[IP address]:80/api/v2/setpoint/discharge/9500”
method: POST
headers:
User-Agent: Home Assistant
Content-Type: application/json
Auth-Token: [TOKEN]
payload: “{}”

set_charge_9500_watts:
url: “http://[IP address]:80/api/v2/setpoint/charge/9500”
method: POST
headers:
User-Agent: Home Assistant
Content-Type: application/json
Auth-Token: [TOKEN]
payload: “{}”

set_discharge_0_watts:
url: “http://[IP address]:80/api/v2/setpoint/discharge/0”
method: POST
headers:
User-Agent: Home Assistant
Content-Type: application/json
Auth-Token: [TOKEN]
payload: “{}”

switch:

  • platform: template
    switches:
    operating_mode_manual:
    friendly_name: “Set Operating Mode Manual”
    icon_template: mdi:cog
    value_template: “{{ is_state(‘sensor.operating_mode_1_api_2_auto_10_tou’, ‘1’) }}”
    turn_on:
    service: rest_command.set_operating_mode_manual
    turn_off:
    service: rest_command.set_operating_mode_auto

    operating_mode_auto:
    friendly_name: “Set Operating Mode Auto”
    icon_template: mdi:autorenew
    value_template: “{{ is_state(‘sensor.operating_mode_1_api_2_auto_10_tou’, ‘2’) }}”
    turn_on:
    service: rest_command.set_operating_mode_auto
    turn_off:
    service: rest_command.set_operating_mode_manual

    discharge_9500:
    friendly_name: “Set Discharge to 9500 Watts”
    icon_template: mdi:battery-charging-100
    value_template: “{{ is_state(‘sensor.ac_power_positive_is_discharging’, ‘9500’) }}”
    turn_on:
    service: rest_command.set_discharge_9500_watts
    turn_off:
    service: rest_command.set_discharge_0_watts

    charge_9500:
    friendly_name: “Set Charge to 9500 Watts”
    icon_template: mdi:battery-charging-100
    value_template: “{{ is_state(‘sensor.ac_power_positive_is_discharging’, ‘-9500’) }}”
    turn_on:
    service: rest_command.set_charge_9500_watts
    turn_off:
    service: rest_command.set_discharge_0_watts

    discharge_0:
    friendly_name: “Set Discharge to 0 Watts”
    icon_template: mdi:battery
    value_template: “{{ is_state(‘sensor.ac_power_positive_is_discharging’, ‘0’) }}”
    turn_on:
    service: rest_command.set_discharge_0_watts
    turn_off:
    service: rest_command.set_discharge_9500_watts