Using the API to create input helper isn't working as expected

I can create an input helper from a bash script like this:

#!/bin/bash

ha_url="https://ha.local"
ha_token="my_longlived_token"

# Create an input_boolean "foo" with friendly name "Foo helper" and sets the state to "on"
curl -X POST \
    -H "Authorization: Bearer ${ha_token}" \
    -H "Content-Type: application/json" \
    -d '{"state": "on", "attributes": {"friendly_name": "Foo helper"}}' \
    "$ha_url"/api/states/"input_boolean.foo"

I can then update the state using the same script by replacing “state”: “on” with “state”: “off”.

However, I seem unable to change the state from within Home Assistant, I can see the toggle on my dashboard and within the ‘helpers’ page, but when I toggle it from its current state it reverts back again.

Is this expected behaviour? Is there a way to stop it?

If I create the input helper from within Home Assistant itself, rather than via the API, I can change it freely in the dashboard and via the API.

That’s because you didn’t actually make an input helper. The API you are using simply allows you to set state. It takes an id and a state to set and adds it to the state machine with that id. The state machine is basically just a giant hash map for that API, no integration will manage or update the state of entities you create with it.

The domain you use is irrelevant there. Just because you picked input_boolean doesn’t make it an input boolean. It’s not handled by that integration, it’s just a state. It can’t be updated in the UI, added to an area, can’t set its name in the UI and won’t be restored after restart (i.e. the things you would expect a properly made input boolean to do).

That API primarily exists so that you can manually change the state of an entity in the developer tools tab. I wouldn’t recommend it for regular use. You should use the UI to make helpers.

1 Like

Thank you for the explanation.

Now i need to rethink my whole life :grinning:

PS They don’t get deleted when HA restarts, I cant figure out how to delete them. They are not in the “.storage” directory so I’m guessing they are orphaned in the HA database.

Huh that’s strange… Do they actually appear in the UI in the states tab of developer tools? Or do you mean that they still appear in the db?

History is still kept in the db even if it is orphaned because the entity it is removed. Records are only removed when they are older then purge_keep_days. So if that’s what you mean then that’s to be expected. But they shouldn’t show up in the UI after restart…

They persisted after a restart, but were removed at some point as I’ve just checked and they are now gone :man_shrugging: