I’m extracting a large list of dates using a REST sensor and running into the 255 character limit. Knowing the list only contains date_times to exact minutes, I’ve been playing with ideas to pack more items in. Firstly converting from data_time to timestamp allowed 23 dates to the nearest second. Using as_timestamp(x)//60
allows me to pack in 28 (//60 converts to minutes, using 8 characters plus a separator per date). I’d like to go further, ideally to 60. It then struck me that these timestamps can be represented by 32bit integers which in turn could be represented by 4 ASCII characters. Allowing another character as a separator makes 5 per, ie 51 dates packed into 255 characters. Not quite my target of 60, but close enough. The trouble is I can’t work out how to do it and my understanding of the terminology for what is going on hasn’t allowed me to search successfully. Can any kind soul point me in the right direction?
What I’ve got so far:
{% set value_json =
[{
"DateTime": "2025-02-11T00:00:00Z",
"Height": 1.897947
}, {
"DateTime": "2025-02-11T00:01:00Z",
"Height": 1.904356
} etc
] %}
{% set target, offset = states('input_number.tide_low_restriction')|float(1.5), states('sensor.tide_offset')|float(0) -%} {% set ns = namespace(times='') -%}
{% for item in value_json -%}
{% if loop.previtem is defined and loop.nextitem is defined -%}
{% if (item.Height < target-offset and loop.previtem.Height >= target-offset )
or (item.Height < target-offset and loop.nextitem.Height >= target-offset ) -%}
{% set ns.times = ns.times ~ as_timestamp(item.DateTime)|int//60~',' -%}
{% endif -%}
{% else -%}
{% if item.Height <= target - offset -%}
{% set ns.times = ns.times ~ as_timestamp(item.DateTime)|int//60~',' -%}
{% endif -%}
{% endif -%}
{% endfor %}
{% set out = ns.times -%}
{{out}}
Incidentally this is getting a list of the times the tide is less than or greater to a certain height restriction, currently only running in developer tools.
I guess another method is the return a list consisting on the number of minutes since the last transition, but that could be a 4 or 5 digit number per plus 9 for the original timestamp, so less efficient & more uncertain.