Command line sensor configuration problem

I’m trying to setup a command line sensor, and I’m getting some errors in the log that I don’t understand. I was following instructions in this thread about motion sensing with IP cameras, and I’m using the same syntax as he is, and it appears to match the documentation:

# Camera motion Sensors
- platform: command_line
  name: Ian Motion
  command: "python3 /home/hass/foscam.py 'getMotionAlert' '192.168.1.142' 'username' 'password'"
  value_template: '{{ value == "2" }}'
  scan_interval: 2

In the log file, there’s this:

16-10-31 16:05:12 homeassistant.components.sensor.command_line: Command failed: python3 /home/hass/foscam.py 'getMotionAlert' '192.168.1.142' 'username' 'password'
16-10-31 16:05:12 homeassistant.components.sensor: Error while setting up platform command_line
Traceback (most recent call last):
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/helpers/entity_component.py", line 151, in _async_setup_platform
    entity_platform.add_entities, discovery_info
  File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
    value = future.result()
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/sensor/command_line.py", line 45, in setup_platform
    add_devices([CommandSensor(hass, data, name, unit, value_template)])
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/sensor/command_line.py", line 60, in __init__
    self.update()
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/sensor/command_line.py", line 84, in update
    value, 'N/A')
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 110, in render_with_possible_json_value
    error_value).result()
  File "/usr/lib/python3.4/concurrent/futures/_base.py", line 402, in result
    return self.__get_result()
  File "/usr/lib/python3.4/concurrent/futures/_base.py", line 354, in __get_result
    raise self._exception
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/util/async.py", line 166, in run_callback
    future.set_result(callback(*args))
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 127, in async_render_with_possible_json_value
    variables['value_json'] = json.loads(value)
  File "/usr/lib/python3.4/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'NoneType'

It appears to be crapping out on the value piece, but I’m not sure why.

Just a guess but is the value a string or an integer? You might need a float.

In foscam.py, each function prints the value. For example:

def getMotionStatus(ip, usr, pwd):
	url = "http://%s:88/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&usr=%s&pwd=%s" %(ip,usr,pwd)
	response = requests.get(url)
	doc = xmltodict.parse(response.content)
	result = doc['CGI_Result']['isEnable'][0]
	print(result)

So it should be a string. I also tested this from the command line, and it does indeed just print out a single integer to stdout.

One last thought would be to remove the quotes:

value_template: '{{ value == 2 }}'

I went ahead and tried that, but I’m getting the same error.

I’m not sure if this is accurate, but the TypeError: the JSON object must be str, not 'NoneType' part of the error message makes me wonder if it’s getting anything back when it runs the command. But like I said, I copy-pasted that command into the terminal (in the same virtualenv that HA uses) and it works fine.

Edit: I tried forcing it to be an Int, that didn’t work either, still the same errors:

value_template: '{{ int(value) == 2 }}'

Does HA have the proper permissions to run the .py file?

I believe so; I can successfully run the command as the ‘hass’ user, in the HA virtual environment.

@rogersmj was your error fixed? Facing the similar issue here… wondering if there is any fix for this…