Achronite / mqtt-energenie-ener314rt

MQTT interface for Energenie ENER314-RT add-on board for the Raspberry Pi, designed for use by Home Assistant.
MIT License
13 stars 5 forks source link

Home Assistant - battery voltage dynamic icon #17

Closed Achronite closed 1 year ago

Achronite commented 1 year ago

Via MQTT Discovery the v0.1.1 implementation reports the voltage of the battery powered devices in Volts with a fixed battery icon. It would be good if the icon reflected the charge state of the battery.

One option is to convert this to a % value, and then make use of the device_class: battery in Home Assistant. This template seems to work for 3V eTRV in the editor:

template:
  - sensor:
      - name: "Battery % 4280"
        unit_of_measurement: "%"
        device_class: battery
        availability: "{{ states('sensor.radiator_4280_voltage')|is_number }}"
        state: >
          {% set batt_volts = states('sensor.radiator_4280_voltage')|float(0) %}
          {% set batt_pct = 111.11 * batt_volts - 233.33 %} {# calculate percent from the voltage #}
          {% set batt_pct_clamped = ([0, batt_pct, 100]|sort)[1]|round(0) %} {# clamp the output to a 0-100% range and round to full number #}
          {{ batt_pct_clamped }}

I'm not sure how best to automatically add and use this template in Home Assistant. Anybody?

Achronite commented 1 year ago

Another (simpler) option would be to calculate the % in my code from VOLTAGE and report this as a new topic in MQTT, either: battery/state or VOLTAGE/percent or even replace VOLTAGE/state with the % value

djwillis commented 1 year ago

Fairly sure this could be put into a _template (value or state) of sorts, but I can't get it working and not sure on the exact syntax.

That said, is the simplier option not actually better overall anyway? Well the code has to sit in the app but at least it is 100% abstract from anything upstream so easy to tweak calculations.

mqtt-energenie-ener314rt is already provising a bunch of extra fuctionality over the lib so having this type of stuff built in seems in keeping with that and also has the added benifit of being easilly avalable to anyone using this from MQTT not just HA? - I already use this to feed data into an archiver that has nottihng to do with Home Assistant, along with HA of course.

Something as simple as this should then work for getting it into HA once the code is in the app.

  "unit_of_measurement": "%",
  "device_class": "battery",
  "state_topic": "energenie/3/7575/BATTERY/statel",

At that point, there would be nothing to stop you adding something like

  "entity_category": "diagnostic",
  "enabled_by_default": false,

To other less critical settings like voltage, and maybe more of the TRV values. Then they will appear as hidden values under the Diagnostic control.

image

In fact, adding "entity_category": "diagnostic", to the TRV Maintainance but ensuring it is not hidden may also be an idea?

Achronite commented 1 year ago

The calculation on battery % seems very much an art rather than science, so I'm not 100% convinced of the value now. If I do add it I'll probably do it via a calculation in my mqtt code; the templates just seem a bit fiddly to work with.

Good idea on the diagnostics, I'll think I'll move a few values under there (ERRORS, ERROR_TEXT, etc) in the next release.

Achronite commented 1 year ago

I've decided to add a battery level calculation. I've added it to the eTRV and Whole house energy monitor for now.

This is the calculation I've used based on number of 1.5v batteries in each device and voltage (v)

if (v >= (1.55*batteries)){
    charge = 100;
} else if (v <=0 ){
    charge = 0;
} else if (v > (1.4*batteries)){
    charge = 60.6*v/batteries + 6;
} else if ( v < (1.1*batteries)){
    charge = 8.3*v/batteries;
} else {
    charge = 9412 - 23449*(v/batteries) + 19240*(v*v/batteries) - 5176*(v*v*v/batteries); // cubic regression
}