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: