This mostly works unless the leftmost bit is set to 1 (so hex string start with 8 to F).
In that case, the value is still populated in the template sensor, but HA throws an error in logs :
Error writing config for core.restore_state: Bad data at $.data[63].extra_data.native_value=10011011001110101101010000000001(<class 'int'>
Error writing config for core.restore_state: Bad data at $.data[63].extra_data.native_value=10011011001110101001000000000000(<class 'int'>
To investigate further, testing this in the Model editor in dev tools :
Then if I change 7FFFFFFF to 8FFFFFFF for STGE no result and this error gets throwns in HA logs :
ERROR (MainThread) [homeassistant.components.websocket_api.messages] Unable to serialize to JSON. Bad data found at $.event.result=10001111111111111111111111111111(<class 'int'>
If I split this code into 2 sensors with first and second half of the binary string, it works fine without error.
Just a thought: wouldnt it be ‘easier’ to craft a python script to do this which you call on updated values? python function for this is really simple and powerful
{% set bit = 27 %} {# between 0 and 31 #}
{% set hex = "DEADBEEF" %} {# between 0 and FFFFFFFF #}
hex : {{ hex }}
bit : {{ bit }}
{{ "{:0=32b}".format(hex|int(base=16)) }}
{{ " "*(31-bit) }}^
bit {{ bit }}: {{ hex|int(base=16)|bitwise_and(2**bit)|bool }}
Since here’s lots of template-editor geeks around …
What I don’t get (and I think to remember it was diffent in the past) is:
if I add
{% set hex_str = "F63ACC01" %}
{% set num = hex_str | int(base=16) %}
{{ '{:032b}'.format(num) }}
I get no output at all, eg. a blank result, but if I simply add something senseless such as in line 1 I get both the result from line1 aswell as a binary
{% set hex_str = "F63ACC01" %} {{ hex_str }}
{% set num = hex_str | int(base=16) %}
{{ '{:032b}'.format(num) }}
anyone with an idea about why this happens? Or am I wrong and it was like this in the past? I stumbled across similar things recently when I wanted to do some quick tests in the template editor while getting no results at all until I added some extra stuff.
Looks to me as if something’s broken but I don’t know where to look for that issue.
It’s the same error as the OP was having. The template editor tries to help by interpreting numeric-looking strings as numbers. This:
{{ "1110110001110101100" }}
returns what you’d expect, interpreted as a number, but this:
{{ "11101100011101011001" }}
returns 11101100011101012000. Add another digit and you get a blank result and an error in the system log:
Unable to serialize to JSON. Bad data found at $.event.result=111011000111010110011000(<class 'int'>
You’ve overflowed the signed 32-bit int limit. If you turn down your hex number to below 80000000, it works fine. If you put anything else in the output, it doesn’t try to help and the problem goes away.
We can only raise specific errors. Some of them are caught upstream and displayed as errors in the logs. It’s unlikely that the core team would allow these intercepted errors to be raised to the frontend because it would have unintended changes in things like template entities or frontend cards.
No problem, it happened to me quite often that a {{ something }} simply resulted in no output at all. Perhaps it’s not really essential to know the exact error which might get detailed upsteam but sort of hint that the content should have produced something while the result remained blank.
I’m not into coding so I can’t say if something like that would be possible, but I think about newbies trying the template-editor for the very first time getting no result at all. It’ll be a once-tried and skipped experience perhaps and the template-editor is a damn usefull thing in fact.