Skip to content

3. Home Assistant integration

Richard Pearce edited this page Oct 10, 2024 · 11 revisions

Setup

Version 3.0.x

No configuration is required, it will automatically appear as a new device if you have the MQTT plugin installed.

If you upgrading from 2.2.x to 3.0.x then follow these steps:

  1. Stop the 2.2.x container

  2. Remove all previous Sofar2mqtt configuration from your home assistant configuration.yaml

  3. Start the rpearce/sofar2mqtt-python:latest container

Version 2.2.x (Deprecated)

Note: This is not currently available for the sofar-me-3000.json configuration, if you want to copy it across from the sofar-hyd-ep.json please be my guest! :)

  1. Make sure you have the MQTT plugin installed

  2. Copy the configuration files from the ha folder (or symlink) them to you Home Assistant config folder.

    cp ha/{automations, mqtt}-sofar-hyd-ep.yaml <HA-CONFIG-FOLDER>
  3. Add the following to your configuration.yaml to include it.

    mqtt: !include mqtt-sofar-hyd-ep.yaml
    automation sofar: !include automations-sofar-hyd-ep.yaml
    
    input_select:
      sofar_energy_storage_mode:
        name: Sofar Energy Storage Mode
        options:
          - "Self use"
          - "Time of use"
          - "Timing mode"
          - "Passive mode"
          - "Peak cut mode"
        icon: mdi:home-battery
      sofar_desired_power:
        name: Sofar Desired Power (-discharge/+charge)
        options:
          - "-5000"
          - "-4500"
          - "-4000"
          - "-3500"
          - "-3000"
          - "-2500"
          - "-2000"
          - "-1500"
          - "-1000"
          - "-500"
          - "0"
          - "500"
          - "1000"
          - "1500"
          - "2000"
          - "2500"
          - "3000"
          - "3500"
          - "4000"
          - "4500"
          - "5000"
        initial: "0"
        icon: mdi:battery-charging
  4. Restart/Reload home assistant

Octopus Energy Automations

These required the Home Assistant Octopus Energy integration. You will also find these Octopus Energy Blueprints exceptionally useful.

For the templates below substitute your own binary_sensor for off_peak and saving_sessions. I will try and create a Blueprint for these in future.

Octopus - Charge Off-Peak

alias: Sofar - Octopus - Initiate Charge Off-Peak
description: ""
trigger:
  - platform: state
    entity_id:
      - >-
        binary_sensor.octopus_energy_electricity_123456789_123456789_off_peak
    to: "on"
action:
  - service: select.select_option
    metadata: {}
    data:
      option: Passive mode
    target:
      entity_id: select.sofar_mode
  - service: number.set_value
    metadata: {}
    data:
      value: 2000
    target:
      entity_id: number.sofar_desired_power
mode: single

Octopus - After session return to Self-Use

alias: Sofar - Octopus - After session return to Self-Use
description: ""
trigger:
  - platform: state
    entity_id:
      - >-
        binary_sensor.octopus_energy_electricity_123456789_123456789_off_peak
    to: "off"
action:
  - service: select.select_option
    metadata: {}
    data:
      option: Self use
    target:
      entity_id: select.sofar_mode
mode: single

Octopus Saving-Session - Initiate Discharge

alias: Sofar - Octopus Saving-Session - Initiate Discharge
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.octopus_energy_a_123456789_octoplus_saving_sessions
    to: "on"
condition:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.sofar_battery_soc
        above: 20
action:
  - service: select.select_option
    metadata: {}
    data:
      option: Passive mode
    target:
      entity_id: select.sofar_mode
  - service: number.set_value
    data:
      value: "{{ states('input_number.octopus_power_up_discharge_power') | float(0) }}"
    target:
      entity_id: number.sofar_desired_power
mode: single

Octopus Saving Session - After session return to Self-Use

alias: Sofar - Octopus Saving Session - After session return to Self-Use
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.octopus_energy_a_123456789_octoplus_saving_sessions
    to: "off"
condition:
  - condition: state
    entity_id: binary_sensor.octopus_energy_electricity_123456789_123456789_off_peak
    state: "off"
action:
  - service: select.select_option
    metadata: {}
    data:
      option: Self use
    target:
      entity_id: select.sofar_mode
mode: single

Switch to Passive mode when battery is flat

alias: Sofar - Switch to Passive mode when battery is flat
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.sofar_battery_soc
    below: 17
condition:
  - condition: state
    entity_id: binary_sensor.octopus_energy_electricity_123456789_123456789_off_peak
    state: "off"
action:
  - service: select.select_option
    metadata: {}
    data:
      option: Passive mode
    target:
      entity_id: select.sofar_mode
  - service: number.set_value
    metadata: {}
    data:
      value: 0
    target:
      entity_id: number.sofar_desired_power
mode: single

Notifications

Below are some notifications that you might find useful to keep an eye on your Sofar inverter and Batteries. Substitute your own binary_sensor for off_peak and saving_sessions and your notification device.

Notify Sofar status

alias: Sofar - Notify status
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.sofar_battery_soc
      - sensor.sofar_state
      - select.sofar_mode
condition: []
action:
  - service: notify.mobile_app_device
    data:
      message: >-
        states('sensor.sofar_state') }} - {{ states('select.sofar_mode')}} - {{
        states('sensor.sofar_heatsink_temperature') }}c\n  Battery: {{
        states('sensor.sofar_battery_soc')  }}%\n Gen: {{
        states('sensor.sofar_solar_generation_today') }}kWh\nExport: {{
        states('sensor.sofar_export_today') }}kWh
      data:
        url: /lovelace-solar/current
        clickAction: /lovelace-solar/current
        notification_icon: mdi:home-battery
        importance: low
        persistent: true
        sticky: "true"
        tag: sofar
mode: single

Notify when Sofar data has not changed for 10 minutes

alias: Sofar - Notify when Sofar data has not changed for 10 minutes
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ now() - states.sensor.sofar_load_power.last_changed >= timedelta
      (minutes=10) }}
action:
  - service: notify.mobile_app_device
    data:
      title: Sofar data is stale
      message: "{{ states.sensor.sofar_load_power.last_changed }}"
      data:
        channel: Sofar Stale
        importance: high
        vibrationPattern: 100, 1000, 100, 1000, 100
        ledColor: red
        alert_once: true
        notification_icon: mdi:alert-circle
mode: single

Notify when grid power is being used

alias: Sofar - Notify when grid power is being used
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.sofar_on_grid_power
    above: 100
    for:
      hours: 0
      minutes: 0
      seconds: 30
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: >-
          binary_sensor.octopus_energy_electricity_123456789_123456789_off_peak
        state: "off"
action:
  - service: notify.mobile_app_device
    data:
      title: Sofar grid power is high
      message: ">{{ states('sensor.sofar_on_grid_power') }}w for 30 seconds"
      data:
        channel: Sofar Grid power
        ttl: 0
        priority: high
        importance: high
        vibrationPattern: 1, 1, 1, 1, 1
        ledColor: red
        alert_once: true
        notification_icon: mdi:alert-circle
mode: single