Home Assistant help

6 comments started 2022-02-02 last 2022-02-02
APIsHome AutomationHome Assistant
#1 judgepd

Hi, I'm after some help with home assistant and have seen a few users mention it before so I thought I'd ask here.

I have managed to get home assistant to pull from the Givenergy API using the code below. It's working, but I'm trying to get the battery in and battery out added to the built in energy view on home assistant. The problem is that these sensors don't appear in the list to choose from. I believe I need to add something to the config to declare them as long-term energy statistics. I thought that I needed to add:

device_class: energy
state_class: total_increasing
unit_of_measurement: "kWh"

However, when I validate the configuration.yaml it just says:

Invalid config for [sensor.template]: [state_class] is an invalid option for [sensor.template]. Check: sensor.template->sensors->out->state_class. (See ?, line ?).

Does anyone have any clue what I'm on about and have any ideas?

  • platform: rest
    name: plant data
    json_attributes:
    - soc
    - dischargeEnergyToday
    - chargeEnergyToday
    resource: https://api.givenergy.cloud/plant/summary
    scan_interval: 300
    value_template: "{{ value_plant.data }}"
    method: GET
    headers:
    Authorization: myapicodeisinhere
  • platform: template
    sensors:
    soc:
    friendly_name: "SOC"
    value_template: "{{ state_attr('sensor.plant_data', 'soc') }}"
    out:
    friendly_name: "Battery out today"
    device_class: energy
    state_class: total_increasing
    unit_of_measurement: "kWh"
    value_template: "{{ state_attr('sensor.plant_data', 'dischargeEnergyToday') }}"
    in:
    friendly_name: "Battery in today"
    device_class: energy
    state_class: total_increasing
    unit_of_measurement: "kWh"
    value_template: "{{ state_attr('sensor.plant_data', 'chargeEnergyToday') }}"
A
#2 anglefire

Try using state_class: measurement
I do use state_class: total_increasing when I create them directly from Node Red for kWh but I've also seen them with this class elsewhere.

A
#3 anglefire

Also you have used out: and in: and soc:

These should be
name: "SOC"
name: "Battery Out today"
name: "Battery In today"
I think! I'm not an expert, but I suspect the error is the name not the class as such. Possibly!

#4 judgepd

I've had another play around and I think it doesn't like my use of value_template but I've got to be honest I've managed to cobble together what I've done so far from a combination of the official guides and some googling. It seems like value_template should be state:... instead but if I try that then it breaks completely. I think it's working as it is more by luck than judgement as the battery in and battery out seem to almost 'belong' to the SOC sensor somehow.

I think I need to do more reading.

A
#5 anglefire

Hum.

Ok, try something like this:

  - sensor:
     - name: "Battery out today"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: measurement
        state: "{{ state_attr('sensor.plant_data', 'dischargeEnergyToday') }}"
    - name: "Battery in today"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: measurement
        state: "{{ state_attr('sensor.plant_data', 'chargeEnergyToday') }}"
#6 judgepd

Thanks for the help, I've had another crack and it seems to be pretty much working now as the sensors and values are available correctly. I now understand that the api call creates an initial sensor and then the other 3 sensors are created from this one. It also lets me select the battery in and out values in the energy config page of home assistant, although they are both showing 0 at the moment rather than their actual values but I wonder if they will reset for tomorrow.

Edit: It's working perfectly now, I just needed to wait for it to gather a couple of snapshots of data.

Current config:

  • platform: rest
    name: givenergy
    json_attributes:
    - soc
    - dischargeEnergyToday
    - chargeEnergyToday
    resource: https://api.givenergy.cloud/plant/summary
    scan_interval: 300
    value_template: 'OK'
    method: GET
    headers:
    Authorization: myapikeygoeshere

template:
- sensor:
- name: "SOC"
unit_of_measurement: "%"
state: "{{ state_attr('sensor.givenergy', 'soc') }}"


- sensor:
  - name: "Battery out today"
    unit_of_measurement: "kWh"
    device_class: energy
    state_class: total_increasing
    state: "{{ state_attr('sensor.givenergy', 'dischargeEnergyToday') }}"
   
- sensor:
  - name: "Battery in today"
    unit_of_measurement: "kWh"
    device_class: energy
    state_class: total_increasing
    state: "{{ state_attr('sensor.givenergy', 'chargeEnergyToday') }}"