The code doesn't do what it's supposed to

why doesn’t this code do what it’s supposed to?

{% set value_json = {'Battery_State_Test': 1} %}

{% if states('value_json.Battery_State_Test') == 1 %}
ok
{% else %}
nok
{% endif %}

{{ (value_json.Battery_State_Test) }}

I need an explanation because I don’t understand it

The states() function is for reporting the state value of an entity, not a Jinja2 variable.

Reference
Templating - States

Corrected version:

{% set value_json = {'Battery_State_Test': 1} %}

{% if value_json.Battery_State_Test == 1 %}
ok
{% else %}
nok
{% endif %}

{{ (value_json.Battery_State_Test) }}

What is it that you attempting to do?

I will try to change the number to text for the entity

victron venus sends battery charge status as number 0-idle , 1-charge, 2-discharge

i use mqtt for that

victron integration does not go as expected

If it reports a number and you want to display the text that corresponds to the number, you can do it like this:

{% set value_json = {'Battery_State_Test': 1} %}
{{ ['idle', 'charge', 'discharge'][value_json.Battery_State_Test] }}

so this is unbelievable. man, I have a beer for you

this is exactly what i need
thx

1 Like