Command line sensor `service xxx status` not updating when service stopped

I’m using a command_line sensor to monitor a status of a service running on my system. It works ok when system is starter or the monitored service is restarted but it does not update when the service is stopped (which is the most interesting event for me). My config is the following:

sensor:
  - platform: command_line
    name: "CA350 service status"
    command: "service ca350 status"
    scan_interval: 30

The command line service ca350 status returns ca350 is not running. or ca350 is running as pid xxx. where xxx is the pid.
I have also just realized that there is an error in the log when the service is stopped:

Logger: homeassistant.components.command_line
Source: components/command_line/__init__.py:41
Integration: command_line (documentation, issues)
First occurred: 2:49:19 PM (58 occurrences)
Last logged: 3:08:20 PM

Command failed: service ca350 status

I have also tried with command line binary sensor:

binary_sensor:
  - platform: command_line
    command: 'service ca350 status'
    value_template: >-
      {% if value[6:16] == 'is running' %}
        running
      {% else %}
        stopped
      {% endif %}
    payload_on: 'running'
    payload_off: 'stopped'
    name: CA350 service binary

Again, when the service is running it works ok but when it is stopped the error in the log is written and sensor is not updated:

Logger: homeassistant.helpers.entity
Source: helpers/template.py:567
First occurred: 4:06:20 PM (1 occurrences)
Last logged: 4:06:20 PM

Update for binary_sensor.ca350_service_binary fails
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/entity.py", line 278, in async_update_ha_state
    await self.async_device_update()
  File "/srv/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/entity.py", line 474, in async_device_update
    raise exc
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.8/site-packages/homeassistant/components/command_line/binary_sensor.py", line 108, in update
    value = self._value_template.render_with_possible_json_value(value, False)
  File "/srv/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/template.py", line 535, in render_with_possible_json_value
    return run_callback_threadsafe(
  File "/usr/local/lib/python3.8/concurrent/futures/_base.py", line 432, in result
    return self.__get_result()
  File "/usr/local/lib/python3.8/concurrent/futures/_base.py", line 388, in __get_result
    raise self._exception
  File "/srv/homeassistant/lib/python3.8/site-packages/homeassistant/util/async_.py", line 53, in run_callback
    future.set_result(callback(*args))
  File "/srv/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/template.py", line 567, in async_render_with_possible_json_value
    return self._compiled.render(variables).strip()
  File "/srv/homeassistant/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/srv/homeassistant/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/srv/homeassistant/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: 'NoneType' object is not subscriptable

Any suggestion how to get the status of the service when it is not running instead of the error? Or can I trigger an automation based on this error entry?

I think I know where the problem is but don’t know how to solve it. The service xxx status command returns exit code 1 when the service is not running which is probably interpreted by HA as failed call. Is there a way to overcome it?

OK, so I’ve implemented a workaround: created a shell script that is checking service status and returns its status. This always returns exit code 0 so no issue on HA side.

#!/bin/sh
if service ca350 status>nul; then
        echo running
else
        echo stopped
fi

and the binary sensor is also simpler:

binary_sensor:
  - platform: command_line
    command: '/root/servicetest'
    payload_on: 'running'
    payload_off: 'stopped'
    name: CA350 service
    scan_interval: 30

Any better/more elegant solution out there?