Up Time Sensor for Pi (hello world example)

I have some command_line/template questions.
To learn, I am creating this sensor (UpTime). It is a hello world as I am learning ‘command_line’ and template.

pi@hassbian:/home/homeassistant/.homeassistant $ uptime
22:18:04 up 1 day, 4:18, 1 user, load average: 0.29, 0.20, 0.18

This is my sensor yaml

  • platform: command_line
    name: UpTime
    command: “uptime”
    unit_of_measurement: “time”
    value_template: >
    {%set list1 = value.split(’ ')%}
    {{ list1[0] }}
scan_interval: 1

I have this in my group yaml

view: yes
- sensor.pws_alerts
- sensor.CPU_Temperature
- sensor.UpTime

How would I customize the sensor so I can see the all of it. Right now the circle is cutting off part of it.

If you want to see the entire string including time, users and load then you can simplify your sensors.yaml as that is the default behaviour for this sensor:

- platform: command_line
  name: UpTime
  command: "uptime"
  scan_interval: 1

I use the “uptime -p” command to just get server uptime and ignore the other fields. This command returns a string of the format “up 1 day, 1 hour, 42 minutes”. I then use the value_template to strip off the "up " from the beginning of the line by ignoring the first three characters. I also set scan_interval to 1 minute to cut down unnecessary updates. So in my sensor.yaml I have:

- platform: command_line
  name: Hassbian Uptime
  command: "uptime -p"
  value_template: "{{ value[3:] }}"
  scan_interval: 60

and this in customise.yaml to give it a nice icon:

  friendly_name: Hassbian Uptime
  icon: mdi:clock

Thanks @PeteB for the tip .
This started as a "hello world " learning opportunity, but might become useful .
So how does unit_of_measure affect output and where is there a list? Without UOM when I clicked on the icon it had a red error . Not sure how that all fits to the front end .

Good question, I’ve been looking at that lately as well. In this case “uptime” would arguably be a timedelta rather than time uom. I have seen HA UI use uom in three ways: 1) append a postfix, 2) decide type of history graph (i.e. one vs two dimensional, and, 3) consolidate history 2d graphs for values using same uom. There is also the units_system attribute that seems to help select (coerce ?) values into the desired uom.

I’d be interested to learn of other uses if others have more insight?

And get a list of supported ones. :slight_smile: