Convert an integer list to bin and then base64

Continuing the discussion from Convert a base 64 to bin and then integer:
thank @freshcoast
i need now to solve the same issue in a previosly topic but in revers way.
whith

{% set value_to = states('input_text.gatti_meal_plan') %}

{% set timer_1_hh = state_attr('input_datetime.gatti_timer_1', "hour") %}
{% set timer_1_mm = state_attr('input_datetime.gatti_timer_1', "minute") %}
{% set timer_1_meal = states('input_number.gatti_porzioni_cibo') |round (0)  %}
{% set timer_1_enable = iif(is_state('input_boolean.gatti_timer_1_attiva', 'on'), "01", "00") |int  %}

{% set timer_2_hh = state_attr('input_datetime.gatti_timer_2', "hour") %}
{% set timer_2_mm = state_attr('input_datetime.gatti_timer_2', "minute") %}
{% set timer_2_meal = states('input_number.gatti_porzioni_cibo') |round (0)  %}
{% set timer_2_enable = iif(is_state('input_boolean.gatti_timer_2_attiva', 'on'), "01", "00") |int  %}

{% set timer_3_hh = state_attr('input_datetime.gatti_timer_3', "hour") %}
{% set timer_3_mm = state_attr('input_datetime.gatti_timer_3', "minute") %}
{% set timer_3_meal = states('input_number.gatti_porzioni_cibo') |round (0)  %}
{% set timer_3_enable = iif(is_state('input_boolean.gatti_timer_3_attiva', 'on'), "01", "00") |int  %}

{% set timer_4_hh = state_attr('input_datetime.gatti_timer_4', "hour") %}
{% set timer_4_mm = state_attr('input_datetime.gatti_timer_4', "minute") %}
{% set timer_4_meal = states('input_number.gatti_porzioni_cibo') |round (0)  %}
{% set timer_4_enable = iif(is_state('input_boolean.gatti_timer_4_attiva', 'on'), "01", "00") |int  %}

{% set timer_5_hh = state_attr('input_datetime.gatti_timer_5', "hour") %}
{% set timer_5_mm = state_attr('input_datetime.gatti_timer_5', "minute") %}
{% set timer_5_meal = states('input_number.gatti_porzioni_cibo') |round (0)  %}
{% set timer_5_enable = iif(is_state('input_boolean.gatti_timer_5_attiva', 'on'), "01", "00") |int  %}

{% set timer_6_hh = state_attr('input_datetime.gatti_timer_6', "hour") %}
{% set timer_6_mm = state_attr('input_datetime.gatti_timer_6', "minute") %}
{% set timer_6_meal = states('input_number.gatti_porzioni_cibo') |round (0)  %}
{% set timer_6_enable = iif(is_state('input_boolean.gatti_timer_6_attiva', 'on'), "01", "00") |int  %}
{{ [timer_1_hh, timer_1_mm, timer_1_meal, timer_1_enable,
  timer_2_hh, timer_2_mm, timer_2_meal, timer_2_enable,
  timer_3_hh, timer_3_mm, timer_3_meal, timer_3_enable,
  timer_4_hh, timer_4_mm, timer_4_meal, timer_4_enable,
  timer_5_hh, timer_5_mm, timer_5_meal, timer_5_enable,
  timer_6_hh, timer_6_mm, timer_6_meal, timer_6_enable]}}

I have

[9, 0, 3, 1, 12, 0, 3, 1, 15, 0, 3, 1, 18, 0, 3, 1, 21, 0, 3, 1, 0, 0, 3, 0]

which is the revers engeenering to have the conversion [list]->Base64?

This is not as easy since there’s no filter to convert from integer back to unicode character, AFAIK. I think this does the job though:

{% set input = "DAABAQweAgENAAQBDR4DAQ4ABAEOHgYB" %}
Input is {{ input }}
{% set decoded_bytes = (input | base64_decode).encode() | map('int') | list -%}
Decoded bytes are {{ decoded_bytes }}
{%- set ns = namespace(x="") %}
{%- for b in decoded_bytes %}
{%- set ns.x = ns.x + pack(b, '=B').decode() %}
{%- endfor %}
Encoded again: {{ ns.x | base64_encode }}

Results in:

Input is DAABAQweAgENAAQBDR4DAQ4ABAEOHgYB
Decoded bytes are [12, 0, 1, 1, 12, 30, 2, 1, 13, 0, 4, 1, 13, 30, 3, 1, 14, 0, 4, 1, 14, 30, 6, 1]
Encoded again: DAABAQweAgENAAQBDR4DAQ4ABAEOHgYB

and:

{% set example = [9, 0, 3, 1, 12, 0, 3, 1, 15, 0, 3, 1, 18, 0, 3, 1, 21, 0, 3, 1, 0, 0, 3, 0] %}
example = {{ example }}
{%- set ns = namespace(x="") %}
{%- for b in example %}
{%- set ns.x = ns.x + pack(b, '=B').decode() %}
{%- endfor %}
example as base64 {{ ns.x | base64_encode }}

result:

example = [9, 0, 3, 1, 12, 0, 3, 1, 15, 0, 3, 1, 18, 0, 3, 1, 21, 0, 3, 1, 0, 0, 3, 0]
example as base64 = CQADAQwAAwEPAAMBEgADARUAAwEAAAMA

thanks for your help.
I would never have arrived at the solution. great!

1 Like