No Historical Statistics from Command Line Sensor

Hi all,

I’ve been hitting my head against the wall with this for at least a week so I’m turning to the experts.

My ISP recently broke the integration I used to track usage against my monthly data cap. To combat this, I’ve setup 3 command line sensors that run ssh commands against my pfSense firewall to collect monthly and daily usage via vnstat. The command is formatted to export the values in Bytes, then convert to Gigabytes using a value_template formula.

  • Sensor 1 (datacap_mtd_gb) collects the value every hour to give me a running total in GB of how much data I’ve used for the month.
  • Sensor 2 (datacap_mtd_percent) is basically identical to sensor 1, but then divides the number by my ISP data cap to give me the utilization in %.
  • Sensor 3 (datacap_daily_gb) collects the daily value. This sensor is configured with a scan_interval of 2 weeks, but is updated daily at 11:55PM via an automation task. The goal of this sensor is to collect the daily data usage once every day so I can use a statistics graph to display the daily usage for the last 14 days in bar form.

Each of the sensors work flawlessly; data is collected and I can see the updates when I view the entities in the history view. However, when I go to select the entity of sensor 3 (daily usage) as the source for a statistics graph card, the entity does not appear as an option. If I manually add the entity in the card’s text editor, the graph just doesn’t display anything at all (and I mean nothing, not just that the data is missing, the card is 100% blank). For reference, below is a picture of what I’m trying to emulate on my dashboard:

Troubleshooting steps taken so far:

  • Using the template sensor in more variants than I can remember, to no avail.
  • Using the customize functions to modify the device_class and/or state_class (in each iteration/combination I can think of) to “measurement”, to no avail.
  • Someone on Discord mentioned I should try installing InfluxDB and Grafana, so I did, with no include or exclude parameters. I can confirm that the configuration is correct because all my entities are being recorded in Influx EXCEPT my command_line sensors.

I’m starting to think that either this is a configuration issue in my sensor or that this is simply a limitation of command_line sensors. Copied below are the configurations of each of the three sensors. Please note that this is the contents of my broken out sensor.yaml file which is why the “sensor:” heading is missing.

# Monthly ISP Data Cap. Usage (GB) from pfSense
- platform: command_line
  name: "Data Cap. Usage GB MtD"
  unique_id: datacap_mtd_gb
  scan_interval: 3600
  command: >-
    ssh -o UserKnownHostsFile=/config/dzresources/ssh_hosts [email protected] -i /config/dzresources/pfha_id_rsa 'vnstat -i wan1 --oneline b' | cut -d';' -f 11
  unit_of_measurement: "GB"
  # Convert from B to GB
  value_template: '{{ value | multiply(0.000000001) | round(2) }}'

# Monthly ISP Data Cap. Usage (%) from pfSense
- platform: command_line
  name: "Data Cap. Usage % MtD"
  unique_id: datacap_mtd_percent
  scan_interval: 3600
  command: >-
    ssh -o UserKnownHostsFile=/config/dzresources/ssh_hosts [email protected] -i /config/dzresources/pfha_id_rsa 'vnstat -i wan1 --oneline b' | cut -d';' -f 11
  unit_of_measurement: "%"
  # Convert from B to GB, then convert to % of data cap (1250GB)
  value_template: '{{ (value | multiply(0.000000001)/1250*100) | round(1) }}'

# Daily ISP Data Usage from pfSense, collected at 11:55PM each day via automation task
- platform: command_line
  name: "Data Usage GB Daily"
  unique_id: datacap_daily_gb
  # Refresh rate: 2 weeks, updated every day at 11:55PM via automation task
  scan_interval: 1209600
  command: >-
    ssh -o UserKnownHostsFile=/config/dzresources/ssh_hosts [email protected] -i /config/dzresources/pfha_id_rsa 'vnstat -i wan1 --oneline b' | cut -d';' -f 6
  unit_of_measurement: "GB"
  # Convert from B to GB
  value_template: '{{ value | multiply(0.000000001) | round(2) }}'

Note on the SSH command: the vnstat command outputs a string of values related to the data you’ve requested, separated by semicolons (";"). The section at the end of the commands listed above cuts the string into individual values, then selects the value I’m interested in (monthly totals are value 11, daily totals are value 6).

I would prefer to avoid MQTT (if possible) as well as changes on my firewall itself (after all, HA is able to collect the data just fine), but otherwise I’m open to trying just about anything at this point. I’m happy to provide additional information regarding the setup upon request. Any and all assistance is greatly appreciated! Thanks in advance!

Also, here is my InfluxDB config in case it’s relevant:

influxdb:
  host: X.X.X.X #Actual IP redacted, but it's my HA host IP
  port: 8086
  database: homeassistant
  username: homeassistant
  password: <password> #Redacted, but properly entered in config
  max_retries: 3
  default_measurement: state

To have LTS (long term statistics) collected for a sensor it must have a state class of “measurement”. The command line sensor does not support adding this directly in the sensor config but you can add it using customize.

e.g.

configuration.yaml:

homeassistant:
  customize: !include customize.yaml

customize.yaml:

sensor.data_usage_gb_daily:
  state_class: measurement

Hi Tom,

Thanks for the recommendation! I think I’ve tried this before to no avail, but I’ll give it another try. Just to confirm though, shouldn’t the contents of customize.yaml contain the below for my sensor rather than “sensor.data_usage_gb_daily” given the sensor’s defined entity id of “datacap_daily_gb”?

sensor.datacap_daily_gb:
  state_class: measurement

Or is customize.yaml truly supposed to refer to the friendly name with spaces replaced with underscores?

@tom_l I owe you a drink! I switched to using the sensor name in customize.yaml exactly as you mentioned and now I’m getting long term statistics! This must have been the mistake I was making the whole time. Not sure how I didn’t notice that. I’m only seeing recent data points right now, presumably because the config was just updated today, so I’ll keep an eye on it over the next few days and post an update. In either case thank you so much for your assistance!

1 Like

Providing an update as promised; this is now working exactly as intended. Rookie mistake on my part.

Thank you again, @tom_l!

One additional point here for any poor unfortunate soul who is looking to setup something similar and stumbles onto this thread: the entity’s state needs to be reset to 0 before midnight each day for the graph to display properly, otherwise your previous day’s data will spill over into the next day. I’m using this python script as part of an automation task to reset the state to “0” at 11:58PM each day (3 minutes after the actual reading is collected). This way the graph can use the max statistic to display the highest reading from the day.

@dirtycamacho
I was just looking into doing the same thing. thanks for posting your sensors. saves me a bit of time!