Counter set state is not "used", bug?

I created a counter helper.

I set the state in developer tools to 50 as an example.
I then open the more info on the entity and press decrement, I assumed that would mean 49, but I get 0.
Or if I have previously incremented it to 10, I get 9.

Does not state set the counter? When I use an automation and call service I get the same result.

1 Like

I can confirm that input_number and input_select behave the same way.

I don’t have an adequate understanding of Home Assistant’s state machine to explain the behavior properly but I do know that using Set State is not a “durable” way (for lack of a better term) to influence an entity’s state.

Similarly, if you use Set State to set an entity’s state value, that value will not be stored/restored after a restart.


EDIT

I overlooked to mention that it works correctly if you set the entity’s state value using the appropriate service call such as counter.configure for a counter (or input_number.set for an input_number).

1 Like

Set state just overwrites the state, it does not change the underlying data. Always use the services.

1 Like

I was quite sure that was what I was doing last night, at least in the automations and scripts I tried.
I tried it today with both setting the state and initial using script/automations and it works now.

That’s my understanding as well; the product of Set State isn’t durable (the value doesn’t persist).

What’s unclear is how a service call differs from this so-called “overwriting” process. Both change an entity’s state but one is retained and the other isn’t.

Is it because a service call performs an official transaction (of some sort) that’s recognized by the state machine whereas Set State does some sort of end-run around the transaction and merely alters the existing state value?

What’s unclear is how does the state machine remember the previous “official” state value, when executing counter.increment, as opposed to the “overwritten” value?

the service “sets” the metadata

You’re just plopping the value in the state machine, the state machine is a snapshot of the current states. You’re just overwriting what’s in the snapshot, not what’s in the metadata that creates the snapshot.

1 Like

OK, if I understood that correctly, it implies a service call, like counter.increment, uses the state value in the meta-data as opposed to the one in the state machine. That one is “just for show” and is normally identical to the one in the meta-data, unless it was altered by Set State. Is that more or less correct?

It’s different for every integration. But you can look at the code youreself. You’ll see that the @property state is most likely pulled from a self._state. That’s the metadata.