Homeassistant Statusbar

I want to show in my statusbar the current CO2 value and the change of CO2 in the last minute. My statusbar is waybar with Sway as windowmanager.

So first add a new sensor that shows the change of the CO2 sensor in the last minute to the homeassistant configuration.yaml. This is a Derivative sensor with a time window of one minute.

sensor:
  - platform: derivative
    source: sensor.scd30_co2
    name: CO2 per last minute
    round: 1
    unit_time: min
    time_window: "00:01:00"

This looks like this in homeassistant:

screenshot of derivative sensor in homeassistant

Next we create another sensor that formats the string we want for the statusbar:

sensor:
  - platform: template
    sensors:
      statusbar_co2:
        value_template: "CO2: {{ states('sensor.scd30_co2') }} [{{'%+3.2f'|format(states('sensor.co2_per_last_minute')|float)}}]"

The value_template is in jinja syntax and the value in the bracket is forced to have a sign in front (+ or -) using a Python-style format string. The result looks for example like this: CO2: 723.3 [+7.00].

Now we need to get the value from the homeassistant API to show them in the statusbar. Prerequisite is a "Long-lived access token" which we can generate on the profile view of homeassistant.

The curl and jq line to get the "statusbar_co2" sensor:

curl \
  -s -H "Authorization: Bearer YOUR_LONG_LIVED_TOKEN" \
  -H "Content-Type: application/json" http://localhost:8123/api/states | \
  jq -r '.[] | select(.entity_id == "sensor.statusbar_co2") | .state'

The jq command filters the list of entities to the "statusbar_co2" entity and returns the state value. The -r removes the quotes around the result of jq. I put the curl command into co2_status.sh and call it from waybar like this:

"custom/co2": {
   "exec": "/home/user/bin/co2_status.sh",
   "interval": 30
},