{% set timezone = '-04:00' %}
{% set delta = states('sensor.number_of_days_next_alarm_macro') %}
{% if delta == 'Not set' %} {{ delta }}
{% else %}
{% set delta = delta | int %}
{% set time = now() %}
{% set mapper = [ 'Today', 'Tomorrow', 'The day after tomorrow,' ] %}
{% set disabled = is_state('input_boolean.alarmclock_wd_enabled','off') and is_state('input_boolean.alarmclock_we_enabled','off') %}
{% set daystring = 'Not set' if disabled else mapper[delta] if delta < mapper | length else 'Next' %}
{% set time = now() %}
{% set current = time.strftime('%j') | int %}
{% set next = current + delta if current + delta <= 365 else (current + delta)-365 %}
{% set year = time.year if current + delta <= 365 else time.year + 1 %}
{% set next = '{} {} 00:00:00{}'.format(year, next, timezone) %}
{% set next = as_timestamp(strptime(next, '%Y %j %H:%M:%S%z')) | timestamp_custom('%A, %-d %B') %}
{{ daystring }} {{ next }}
{% endif %}
what is next set to between the format and the strptime on your system?
Hmm, that one is actually quite challenging. It’s only challenging because states are strings and we need numbers. You could make a python script to do this though, and it would be easy.
I will on the 92.2 system.
the thing is, my main and most robust system is on 84.3…
maybe if id understand the difference I could see to try and adjust the template.
though even on that system the template has some peculiarities, see what happens, with alarm weekend set to Off and no number_of_days_macro sensor ;-)):
It’s not really a matter of having a list, it’s that you can’t create a list in jinja. So the problem lies in the fact that we need to sort the numbers numerically. Well we can’t do that because a devices state is a string. So it can only be sorted based on a string. Well that doesn’t work well because string sorting looks at the first number and sorts off that. So 9 would be greater than 10, because 9 is greater than 1. So our only recourse is to convert the list into a list of numbers. In order to do that, we need to create a new list. And that’s the road block. Creating a list and adding to it is not possible in jinja. Pretty much need to use a python script to generate the list.
Now if you just want the max item without getting the top 3 you can just use the min/max sensor
Otherwise, gotta go the python script route with an automation that triggers the script to run when an entity changes.
if you put all the sensors in a group, this script should give you the top 3 entities. You’d need to figure out where you want to store this info. Typically mqtt or a input_text.
input_text = 'input_text.max_sensors'
sensors = ['sensor.a','sensor.b','sensor.c','sensor.d']
class sortsensor:
def __init__(self, name, state):
self.name = name
try:
self.state = float(state)
except ValueError:
self.state = 0
sensors = [ sortsensor(sensor, hass.states.get(sensor).state) for sensor in sensors ]
sensors.sort(key=lambda x: x.state)
top = ', '.join([sensor.name for sensor in sensors[:3]])
hass.states.set(input_text, top)
EDIT: Let me know if this works. If it does, i’ll help you with the automations and the sensors that display friendly names and states.