What is the difference between this an that?

Hello!

Can someone tell me what is the difference between these two lines?

{{ states.sensor_newma_activation.state }}
{{ states("sensor_newma_activation") }}

I know that the lines could act different in some situations, but why?! What’s the purpose of each line?

Thank you!

First, your syntax for those two lines is incorrect.

{{ states.sensor.newma_activation.state }}
{{ states("sensor.newma_activation") }}

But the first one will produce an error in the log if the entity doesn’t exist.

the second will just return a value of “unknown”.

see the “warning” box here:

1 Like

I just worte “_” instead “.” by mistake… That was not the point.

Both of the lines are working flawless, as you can see in the image

I just want to know what is the difference between them, or… what are the differences, because one I already know, but I don’t know why and I can’t explain it.
Till today, I thought that both of the lines are “identical”… and working in the same way, but… not quite

I know about the Warning info, using that instead of that… but, I don’t understand why they recommend this, in the same time as the first line is working better than the second, or proper. At least in this case, which is very important for me, to know when an entity is defined or not.

See img


This case, made me open this post, in order to find all the differences between these lines…

What part of this explanation do you not understand?

to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).

I don’t understand why they recommend states("") instead of states…, while the states("") can’t tell me if an enity exists or not. On the other part, states… it is giving me a proper answer…

Using both lines… I don’t get any error at HA startup…

Take a look at the next image

states… it is showing me the correct result
states("")… it is showing me the same result for existing and non existing entity :smiley:

This is what I don’t understand…

Your experiment is flawed.

Compare the results of acquiring the state value of a non-existent entity using the two methods:

The first one returns nothing (‘None’) and the second returns unknown. Now see what happens when you attempt to use the returned value.

First format:

Second format:

This is precisely the failure situation mentioned in the documentation; if an entity isn’t ready at startup it fails to exists and attempts to access its state value using the first format will potentially fail and cause an error message.

1 Like

I am trying to understand, but… anyway, let’s do it in the recommended way, there is any proper solution in order to find out if an entity exists or not, with a template?

If I use state("") without “is defined”, only simple state, which I see I get “unknown” for an inexistent entity… is the “unknown” state, a proper state for non existing entity, or this state status can be because of multiple other reasons?