Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with Huawei FusionSolar #95

Open
EmAnzi3 opened this issue Dec 3, 2021 · 16 comments
Open

Working with Huawei FusionSolar #95

EmAnzi3 opened this issue Dec 3, 2021 · 16 comments
Labels
documentation Improvements or additions to documentation

Comments

@EmAnzi3
Copy link

EmAnzi3 commented Dec 3, 2021

Hello,
As per the one of the last post I don't really have issue, I simply love the card and I'd like to replicate the app readings, FusionSolar, in HA.
In HA the integration mainly utilizes the following entities:
Home Usage (Load):

      home_usage:
        friendly_name: "Home Usage"
        unit_of_measurement: "W"
        value_template: >-
          {% set active_power = states('sensor.sun2000_8ktl_m1') | int %}
          {% set power_meter = state_attr('sensor.sun2000_8ktl_m1', 'power_meter_active_power') | int %}
          {% if power_meter < active_power %}
              {{ (active_power - power_meter) }}
          {% else %}
              {{ (0) | int }}
          {% endif %}

Power Imported from Grid:

      import_from_grid:
        friendly_name: "Power Imported from Grid"
        unit_of_measurement: "W"
        value_template: >-
          {% set pm_active_power = state_attr('sensor.sun2000_8ktl_m1', 'power_meter_active_power') | int %}
          {% if pm_active_power < 0 %}
              {{ (-1*pm_active_power) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}

PV Input Power:

      pv_input_power:
        friendly_name: "PV Input Power"
        unit_of_measurement: 'W'
        value_template: "{{ state_attr('sensor.sun2000_8ktl_m1', 'input_power') | float(0) }}"

Batteries charge/discharge readings are coming from sensor.storage_charge_discharge_power, this come directly from the integration. When value is positive the battery is charged by the PV system or from the external grid, when negative it feeds the Home Usage (Load).
How can I setup the card in order to properly distribute all the power flows? Thanks.

@EmAnzi3
Copy link
Author

EmAnzi3 commented Dec 5, 2021

Any clue, please?

@sebrep
Copy link

sebrep commented Dec 6, 2021

Read the configuration from the tesla powerwall setup. Sure the variable names are different but you should be good with those. Otherwise search on the ha community website for someone that already did this or something similar.

@JacoboSanchez
Copy link

FusionSolar flow chart is different from this one as it has no direct lines between entities. They have intermediate units like meter and inverter and you must define individual lines from node to node. So to replicate it you must somehow define your own rules. All of this is a lot more complicated with batteries. I will post you my current configuration and templates... they may give wrong values in some situations as I am still monitoring and tweaking it.

Sensors (note that you need to substitute $CHARGE_DISCHARGE_POWER$, $POWER_METER_ACTIVE_POWER$, $INPUT_POWER$, $ACTIVE_POWER$)

  - platform: template
    sensors:
      grid_to_battery:
        friendly_name: "Grid to battery"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set ip = $ACTIVE_POWER$ | int %}
          {% set cdp = $CHARGE_DISCHARGE_POWER$ | int %}
          {% if ip < 0 and cdp > 0 %}
              {{ (-1 * ip) | int }}
          {% else %}
              {{ (0) }}
          {% endif %}
      grid_to_house:
        friendly_name: "Grid to house"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set g_c = states('sensor.grid_consumption') | int %}
          {% set g_t_b = states('sensor.grid_to_battery') | int %}
          {{ (g_c-g_t_b) | int }}
      generation_to_battery:
        friendly_name: "Generation to battery"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set b = states('sensor.battery_charge') | int %}
          {% set ap = $INPUT_POWER$ | int %}
          {% if b > 0 and ap > 0 and b < ap %}
              {{ (b) | int }}
          {% elif b > 0 and ap > 0 and b > ap %}
              {{ (ap) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
      generation_to_house:
        friendly_name: "Generation to house"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set pv = $INPUT_POWER$ | int %}
          {% set g_t_b = states('sensor.generation_to_battery') | int %}
          {% set g_t_gr = states('sensor.generation_to_grid') | int %}
          {% if (pv - g_t_b - g_t_gr) > 0 %}
              {{ (pv - g_t_b - g_t_gr) | int }}
          {% else %}
              {{ (0) }}
          {% endif %}
      grid_consumption:
        friendly_name: "Grid consumption"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set u = $POWER_METER_ACTIVE_POWER$ | int %}
          {% if u < 0 %}
              {{ (- u) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
      generation_to_grid:
        friendly_name: "Generation to grid"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set ip = $INPUT_POWER$ | int %}
          {% set pmap = $POWER_METER_ACTIVE_POWER$ | int %}
          {% if pmap > 0 and ip > pmap%}
              {{ (pmap) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
      battery_to_house:
        friendly_name: "Battery to house"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set u = $CHARGE_DISCHARGE_POWER$ | int %}
          {% if u < 0 %}
              {{ (- u) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
      battery_charge:
        friendly_name: "Battery charge"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set u = $CHARGE_DISCHARGE_POWER$ | int %}
          {% if u > 0 %}
              {{ (u) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
      house_consumption:
        friendly_name: "House consumption"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set t = states('sensor.battery_to_house') | int %}
          {% set u = states('sensor.total_consumption') | int %}
          {{ (t + u) }}
      total_consumption:
        friendly_name: "Total consumption"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set t = $ACTIVE_POWER$ | int %}
          {% set u = $POWER_METER_ACTIVE_POWER$ | int %}
          {% if u < t %}
              {{ (t - u) }}
          {% else %}
              {{ (0) | int }}
          {% endif %}

Card configuration:

type: custom:tesla-style-solar-power-card
generation_to_house_entity: sensor.generation_to_house
generation_to_grid_entity: sensor.generation_to_grid
generation_to_battery_entity: sensor.generation_to_battery
grid_consumption_entity: sensor.grid_consumption
house_consumption_entity: sensor.house_consumption
grid_to_house_entity: sensor.grid_to_house
battery_to_house_entity: sensor.battery_to_house
battery_extra_entity: sensor.storage_state_of_capacity_102150268986
grid_to_battery_entity: sensor.grid_to_battery

(Hope I paste it correctly)

@MicheleMercuri
Copy link

MicheleMercuri commented Mar 13, 2022

maybe something wrong in sensor.grid_to_battery?

        friendly_name: "Grid to battery"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set ip = $ACTIVE_POWER$ | int %}
          {% set cdp = $CHARGE_DISCHARGE_POWER$ | int %}
          {% if ip < 0 and cdp > 0 %}
              {{ (-1 * ip) | int }}
          {% else %}
              {{ (0) }}
          {% endif %}`

i've a inverter 5ktl + luna2000 huawey, so:
$ACTIVE_POWER$ = sensor.active_power_2 ("sensor.active_power_2" = is power sensor from grid and have negative value when take power "from grid")
$CHARGE_DISCHARGE_POWER$ = sensor.charge_discharge_power ( "sensor.charge_discharge_power" = is power sensor charge/discharge battery with positive value when charging BUT this sensor is also positive when is charging from PV)

@sebrep
Copy link

sebrep commented Mar 13, 2022

yes, it's the one that I don't have and I destroyed my test env so I'm flying blind

@JacoboSanchez
Copy link

Well that was the testing version I had at that moment... home consumption and total consumption there are probably messed up. As I said when I posted it there is no direct translation from data obtained from inverter to the flows used in the card so you have to make your own rules. That sensor assumes that if you are not charging from PV and grid at the same time as the only charge in my case is done by a few W to keep the SOC at 15-16%... if you force the charge from grid that may not be true and you should figure out how to take that into account.

If you manage to get a more realistic version please post it... I can post my current version... however it does have the same code for grid_to_battery

@JacoboSanchez
Copy link

JacoboSanchez commented Mar 13, 2022

Just copied it without formatting nor checking but this is what I am using right now, as I said a more precise version of grid_to_battery will be helpful for all of us.

       grid_to_battery:
        friendly_name: "Grid to battery"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set ip = states('sensor.active_power') | int %}
          {% set cdp = states('sensor.storage_charge_discharge') | int %}
          {% if ip < 0 and cdp > 0 %}
              {{ (-1 * ip) | int }}
          {% else %}
              {{ (0) }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.active_power') not in [ 'unavailable', 'none', 'unknown' ] )
              and (states('sensor.storage_charge_discharge') not in [ 'unavailable', 'none', 'unknown' ]) }}
      grid_to_house:
        friendly_name: "Grid to house"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set g_c = states('sensor.grid_consumption') | int %}
          {% set g_t_b = states('sensor.grid_to_battery') | int %}
          {{ (g_c-g_t_b) | int }}
        availability_template: >-
          {{ (states('sensor.grid_consumption') not in [ 'unavailable', 'none', 'unknown' ] )
              and (states('sensor.grid_to_battery') not in [ 'unavailable', 'none', 'unknown' ]) }}
      generation_to_battery:
        friendly_name: "Generation to battery"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set b = states('sensor.battery_charge') | int %}
          {% set ap = states('sensor.input_power') | int %}
          {% if b > 0 and ap > 0 and b < ap %}
              {{ (b) | int }}
          {% elif b > 0 and ap > 0 and b > ap %}
              {{ (ap) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.battery_charge') not in [ 'unavailable', 'none', 'unknown' ] )
              and (states('sensor.input_power') not in [ 'unavailable', 'none', 'unknown' ]) }}
      generation_to_house:
        friendly_name: "Generation to house"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set pv = states('sensor.input_power') | int %}
          {% set g_t_b = states('sensor.generation_to_battery') | int %}
          {% set g_t_gr = states('sensor.generation_to_grid') | int %}
          {% if (pv - g_t_b - g_t_gr) > 0 %}
              {{ (pv - g_t_b - g_t_gr) | int }}
          {% else %}
              {{ (0) }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.input_power') not in [ 'unavailable', 'none', 'unknown' ] )
              and (states('sensor.generation_to_battery') not in [ 'unavailable', 'none', 'unknown' ]) 
              and (states('sensor.generation_to_grid') not in [ 'unavailable', 'none', 'unknown' ]) }}
      grid_consumption:
        friendly_name: "Grid consumption"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set u = states('sensor.power_meter_active_power') | int %}
          {% if u < 0 %}
              {{ (- u) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.power_meter_active_power') not in [ 'unavailable', 'none', 'unknown' ] )}}
      generation_to_grid:
        friendly_name: "Generation to grid"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set ip = states('sensor.input_power') | int %}
          {% set pmap = states('sensor.power_meter_active_power') | int %}
          {% if pmap > 0 and ip > pmap%}
              {{ (pmap) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.input_power') not in [ 'unavailable', 'none', 'unknown' ] )
              and (states('sensor.power_meter_active_power') not in [ 'unavailable', 'none', 'unknown' ]) }}
      battery_to_house:
        friendly_name: "Battery to house"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set u = states('sensor.storage_charge_discharge') | int %}
          {% if u < 0 %}
              {{ (- u) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.storage_charge_discharge') not in [ 'unavailable', 'none', 'unknown' ] ) }}
      battery_charge:
        friendly_name: "Battery charge"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set u = states('sensor.storage_charge_discharge') | int %}
          {% if u > 0 %}
              {{ (u) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.storage_charge_discharge') not in [ 'unavailable', 'none', 'unknown' ] ) }}
      house_consumption:
        friendly_name: "House consumption"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set t = states('sensor.battery_to_house') | int %}
          {% set u = states('sensor.grid_to_house') | int %}
          {% set g = states('sensor.generation_to_house') | int %}
          {{ (t + u + g) }}
        availability_template: >-
          {{ (states('sensor.battery_to_house') not in [ 'unavailable', 'none', 'unknown' ] )
              and (states('sensor.grid_to_house') not in [ 'unavailable', 'none', 'unknown' ]) 
              and (states('sensor.generation_to_house') not in [ 'unavailable', 'none', 'unknown' ]) }}```

@MicheleMercuri
Copy link

many thanks 4 your code, but I've not any better solution, I don't use charge to/from grid (jus deleted 2 sensors from lovelace).
I'm a newbe and your code is much better and "clean" than my code :)

`# sensor battery discharge
- platform: template  
  sensors:
    power_batt_disch_kwp:
      friendly_name: "Battery Power Dishcarge"
      unit_of_measurement: "W"
      value_template: >-
        {% if states('sensor.charge_discharge_power') | int < 0 %}
          {{(states('sensor.charge_discharge_power') | float * -1) | round(2)}}
        {% elif states('sensor.charge_discharge_power') | int >= 0 %}
          {{(states('sensor.charge_discharge_power') | float * 0) }}
        {% endif %}
# sensor battery charge
- platform: template  
  sensors:
    power_batt_ch_kwp:
      friendly_name: "Battery Power Charge"
      unit_of_measurement: "W"
      value_template: >-
        {% if states('sensor.charge_discharge_power') | int > 0 %}
          {{(states('sensor.charge_discharge_power') | float * 1) | round(2)}}
        {% elif states('sensor.charge_discharge_power') | int <= 0 %}
          {{(states('sensor.charge_discharge_power') | float * 0)}}
        {% endif %}
# sensor from grid power
- platform: template  
  sensors:
    power_grid_kwp:
      friendly_name: "Grid Power"
      unit_of_measurement: "W"
      value_template: >-
        {% if states('sensor.active_power_2') | int < 0 %}
          {{(states('sensor.active_power_2') | float * -1) | round(2)}}
        {% elif states('sensor.active_power_2') | int >= 0 %}
          {{(states('sensor.active_power_2') | float * 0)}}
        {% endif %}
# sensor PV to grid power
- platform: template  
  sensors:
    pv_to_grid_kwp:
      friendly_name: "PV to Grid Power"
      unit_of_measurement: "W"
      value_template: >-
        {% if states('sensor.active_power_2') | int > 0 %}
          {{(states('sensor.active_power_2') | float * 1) | round(2)}}
        {% elif states('sensor.active_power_2') | int <= 0 %}
          {{(states('sensor.active_power_2') | float * 0)}}
        {% endif %}
# sensor PV to house power
- platform: template
  sensors:
    pv_to_house_kwp:
      unit_of_measurement: "W"
      friendly_name: "PV to House Power"
      value_template: "{{ float(states('sensor.input_power')) - float(states('sensor.power_batt_ch_kwp')) }}"
# sensor house total power
- platform: template
  sensors:
    total_house_kwp:
      unit_of_measurement: "W"
      friendly_name: "Total House Power"
      value_template: "{{ float(states('sensor.active_power')) + float(states('sensor.power_grid_kwp')) }}"`

@reptilex reptilex added the documentation Improvements or additions to documentation label Apr 2, 2022
@serbat
Copy link

serbat commented Apr 24, 2022

I've been trying for several days but still without results, I can't get the "tesla" card to work.

I don't understand how to set the sensors, yet for the graph below I managed to make it work, but for this card I just can't .....
Kindly, you can post your complete and updated code, the one posted above gives me the result as per the attached image ..
Thanks

immagine

@JacoboSanchez
Copy link

I did not any major modification from the configuration posted in this post. It is a template based on my sensor names, You should verify that you have the same names or adapt them in your configuration.

Also to check what is going on I suggest you to create a card showing the values for the different flows (grid_to_battery, generation_to_grid, etc..) so you can locate which one used in the flow card is giving you wrong results, for example with an entity card like the following (as allways you should check your own sensor names and adapt the configuration to use them):

type: entities
entities:
  - entity: sensor.generation_to_house
    name: generation_to_house
  - entity: sensor.generation_to_grid
    name: generation_to_grid
  - entity: sensor.generation_to_battery
    name: generation_to_battery_entity
  - entity: sensor.house_consumption
    name: house_consumption_entity
  - entity: sensor.grid_to_house
    name: grid_to_house_entity
  - entity: sensor.battery_to_house
    name: battery_to_house_entity
  - entity: sensor.storage_state_of_capacity
    name: battery_extra_entity
  - entity: sensor.grid_to_battery
    name: grid_to_battery_entity

@Brusta37
Copy link

Brusta37 commented May 6, 2022

Hi there,
I'm using the Huawei Solar Integration from wlcrs.
I tried to add the sensors as per your suggestion above but i'm not able to receive values from the "House_Consumption" sensor.
Checking the entity I can see it as "not available" and an error says that the ID is not unique. But even modifying the ID i continue to rceive the same error.
Any advice will be higly appreciated.

Below you can see the code that I'm using:
`

`#solar sensor
  - platform: template
    sensors:
      grid_to_battery:
        friendly_name: "Grid to battery"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set ip = states('sensor.grid_active_power') | int %}
          {% set cdp = states('sensor.charge_discharge_power') | int %}
          {% if ip < 0 and cdp > 0 %}
              {{ (-1 * ip) | int }}
          {% else %}
              {{ (0) }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.grid_active_power') not in [ 'unavailable', 'none', 'unknown' ] )
              and (states('sensor.charge_discharge_power') not in [ 'unavailable', 'none', 'unknown' ]) }}
      grid_to_house:
        friendly_name: "Grid to house"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set g_c = states('sensor.grid_consumption2') | int %}
          {% set g_t_b = states('sensor.grid_to_battery') | int %}
          {{ (g_c-g_t_b) | int }}
        availability_template: >-
          {{ (states('sensor.grid_consumption2') not in [ 'unavailable', 'none', 'unknown' ] )
              and (states('sensor.grid_to_battery') not in [ 'unavailable', 'none', 'unknown' ]) }}
      generation_to_battery:
        friendly_name: "Generation to battery"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set b = states('sensor.battery_charge') | int %}
          {% set ap = states('sensor.input_power') | int %}
          {% if b > 0 and ap > 0 and b < ap %}
              {{ (b) | int }}
          {% elif b > 0 and ap > 0 and b > ap %}
              {{ (ap) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.battery_charge') not in [ 'unavailable', 'none', 'unknown' ] )
              and (states('sensor.input_power') not in [ 'unavailable', 'none', 'unknown' ]) }}
      generation_to_house:
        friendly_name: "Generation to house"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set pv = states('sensor.input_power') | int %}
          {% set g_t_b = states('sensor.generation_to_battery') | int %}
          {% set g_t_gr = states('sensor.generation_to_grid') | int %}
          {% if (pv - g_t_b - g_t_gr) > 0 %}
              {{ (pv - g_t_b - g_t_gr) | int }}
          {% else %}
              {{ (0) }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.input_power') not in [ 'unavailable', 'none', 'unknown' ] )
              and (states('sensor.generation_to_battery') not in [ 'unavailable', 'none', 'unknown' ]) 
              and (states('sensor.generation_to_grid') not in [ 'unavailable', 'none', 'unknown' ]) }}
      grid_consumption2:
        friendly_name: "Grid consumption2"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set u = states('sensor.grid_active_power') | int %}
          {% if u < 0 %}
              {{ (- u) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.grid_active_power') not in [ 'unavailable', 'none', 'unknown' ] )}}
      generation_to_grid:
        friendly_name: "Generation to grid"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set ip = states('sensor.input_power') | int %}
          {% set pmap = states('sensor.active_power') | int %}
          {% if pmap > 0 and ip > pmap%}
              {{ (pmap) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.input_power') not in [ 'unavailable', 'none', 'unknown' ] )
              and (states('sensor.active_power') not in [ 'unavailable', 'none', 'unknown' ]) }}
      battery_to_house:
        friendly_name: "Battery to house"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set u = states('sensor.charge_discharge_power') | int %}
          {% if u < 0 %}
              {{ (- u) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.charge_discharge_power') not in [ 'unavailable', 'none', 'unknown' ] ) }}
      battery_charge:
        friendly_name: "Battery charge"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set u = states('sensor.charge_discharge_power') | int %}
          {% if u > 0 %}
              {{ (u) | int }}
          {% else %}
              {{ (0) | int }}
          {% endif %}
        availability_template: >-
          {{ (states('sensor.charge_discharge_power') not in [ 'unavailable', 'none', 'unknown' ] ) }}
      house_consumption:
        friendly_name: "House consumption"
        unit_of_measurement: "W"
        device_class: power
        value_template: >-
          {% set t = states('sensor.battery_to_house') | int %}
          {% set u = states('sensor.grid_to_house') | int %}
          {% set g = states('sensor.generation_to_house') | int %}
          {{ (t + u + g) }}
        availability_template: >-
          {{ (states('sensor.battery_to_house') not in [ 'unavailable', 'none', 'unknown' ] )
              and (states('sensor.grid_to_house') not in [ 'unavailable', 'none', 'unknown' ]) 
              and (states('sensor.generation_to_house') not in [ 'unavailable', 'none', 'unknown' ]) }}```

Thanks

@davidalanon-1
Copy link

davidalanon-1 commented Jun 9, 2022

Hello, I have made some changes in generation_to_grid and generation_to_house and it works correctly for me.
configuration.yaml:

- platform: template
sensors:
 grid_to_battery:
    friendly_name: "Grid to battery"
    unit_of_measurement: "W"
    device_class: power
    value_template: >-
      {% set ip = states('sensor.grid_active_power') | int %}
      {% set cdp = states('sensor.charge_discharge_power') | int %}
      {% if ip < 0 and cdp > 0 %}
          {{ (-1 * ip) | int }}
      {% else %}
          {{ (0) }}
      {% endif %}
    availability_template: >-
      {{ (states('sensor.grid_active_power') not in [ 'unavailable', 'none', 'unknown' ] )
          and (states('sensor.charge_discharge_power') not in [ 'unavailable', 'none', 'unknown' ]) }}
  grid_to_house:
    friendly_name: "Grid to house"
    unit_of_measurement: "W"
    device_class: power
    value_template: >-
      {% set g_c = states('sensor.grid_consumption2') | int %}
      {% set g_t_b = states('sensor.grid_to_battery') | int %}
      {{ (g_c-g_t_b) | int }}
    availability_template: >-
      {{ (states('sensor.grid_consumption2') not in [ 'unavailable', 'none', 'unknown' ] )
          and (states('sensor.grid_to_battery') not in [ 'unavailable', 'none', 'unknown' ]) }}
  generation_to_battery:
    friendly_name: "Generation to battery"
    unit_of_measurement: "W"
    device_class: power
    value_template: >-
      {% set b = states('sensor.battery_charge') | int %}
      {% set ap = states('sensor.input_power') | int %}
      {% if b > 0 and ap > 0 and b < ap %}
          {{ (b) | int }}
      {% elif b > 0 and ap > 0 and b > ap %}
          {{ (ap) | int }}
      {% else %}
          {{ (0) | int }}
      {% endif %}
    availability_template: >-
      {{ (states('sensor.battery_charge') not in [ 'unavailable', 'none', 'unknown' ] )
          and (states('sensor.input_power') not in [ 'unavailable', 'none', 'unknown' ]) }}
  generation_to_house:
    friendly_name: "Generation to house"
    unit_of_measurement: "W"
    device_class: power
    value_template: >-
      {% set pv = states('sensor.input_power') | int %}
      {% set pmap = states('sensor.active_power') | int %}
      {% set g_t_b = states('sensor.generation_to_battery') | int %}
      {% set g_t_gr = states('sensor.generation_to_grid') | int %}
      {% if (pv > pmap) %}
          {{ pmap | int }}		   
      {% elif (pv - g_t_b - g_t_gr) > 0 %}
          {{  (pv - g_t_b - g_t_gr) | int }}
      {% else %}
          {{ (0) }}
      {% endif %}
    availability_template: >-
      {{ (states('sensor.input_power') not in [ 'unavailable', 'none', 'unknown' ] )
          and (states('sensor.generation_to_battery') not in [ 'unavailable', 'none', 'unknown' ]) 
          and (states('sensor.generation_to_grid') not in [ 'unavailable', 'none', 'unknown' ]) }}
   grid_consumption2:
    friendly_name: "Grid consumption2"
    unit_of_measurement: "W"
    device_class: power
    value_template: >-
      {% set u = states('sensor.grid_active_power') | int %}
      {% if u < 0 %}
          {{ (- u) | int }}
      {% else %}
          {{ (0) | int }}
      {% endif %}
    availability_template: >-
      {{ (states('sensor.grid_active_power') not in [ 'unavailable', 'none', 'unknown' ] )}}
  generation_to_grid:
    friendly_name: "Generation to grid"
    unit_of_measurement: "W"
    device_class: power
    value_template: >-
      {% set g2g = states('sensor.grid_active_power') | int %}
      {% if (g2g) > 0 %}
          {{ (g2g) | int }}
      {% else %}
          {{ (0) | int }}
      {% endif %}
    availability_template: >-
      {{ (states('sensor.grid_active_power') not in [ 'unavailable', 'none', 'unknown' ] )}}
   battery_to_house:
    friendly_name: "Battery to house"
    unit_of_measurement: "W"
    device_class: power
    value_template: >-
      {% set u = states('sensor.charge_discharge_power') | int %}
      {% if u < 0 %}
          {{ (- u) | int }}
      {% else %}
          {{ (0) | int }}
      {% endif %}
    availability_template: >-
      {{ (states('sensor.charge_discharge_power') not in [ 'unavailable', 'none', 'unknown' ] ) }}
  battery_charge:
    friendly_name: "Battery charge"
    unit_of_measurement: "W"
    device_class: power
    value_template: >-
      {% set u = states('sensor.charge_discharge_power') | int %}
      {% if u > 0 %}
          {{ (u) | int }}
      {% else %}
          {{ (0) | int }}
      {% endif %}
    availability_template: >-
      {{ (states('sensor.charge_discharge_power') not in [ 'unavailable', 'none', 'unknown' ] ) }}
  house_consumption:
    friendly_name: "House consumption"
    unit_of_measurement: "W"
    device_class: power
    value_template: >-
      {% set t = states('sensor.battery_to_house') | int %}
      {% set u = states('sensor.grid_to_house') | int %}
      {% set g = states('sensor.generation_to_house') | int %}
      {{ (t + u + g) }}
    availability_template: >-
      {{ (states('sensor.battery_to_house') not in [ 'unavailable', 'none', 'unknown' ] )
          and (states('sensor.grid_to_house') not in [ 'unavailable', 'none', 'unknown' ]) 
          and (states('sensor.generation_to_house') not in [ 'unavailable', 'none', 'unknown' ]) }}

Lovelace Card:

type: custom:tesla-style-solar-power-card
generation_to_house_entity: sensor.generation_to_house
generation_to_grid_entity: sensor.generation_to_grid
generation_to_battery_entity: sensor.generation_to_battery
grid_consumption_entity: sensor.grid_consumption
house_consumption_entity: sensor.house_consumption
grid_to_house_entity: sensor.grid_to_house
battery_to_house_entity: sensor.battery_to_house
grid_to_battery_entity: sensor.grid_to_battery
battery_extra_entity: sensor.battery_state_of_capacity
hide_inactive_lines: 1
show_gap: true

@Brusta37
Copy link

Brusta37 commented Jun 9, 2022 via email

@davidalanon-1
Copy link

Dear @Brusta37,
The house_consumtion template has 3 sensor in the code:
- sensor.battery_to_house
- sensor.grid_to_house
- sensor.generation_to_house

Check that you have these sensors in home assistant, and if they have different names, change it in the template code.

Best regards,
David.

@Brusta37
Copy link

Brusta37 commented Jun 9, 2022

Hi David,
Thanks for your reply.
I'm using exactly the code that you have shared, so the names should be fine.

@davidalanon-1
Copy link

Dear Brusta37,
You have to delete the three quotes at the end of the last line, it's already fixed in the previous post.
I hope it works for you.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

8 participants