Configure solar production from (python) script output

hi,

I am new to home assistant and I am trying to configure the ‘Return to grid’ in the Electrity grid under Energy. I have scanned through docs and find it challenging to get started with something that looks simple to me.

I have a Huawei Inverter but no API account, so I am using this python module to get data from it : fusion-solar-py · PyPI

It works quite well and example output is :slight_smile:

Current power: 0.84 kW
Total power today: 9.25 kWh
Total power: 2058.27 kWh

The script logs in and opens a session, so I prefer it to keep the session open if possible, but just running it every 60 seconds is good enough too.

All I basically want is for the script output to become 3 sensors for this example so I can set them up.

Can anyone describe step by step how to do this?
I read docs for Templates, Sensors and entities and also found examples but nowhere where to bind the template together with a python script.

I don’t understand what platform means in the examples I saw and why they are sometimes within or outside sensor in YAML files.

A step by step explanation would be really appreciated!

It is strange that all three the entries are called “power”, because the last two are not power but energy.
Is this the exact and complete output that you get?

So looking at the python module, i presume you have copied the example python script. I would alter this so that the output from your script is in json format. Ie.

{
  "current_power": 0.84,
  "total_power_today": 9.25,
  "total_power": 2058.27
}

Then you can use the command line integration to read this into sensors.

Something like this:

command_line:
  - sensor:
    name: "Solar Power "
    command: "python -c path to python script"
    value_template: "{{ value_json.current_power }}"
    json_attributes:
      - total_power_today
      - total_power
    unit_of_measurement: kW
    scan_interval: 60

This will create one sensor with your totals as attributes. You can then create 2 template sensors to add sensors for these attributes. Note to use in the energy dashboard you will also need to set device_class and state_class attributes.

Doing this way means you only call the script once for all 3 sensors. You could add each one in the command_line integration but it will call your script 3 times on each update.

Template sensor example:

template:
  - sensor:
    name: Total Power
    state: "{{ state_attr('sensor.solar_power', 'total_power')|float(0) }}"
    unit_of_measure: kWh
    device_class: energy
    state_class: total_increasing

hi thank you for the quick and elaborate reply, that clears up a lot!

In the mean time I also found this : GitHub - jgriss/FusionSolarHA: A Home Assistant integration for Huawei's Fusion Solar cloud interface
It’s from the same person that made the python module.

It took some time for me to figure out installation, but basically:

  • copy custom_components folder and contents in the /config directory (where configuration.yaml resides).
  • in HA, go to Settings - Devices & Services - Add integration ( look for Fusion, should show up )
  • click and fill in credentials for user acccount ( not API user, that is another plugin )

I had to make some changes to the source code to send along the proper domain, I will try to make a PR at the repo when I have time ( it is rather clear what to do from the module docs ).

Now I have the values that are in the plugin by default as sensors showing in the Overview.

I ordered a cable to get my meter data in, then I will continue to hook that up.

Thanks a lot for the help, it is very useful in general to put other things in HA and documentation is quite scattered, so great to have the process explained!

I added a PR : add support for Huawei domain like in the used library by VGerris · Pull Request #2 · jgriss/FusionSolarHA · GitHub - works on my HA :).