RESTful API - Switch doesn't show up in the overview tab?

I’m working on getting my irkit configured with hass via the RESTful API, and I think I’m close, but the switch doesn’t show up in the overview tab.

My configuration code is below. (I’ve removed the IP and IR data as it’s long) The configuration validation tool returns “configuration valid!”, but I don’t see the switch in the overview tab. Any idea what I’m doing wrong?

switch:
  - platform: rest
    name: BedroomLights
    resource: http://192.168.1.##/messages
    body_on: '{"format":"raw","freq":38,"data":[IR_DATA_HERE]}'
    body_off: '{"format":"raw","freq":38,"data":[IR_DATA_HERE]}'
    headers: 
      X-Requested-With: curl
      Content-Type: application/x-form-urlencoded

Does the switch show up on the States page? Do you see any errors when you restart HA? If it shows up on the States page, and you don’t see any errors, is it possible you’ve customized your UI and you need to specifically add this new device?

1 Like

Thanks for the reply!

No, it does not show up on the states page, and the only error I get when I start HA is “timer got out of sync. Resetting.”

(That error has been around for a while though, haven’t had a chance to look into it yet.)

I don’t think I’ve customized the UI? How would I know? I’ve only been poking around with Hass for a couple days so far, and gotten a couple other devices hooked up.

Regarding the “timer got out of sync” error, yeah, that seems to happen from time to time, and more so recently. AFAIK it pretty much means that at times there are too many events for the system to process and still update the timer every second. Not a lot can be done, other than doing less, or getting faster harder, I guess. But, in any case, that should have nothing to do with this issue.

If it’s not showing up on the States page, then there’s probably something wrong with your configuration, even though you’re not seeing errors.

Have you changed the logger settings? You might want to check out Logger to make sure you can see DEBUG and higher messages (although that should be the default.)

Oh, where are you looking for messages? If you’re only looking at the Info page in the frontend, you might want to look at home-assistant.log in the config directory. That gives a lot more detail.

1 Like

Ah, the sync error is probably because I’m running it on a gen1 pi then. Maybe it’s time to upgrade…

I haven’t changed the logger settings, and here’s the home-assistant.log output from the last startup:

2018-09-10 10:33:25 WARNING (MainThread) [homeassistant.setup] Setup of recorder is taking over 10 seconds.
2018-09-10 10:33:57 WARNING (MainThread) [homeassistant.setup] Setup of frontend is taking over 10 seconds.
2018-09-10 10:34:13 ERROR (MainThread) [homeassistant.components.switch.rest] Got non-ok response from resource: 400
2018-09-10 10:34:19 WARNING (MainThread) [homeassistant.setup] Setup of config is taking over 10 seconds.
2018-09-10 10:34:50 WARNING (MainThread) [homeassistant.components.sensor] Setup of platform yr is taking over 10 seconds.
2018-09-10 10:35:55 WARNING (SyncWorker_1) [netdisco.ssdp] Error fetching description at http://192.168.1.1:8200/rootDesc.xml
2018-09-10 10:35:59 ERROR (MainThread) [homeassistant.core] Timer got out of sync. Resetting
2018-09-10 10:35:59 WARNING (MainThread) [homeassistant.helpers.entity] Update of media_player.sharp_aquos_tv is taking over 10 seconds
2018-09-10 10:35:59 WARNING (MainThread) [homeassistant.components.media_player] Updating aquostv media_player took longer than the scheduled update interval 0:00:10
2018-09-10 10:36:05 ERROR (MainThread) [homeassistant.core] Timer got out of sync. Resetting
2018-09-10 10:36:09 ERROR (MainThread) [homeassistant.core] Timer got out of sync. Resetting
2018-09-10 10:36:10 WARNING (MainThread) [homeassistant.components.media_player] Updating aquostv media_player took longer than the scheduled update interval 0:00:10
2018-09-10 10:36:21 WARNING (MainThread) [homeassistant.helpers.entity] Update of media_player.marantzamp is taking over 10 seconds
2018-09-10 10:36:21 WARNING (MainThread) [homeassistant.components.media_player] Updating aquostv media_player took longer than the scheduled update interval 0:00:10
2018-09-10 10:36:21 WARNING (MainThread) [homeassistant.components.media_player] Updating denonavr media_player took longer than the scheduled update interval 0:00:10
2018-09-10 10:36:32 WARNING (MainThread) [homeassistant.components.media_player] Updating aquostv media_player took longer than the scheduled update interval 0:00:10
2018-09-10 10:36:32 WARNING (MainThread) [homeassistant.components.media_player] Updating denonavr media_player took longer than the scheduled update interval 0:00:10
2018-09-10 10:36:34 ERROR (MainThread) [homeassistant.components.device_tracker] Unable to load /config/known_devices.yaml: Config file not found: /config/known_devices.yaml
2018-09-10 10:36:34 ERROR (MainThread) [homeassistant.core] Timer got out of sync. Resetting
2018-09-10 10:36:43 WARNING (MainThread) [homeassistant.components.media_player] Updating aquostv media_player took longer than the scheduled update interval 0:00:10
2018-09-10 10:36:53 WARNING (MainThread) [homeassistant.components.media_player] Updating aquostv media_player took longer than the scheduled update interval 0:00:10
2018-09-10 10:37:04 WARNING (MainThread) [homeassistant.components.media_player] Updating aquostv media_player took longer than the scheduled update interval 0:00:10
2018-09-10 10:37:15 WARNING (MainThread) [homeassistant.components.media_player] Updating aquostv media_player took longer than the scheduled update interval 0:00:10
2018-09-10 10:37:36 WARNING (MainThread) [homeassistant.helpers.entity] Update of media_player.sharp_aquos_tv is taking over 10 seconds

There’s your problem. Error 400 means bad/invalid request. So your resource: value must be incorrect, or the server doesn’t like a GET request to that endpoint.

1 Like

FWIW, I’m running on a Pi3B and I still see the out of sync errors. In fact, they seemed to increase since I updated to the latest HA release.

1 Like

Aha, so hass sends a get request as part of configuring the device at startup? Is there some way I can modify the GET request it sends? The device responds properly to a GET request, as you can see from the command line output below:

C:\Users\####>curl -i "http://192.168.1.8/messages" -H "X-Requested-With: curl"
HTTP/1.0 200 OK
Access-Control-Allow-Origin: *
Server: IRKit/3.0.0.0.g85190b1
Content-Type: text/plain

{"format":"raw","freq":38,"data":[2064,4554,382,1567,205,52381,486,4107,205,45647,262,13230,454,4713,345,9707,262,13230,345,10047,205,11529,333,3119,280,8173,333,1622,333,11139,345,3013,205,4878,486,3119,357,65535,0,8755,205,14172,409,14172,271,16263,271,41171,220,65535,0,65535,0,1514,262,62212,262]}

No, not at startup.

According to the RESTful Switch doc page, for every periodic update it will send a GET request to the URL given by the resource parameter to get the current state of the switch. And to change the state of the switch (i.e., turn it on or off), it will send another request to the same URL, where the method used (POST or GET) is defined by the method parameter (POST being the default.)

If the RESTful Switch is too limiting in your case, then you might want to try the Command line Switch instead, in conjunction with curl.

1 Like

Thanks a lot for pointing me in the direction of the Command Line Switch, looks like it’s the right tool for the job. I think I’m getting closer… but it seems the nested quotes are messing things up. In particular, the single quote after the -d before {“format” seems to be causing issues, but I don’t know what to replace it with to make the code work. Any ideas how I can fix this?

switch:
  - platform: command_line
    switches:
    BRLIght:
      command_on: '/usr/bin/curl -i "http://192.168.1.8/messages" -H "X-Requested-With: curl" -d '{"format":"raw","freq":38,"data":[6881,3458,904,815,904,815,904,2537,904,2537,904,904,904,2537,904,904,904,815,904,815,904,2537,904,815,904,815,904,2537,904,904,904,2537,904,815,904,2537,904,815,904,815,904,2537,904,904,904,904,904,904,904,815,904,815,904,815,904,2537,904,2537,904,904,904,2537,904,904,904,815,904,2537,904,815,904,2537,904,815,904,815,904,2537,904,904,904,761,904]}''
      command_off: '/usr/bin/curl -i "http://192.168.1.8/messages" -H "X-Requested-With: curl" -d '{"format":"raw","freq":38,"data":[6881,3341,873,873,873,873,873,2537,873,2537,873,873,873,2537,873,873,873,873,873,873,873,2537,873,873,873,873,873,2537,873,873,873,2537,873,873,873,2537,873,873,873,873,873,2537,873,873,873,873,873,873,873,873,873,873,873,2537,873,2537,873,2537,873,873,873,2537,873,873,873,873,873,2537,873,2537,873,2537,873,873,873,873,873,2537,873,873,873,787,873]}''
      friendly_name: Bedroom Lights

For YAML, to include a single quote in a string quoted by single quotes, use two single quotes. So, basically, add another single quote before and after the -d value. Something like:

command_on: '/usr ... -d ''{"format" ... ]}'''
1 Like

Thanks for the suggestion, I tried it but hass doesn’t seem to like it. I’m beginning to wonder if storing the command in a separate file and calling it somehow would work better?

Here’s the output from the configuration validation tool:

Invalid config for [switch.command_line]: expected a dictionary for dictionary value @ data['switches']. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/switch.command_line/

Here’s the config code:

switch:
  - platform: command_line
    switches:
    BRLIght:
      command_on: '/usr/bin/curl -i "http://192.168.1.8/messages" -H "X-Requested-With: curl" -d ''{"format":"raw","freq":38,"data":[6881,3458,904,815,904,815,904,2537,904,2537,904,904,904,2537,904,904,904,815,904,815,904,2537,904,815,904,815,904,2537,904,904,904,2537,904,815,904,2537,904,815,904,815,904,2537,904,904,904,904,904,904,904,815,904,815,904,815,904,2537,904,2537,904,904,904,2537,904,904,904,815,904,2537,904,815,904,2537,904,815,904,815,904,2537,904,904,904,761,904]}'''
      command_off: '/usr/bin/curl -i "http://192.168.1.8/messages" -H "X-Requested-With: curl" -d ''{"format":"raw","freq":38,"data":[6881,3341,873,873,873,873,873,2537,873,2537,873,873,873,2537,873,873,873,873,873,873,873,2537,873,873,873,873,873,2537,873,873,873,2537,873,873,873,2537,873,873,873,873,873,2537,873,873,873,873,873,873,873,873,873,873,873,2537,873,2537,873,2537,873,873,873,2537,873,873,873,873,873,2537,873,2537,873,2537,873,873,873,873,873,2537,873,873,873,787,873]}'''
      friendly_name: Bedroom Lights

Finally got it working, had some indent and capitalization issues, but once those all got resolved it works!!! Thanks so much for the help.

Code in case anyone else with an irkit ever comes across this with the same questions:

switch:
  - platform: command_line
    switches:
      brlight:
        command_on: '/usr/bin/curl -i "http://192.168.1.8/messages" -H "X-Requested-With: curl" -d ''{"format":"raw","freq":38,"data":[6881,3458,904,815,904,815,904,2537,904,2537,904,904,904,2537,904,904,904,815,904,815,904,2537,904,815,904,815,904,2537,904,904,904,2537,904,815,904,2537,904,815,904,815,904,2537,904,904,904,904,904,904,904,815,904,815,904,815,904,2537,904,2537,904,904,904,2537,904,904,904,815,904,2537,904,815,904,2537,904,815,904,815,904,2537,904,904,904,761,904]}'''
        command_off: '/usr/bin/curl -i "http://192.168.1.8/messages" -H "X-Requested-With: curl" -d ''{"format":"raw","freq":38,"data":[6881,3341,873,873,873,873,873,2537,873,2537,873,873,873,2537,873,873,873,873,873,873,873,2537,873,873,873,873,873,2537,873,873,873,2537,873,873,873,2537,873,873,873,873,873,2537,873,873,873,873,873,873,873,873,873,873,873,2537,873,2537,873,2537,873,873,873,2537,873,873,873,873,873,2537,873,2537,873,2537,873,873,873,873,873,2537,873,873,873,787,873]}'''
        friendly_name: Bedroom Lights