Create a proper Energy(PV) meter with power and energy from external source

I want to incorporate the output of a script in HA. The script provides a value for PV

  • Power
  • Daily energy
  • Total energy all time

every 5 seconds.
I have the opportunity to POST these values using curl every 5 seconds to HA. How do I set op HA properly so that it properly interprets this sensor as an energy meter?

I upload the “power” value using this command:
curl -X POST -H “Authorization: Bearer ##MYTOKEN##”
-H “Content-Type: application/json”
-d ‘{“state”: “500”, “attributes”: {“unit_of_measurement”: “W”, “friendly_name”: “Solar-power”}}’ http://192.168.2.120:8123/api/states/sensor.solar_power

500 is an example. The script fills in the correct value. How do I set up HA properly to have a PV sensor that I can use in the Energy Dash?

What you want is to set up a webhook in HA. I would not go the API route because you need a sensor you can set, which does not have a way to set the right state_class.

You create a webhook by creating an automation or by creating a trigger based template sensor using a webhook trigger. Usually I go for the latter, the principle is the same.

The sensors need the right device class, state class and unit for whatever type of sensor you are setting up. State class will need to be total_increasing for energy.

Interesting. I’l take a look. It is a bit difficult to understand what they mean with the different terms. I will figure it out. but when the webhook is in place and I upload some data: how do I connect that to a sensor with correct device and state class? Another thought: the daily and total energy values are total current values as is. It should not be added to previous values. Will that work with total_increasing?

The webhook trigger makes it so HA listens for messaged. It triggers when the data comes in. A trigger based template sensor uses the trigger to store the data in template sensors. They look a lot like the automation examples in the webhook documentation. But instead of actions you define the sensors beneath it, using templates to extract the data.

So I added a bit of code to my configuration.yaml

template:
  - triggers:
      - trigger: webhook
        webhook_id: abacadabra
    sensor:
      - name: "My PV Power"
        state: "{{ trigger.json.pvp }}"
        unit_of_measurement: W
        device_class: power
        state_class: measurement

      - name: "My PV Energy daily"
        state: "{{ trigger.json.pved }}"
        unit_of_measurement: Wh
        device_class: energy
        state_class: total_increasing

      - name: "My PV Energy total"
        state: "{{ trigger.json.pvet }}"
        unit_of_measurement: Wh
        device_class: energy
        state_class: total_increasing

In the Energy dash I can select these as energy sources. Current Power will show in the graph. Energy production however, remains zero.
What could be wrong? I do see the updated values in the sensor overview
Also: after a restart of HA the values are unknown. How can I store these?
Also2: Does the energy dash require daily production or total production?

Values returning to zero is strange, Template sensors restore at startup for type sensor and binary_sensor.

I can’t tell why values remain 0 either without knowing what you send. Are you sure you are looking at the right entity? Hve you checked developer tools if there maybe are multiple entites with similar names?

I would add a unique_id to all sensors.

I upload values with the following command:

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"pvp": 0, "pved": 343, "pvet": 43567231}' \
  http://192.168.2.120:8123/api/webhook/abacadabra

Obviously the numbers will be filled in by variables in the upload script.

After a restart the values in the sensor overview are “unknown” until i upload a new value. This might not be a big problem.

I see all the uploaded values in the sensor overview. When I add them to the energy dash, I see only 0 kWh for energy. PV power works as expected.

I created these with unique new names. In don’t know where unique-id’s are specified

Energy reacts on changes. It also updates only once an hour in the energy dash. So give it some time.

From the doc:

The state, including attributes, of trigger-based sensors and binary sensors is restored when Home Assistant is restarted.

I managed to add unique id’s and that took care of having the data in there after a restart. Apparently unique id is necessary for that

1 Like

can this frequency be increased?

No, it only shows an hour bar once the hour is complete. You’d have to build your own graph if you want that.