Calculating Solar charging power

Hi all,

I do not have a sensor installed for Solar production.

But I have sensors for Total load power, Battery charging power & Battery discharging power. All are in Watt.

If Total load power sensor is D, Battery charging power sensor is B & Battery discharging power sensor is C, can someone give me the code to calculate the Solar production (A + B as per the below drawing)?

dia

template:
  - sensor:
      - name: Total Solar Power
        unit_of_measurement: W
        device_class: power
        state_class: measurement 
        state: "{{ states('sensor.d')|float(0) - states('sensor.c')|float(0) + states('sensor.b')|float(0) }}"
        availability: "{{ has_value('sensor.d') and has_value('sensor.c') and has_value('sensor.b') }}"
1 Like

Thak you for the fast reply.

I have put this on configuration,yaml

But I get the error “Invalid config for [sensor.template]: [state_class] is an invalid option for [sensor.template].”

What is the reason?

Where exactly did you put this?

In configuration.yaml file.

Where should I put this?

Where in your configuration.yaml file?

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

sensor:
  - platform: template:
  - sensor:
      - name: Total Solar Power
        unit_of_measurement: W
        device_class: power
        state_class: measurement 
        state: "{{ states('sensor.total_power_consumption')|float(0) - states('sensor.jk_bms_discharging_power')|float(0) + states('sensor.jk_bms_charging_power')|float(0) }}"
        availability: "{{ has_value('sensor.total_power_consumption') and has_value('jk_bms_discharging_power') and has_value('sensor.jk_bms_charging_power') }}"

Yeah. That’s not what I wrote.

template: is an integration. Like sensor: , script: or automation: it belongs hard up against the file margin.

You don’t put it under sensor:. You do this:

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

template:
  - sensor:
      - name: Total Solar Power
        unit_of_measurement: W
        device_class: power
        state_class: measurement 
        state: "{{ states('sensor.d')|float(0) - states('sensor.c')|float(0) + states('sensor.b')|float(0) }}"
        availability: "{{ has_value('sensor.d') and has_value('sensor.c') and has_value('sensor.b') }}"

See:

Thank you very much.

It was my mistake