custom-cards / custom-card-helpers

Custom Card Helper Functions/Types for Developers
https://custom-cards.github.io/custom-card-helpers/
MIT License
48 stars 29 forks source link

Looking for help solving this long-time conversion issue (can't find example code to learn from) #51

Open ironsheep opened 2 years ago

ironsheep commented 2 years ago

Hi, I'm back and still just as unsuccessful. re: (my #34)

For more than a year now I've had the interval since the last update not showing up on my card : https://github.com/ironsheep/lovelace-rpi-monitor-card

It was working long ago but changes to the underlying helper methods stopped the display from appearing. You can see the value in the screenshots at the repo. It was working... I'd like to get it working again.

And... it seems (#42) also pointed out this issue...

The issue is that I've spent hours twice now trying to figure out how to call these methods.

The 2nd time the parameters have changed yet again. Can someone please provide examples for how to call these methods in the latest version of these helpers... ??

I even found https://custom-cards.github.io/custom-card-helpers/ which is pretty but does not show any example invocations of any of the methods.

I'm attempting to use computeStateDisplay() and relativeTime()

I apologize if I'm new enough that you deem that this was obvious. Please feel free to send me to examples to study.

Here's what I think I should be doing but it doesn't compile due to the new third parameter data type changing. I'm trying to make the change but am unsuccessful.

private _getRelativeTimeSinceUpdate(): string {
    const stateObj = this._config.entity ? this.hass.states[this._config.entity] : undefined;
    const stateStrInterp = computeStateDisplay(this.hass?.localize, stateObj!, this.hass?.locale);
    const relativeInterp =
      stateStrInterp === undefined ? '{unknown}' : relativeTime(new Date(stateStrInterp), this.hass?.localize);
    const desiredValue = this._sensorAvailable ? relativeInterp : '{unknown}';
    return desiredValue;
  }

  private _getMinutesSinceUpdate(): number {
    const stateObj = this._config.entity ? this.hass.states[this._config.entity] : undefined;
    const stateStrInterp = computeStateDisplay(this.hass?.localize, stateObj!, this.hass?.locale);
    const then = new Date(stateStrInterp);
    const now = new Date();
    let diff = (now.getTime() - then.getTime()) / 1000;
    diff /= 60;
    return Math.abs(Math.round(diff));
  }

The issue is I don't know from where to get the locale value for the 3rd parameter of computeStateDisplay() since there is one in hass (HomeAssistant) I'm trying to use it but the value can come back as undefined which now fails to compile as you might expect.