Another widget for battery level

Here’s another widget for displaying the battery levels for devices. It is a replacement for the standard oh-label-item with the following features:

  • Warn if the battery level is below a limit
  • Warn if the value has not been updated for a specific time

There are two configurable levels (warn - orange and error red) that can be set both on the battery level and the number of hours since the last update.

You need to have two items for the widget configuration; one for the actual device (with a battery level) and one for the time for the last update.

image

Inspiration came from Battery Status List, but I wanted a similar layout as oh-label-item as well as being warned if the device is not responding.

There is some info on how to get the time of last update (or change) here, Design Pattern: Time of Last Update

Changelog

Version 0.1

  • initial release

Resources

uid: mhn-batterylevel-label-item
tags:
  - label-item
  - battery
props:
  parameters:
    - description: Title for the widget
      label: Title
      name: title
      required: false
      type: TEXT
    - context: item
      description: Battery level item
      label: Item
      name: item
      required: false
      type: TEXT
    - context: item
      description: Item for last update
      label: Last update item
      name: lastUpdate
      required: false
      type: TEXT
    - default: "25"
      description: Warning when battery level is below
      label: Warning level
      name: warning
      required: false
      type: INTEGER
      min: 0
      max: 100
    - default: "10"
      description: Error when battery level is below
      label: Error level
      name: error
      required: false
      type: INTEGER
      min: 0
      max: 100
    - default: "8"
      description: Number of hours without update until it is a warning
      label: Number of hours until warning
      name: warningTime
      required: false
      type: INTEGER
      min: 0
      max: 100
    - default: "24"
      description: Number of hours without update until it is an error
      label: Number of hours until error
      name: errorTime
      required: false
      type: INTEGER
      min: 0
      max: 100
  parameterGroups: []
timestamp: Jan 16, 2022, 5:29:08 PM
component: oh-list-item
config:
  title: = props.title
  style:
    color: '=(Number.parseFloat(items[props.item].state) > props.warning && dayjs(items[props.lastUpdate].state).add(props.warningTime, "hour").isAfter(now()) ) ? "black" : (Number.parseFloat(items[props.item].state) > props.error && dayjs(items[props.lastUpdate].state).add(props.errorTime, "hour").isAfter(now()) )  ? "orange" : "red"'
slots:
  after:
    - component: Label
      config:
        text: =items[props.item].state
        style:
          color: '=(Number.parseFloat(items[props.item].state) > props.warning && dayjs(items[props.lastUpdate].state).add(props.warningTime, "hour
").isAfter(now())) ? "gray" : (Number.parseFloat(items[props.item].state) > props.error && dayjs(items[props.lastUpdate].state).add(props.errorTime, "hour").isAfter(now()) ) ? "orange" : "red"'
2 Likes