Appdaemon error setting input_boolean state

The following lines of code are generating an error and I’m not sure why.

        level=ptrdict[sys]["marker"][component]["tonerlow"]["value"]
        self.log("component {} - input_boolean level={}".format(component,level),"INFO")
        self.set_state(component,state=level)                 # set ha value for input boolean

Here is the appdaemon.log output related to the error

2017-01-17 15:23:21.532100 INFO printermonitor: component input_boolean.ofhp1tonerlow - input_boolean level=0
2017-01-17 15:23:21.558866 WARNING Logged an error to /home/hass/appdaemon/appdaemon.err

Here are the lines from the appdaemon.err file related to the error.

2017-01-17 15:23:21.557585 WARNING Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 422, in worker
    function()
  File "/home/hass/code/appdaemon/printermonitor.py", line 19, in initialize
    self.check_printers()
  File "/home/hass/code/appdaemon/printermonitor.py", line 96, in check_printers
    self.set_state(component,state=level)                 # set ha value for input boolean
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appapi.py", line 177, in set_state
    r.raise_for_status()
  File "/usr/local/lib/python3.4/dist-packages/requests/models.py", line 893, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http://localhost:8123/api/states/input_boolean.ofhp1tonerlow

If I enter in the HTTP url in the browser, I get back the information on the appropriate component. So I’m a little confused.

Couple of things that may be at issue:

  1. An input boolean can only be “on” or “off”, not “0”.

  2. If you actually want to toggle it you need to call a service to do so, not set its state, e.g.

self.turn_on(component)

However, looking at your code I am not sure exactly what you are trying to do here - looks like you are working with custom attributes - either way, you probably can’t set the state to “0”.

Ahhh, ok
I’ll give that a try. I’m using it as a flag to show if a printer is low on toner or not. The return from the SNMP query is a 0 or a 1 and I was just passing it, thinking it was like a true or false kind of thing.

Yep that worked.
Thanks