Switch created with API cannot be used with services API

Hi!

I noticed that if I create new switch with curl it cannot be used with services API. Could anyone confirm if this is a known problem or do I always need to create switches with configuration.yaml to get them working?

command to create new switch:
curl -v -X POST -H "Content-Type: application/json" -d '{"state":"off"}' http://localhost:8123/api/states/switch.testing123

reply from HASS:
{"attributes": {}, "entity_id": "switch.testing123", "last_changed": "2017-07-14T20:19:57.377837+00:00", "last_updated": "2017-07-14T20:19:57.377837+00:00", "state": "off"}%

command to toggle state of the switch I just created:
curl -v -X POST -H "Content-Type: application/json" -d '{"entity_id":"switch.testing123"}' http://localhost:8123/api/services/switch/toggle

reply from HASS:
[]%

If i toggle state of switch that has been created with configuration.yaml the reply from HASS is as it should be (return the state of the switch).

Hey @Jayden

Did you ever find a solution? When the switch is created it doesn’t have a button to toggle.

Using api/states is creating a state in the state machine, it’s not actually creating an entity. Services operate on entities, not states in the state machine.

You can find more information about this here and here.

1 Like

Hi there!

I’ve bypassed the REST API problem by using Python API and remote.set_state -function to trigger states to on and off. Autimation takes care of the the rest when it detects state changes.

I have a setup where I have about 96 inputs and outputs on RPi via I2C bus. No GPIO pins are used. It takes care of basically everything in my house.

It’s basically a wonder that I haven’t found anyone else doing such stuff via I2C bus in similar manner. I needed to build custom python component and service to take care of all this interaction between Home Assistant and buttons/switches.

It’s quite stable nowadays, but would need bit updating, I’m sure :slight_smile: