Problem running a script via command_line sensor

So I am trying to run a script that returns the time of last boot (I know there’s a system monitor platform and resource that returns that but I don’t like the format and it comes in as a state not an attribute so I can’ reformat it)

I have this:

sensor:
# Up since
  - platform: command_line
    name: System Uptime
    command: /config/uptimemeas.sh
    scan_interval: 60
# Last Boot
  - platform: command_line
    name: Last Boot
    command: /config/lastboot.sh
    scan_interval: 600

The uptimemeas.sh works but the lastboot.sh one does not.

Here are the scripts:

#!/bin/bash
uptime | awk -F'( |,|:)+' '{print $6,$7",",$8":"$9}'

and

#!/bin/bash
who -b | awk -F'( |,|:|-)+' '{print $6"/"$5"/"$4", "$7":"$8}'

Both are executable with the same permissions and work from ssh…

What am I missing here??

ah it seems who is not available in the ssh container
might need to use uptime --last instead and parse that

uptime --last

doesn’t work on all systems, eg ubuntu 18.04

uptime -s

seems to work though and give the info required.

doesn’t work in the ssh addon… I am looking for the date and time of the last boot

Is that on hassio? Does that run busybox for system tools like uptime?

yes hassio community addon. it seems to run some

so those commands just arent avail;able with the switches I need to do what I want to do.
It just ignores and command line modifiers after uptime.

image

I’d just like this to display as 12/11/2018

I am thinking you could use a template perhaps to format your output.

Or a script should be able to split that string up and spit it out again.

Tried looking at a template but I can’t work it out. I haven’t played at all with scripts…

My scripting is not what it was, I just tried for you and failed.

1 Like

I’ve been looking at the Jinja manual and can’t see how to do it and as I said I have not used scripts. Doing it with the linux script was exactly what I wanted but the docker container doesn’t give me that command option which is pretty weird actually. Runs on the host but not in the docker container. Actually I probably should check in a console in the container in Portainer to be sure…

What command are you running to get “2018-11-21” ??

sensor:
  - platform: systemmonitor
    resources:
    - type: disk_use
      arg: /
    - type: disk_free
      arg: /
    - type: disk_use_percent
      arg: /
    - type: memory_use_percent
    - type: memory_use
    - type: memory_free
    - type: swap_use_percent
    - type: load_1m
    - type: load_5m
    - type: load_15m
    - type: processor_use
    - type: ipv4_address
      arg: 'eno1'
    - type: network_in
      arg: 'eno1'
    - type: network_out
      arg: 'eno1'
    - type: last_boot

also confirm same result if I run a console in the container in Portainer

This very poor script converts 2018-11-22 to 22/11/2018

pi@pihole:~ $ cat test.sh
TEMP=$1
YEAR=`echo $TEMP|cut -d \- -f 1`
MONTH=`echo $TEMP|cut -d \- -f 2`
DAY=`echo $TEMP |cut -d \- -f 3`
echo $DAY\/$MONTH\/$YEAR

In action:

pi@pihole:~ $ ./test.sh "2018-11-12"
12/11/2018

Run the script with the input parameter the string you want to convert, it outputs the string you desire.

Explanation:

Line 1: sets a temporary variable (TEMP) to the first parameter on the command line

Line 2: passes that string through cut which (using - as a parameter, -d -) takes the first field (-f1) and assigns it to a variable named YEAR

Lines 3 & 4 do the same for MONTH and DAY.

Line 5 echos the DAY MONTH and YEAR with / between them.

So I can create the script and then presumably an automation to run it on startup… but how do I parse the state of the sensor to the script in an automation?

Yeah thats the next step…