- sensor:
  - unique_id: bad_fade_slutt
    state: >-
      {{ states.timer.lysfade_bad_aktiv_fade.state }}
    icon: >
      {{ state_attr("timer.lysfade_bad_aktiv_fade","icon") }}
    attributes:
      friendly_name: >
        Bad: fader til scene {{ states.input_text.lysfade_bad_aktiv_scene.state }}
      duration: >
        {{ state_attr("timer.lysfade_bad_aktiv_fade","duration") }}
      start_time: >
        {{ states.timer.lysfade_bad_aktiv_fade.last_changed }}
      end_time: >
        {{ state_attr("timer.lysfade_bad_aktiv_fade","finishes_at") }}
  - unique_id: aktiv_lysfade_hoved
    state: >-
      {% set ns = namespace(entity_list='') %}
      {% for item in states.pyscript %}
        {% if 'hue' in item.entity_id and 'hoved' in item.entity_id and item.state == 'on' %}
          {% set ns.entity_list = ns.entity_list + ',' + item.name %}
        {% endif %}
      {% endfor %}
      {{ ns.entity_list.split(',') | select("ne", '') | join(', ') }}
    icon: >
      {{ state_attr("input_select.lysfade_aktiv_gruppe","icon") }}
    attributes:
      friendly_name: >
        {% set ns = namespace(entity_list='') %}
        {% for item in states.pyscript %}
          {% if 'hue' in item.entity_id and 'hoved' in item.entity_id and item.state == 'on' %}
            {% set ns.entity_list = ns.entity_list + ',' + item.name %}
          {% endif %}
        {% endfor %}
        {{ ns.entity_list.split(',') | select("ne", '') | join(', ') }}
  - unique_id: aktiv_lysfade_fastelys
    state: >-
      {% set ns = namespace(entity_list='') %}
      {% for item in states.pyscript %}
        {% if 'hue' in item.entity_id and 'fastelys' in item.entity_id and item.state == 'on' %}
          {% set ns.entity_list = ns.entity_list + ',' + item.name %}
        {% endif %}
      {% endfor %}
      {{ ns.entity_list.split(',') | select("ne", '') | join(', ') }}
    icon: >
      {{ state_attr("input_select.lysfade_aktiv_gruppe","icon") }}
    attributes:
      friendly_name: >
        {% set ns = namespace(entity_list='') %}
        {% for item in states.pyscript %}
          {% if 'hue' in item.entity_id and 'fastelys' in item.entity_id and item.state == 'on' %}
            {% set ns.entity_list = ns.entity_list + ',' + item.name %}
          {% endif %}
        {% endfor %}
        {{ ns.entity_list.split(',') | select("ne", '') | join(', ') }}
  - unique_id: tablet_next_alarm
    state: >-
      {% if states.sensor.nettbrett1_next_alarm.state == "unavailable" %}
      {{ "unknown" }}
      {% elif states.sensor.nettbrett1_next_alarm.state == "unknown" %}
      {{ "unknown" }}
      {% else %}
      {{ as_timestamp(states.sensor.nettbrett1_next_alarm.state)|timestamp_custom('%H:%M', true) }}
      {% endif %}
    attributes:
      icon: 'mdi:alarm'
      friendly_name: 'Neste alarm på nettbrett'
      active: >-
        {% if states.sensor.nettbrett1_next_alarm.state == "unavailable" %}
        {{ "Off" }}
        {% else %}
        {{ "On" }}
        {% endif %}
  - unique_id: wakeup_alarm
    state: >-
      {{ state_attr('input_datetime.vekking', 'timestamp') | timestamp_custom('%H:%M', false) }}
    attributes:
      icon: 'mdi:alarm'
      friendly_name: 'Vekking'
  - unique_id: missing_some_alarm_setting
    state: >-
      {{ states.sensor.template_tablet_next_alarm.state == "unknown" or  states.input_boolean.alarm_aktiv.state == "off" }}
    attributes:
      friendly_name: 'Alarm inaktiv for HA eller nettbrett'
  - unique_id: tablet_alarm_active
    state: >-
      {% if states.sensor.template_tablet_next_alarm.state == "unknown" %}
      {{ "off" }}
      {% else %}
      {{ "on" }}
      {% endif %}
    attributes:
      icon: 'mdi:alarm'
      friendly_name: 'Alarm nettbrett aktiv'
  - unique_id: huetrans_vekking_endtime_auto
    state: "{{ (states.sensor.vekking_for_lysfade.state|int(0) + (30*60))|timestamp_custom('%H:%M:%S', false) }}"
    attributes:
      friendly_name: 'LysFade: Slutt Vekking (beregnet)'
      editable: "{{ false }}"
      has_date: "{{ false }}"
      has_time: "{{ true }}"
      hour: "{{ ((states.sensor.vekking_for_lysfade.state|int(0) + (30*60))/(60*60))|int(0) % 24 }}"
      minute: "{{ ((states.sensor.vekking_for_lysfade.state|int(0) + (30*60))/60)|int(0) % 60 }}"
      second: "{{ (states.sensor.vekking_for_lysfade.state|int(0) + (30*60)) % 60 }}"
      timestamp: "{{ (states.sensor.vekking_for_lysfade.state|int(0) + (30*60)) }}"
  - unique_id: huetrans_vekking_endtime_auto_old
    state: "{{ (state_attr('input_datetime.vekking', 'timestamp') + (30*60))|timestamp_custom('%H:%M:%S', false) }}"
    attributes:
      friendly_name: 'LysFade: Slutt Vekking (beregnet) - gammel beregning'
      editable: "{{ false }}"
      has_date: "{{ false }}"
      has_time: "{{ true }}"
      hour: "{{ ((state_attr('input_datetime.vekking', 'hour') + (state_attr('input_datetime.vekking', 'minute')/60) + (state_attr('input_datetime.vekking', 'second')/(60*60)) + 0.5) // 1) | int }}"
      minute: "{{ ((state_attr('input_datetime.vekking', 'minute') + (state_attr('input_datetime.vekking', 'second')/60) + 30) % 60) | int }}"
      second: "{{ (state_attr('input_datetime.vekking', 'second') + (30*60)) % 60 }}"
      timestamp: "{{ (state_attr('input_datetime.vekking', 'timestamp') + (30*60)) }}"
  - unique_id: vekking_timestamp
    state: "{{ state_attr('input_datetime.vekking', 'timestamp') }}"
    attributes:
      friendly_name: 'Vekking: timestamp (beregnet)'
  - unique_id: wakeup_lightfade_delay_timestamp
    state: "{{ state_attr('input_datetime.wakeup_lightfade_delay', 'timestamp') }}"
    attributes:
      friendly_name: 'Vekking lysfade delay: timestamp (beregnet)'
  - unique_id: huetrans_hoved_arbeidslys_timestamp_helper
    state: >-
      {% set item_entity_id = 'pyscript.huetrans_hoved_arbeidslys' %}
      {% set after_entity = state_attr(item_entity_id, 'after_entity') %}
      {% set min_diff = state_attr(item_entity_id, 'min_diff') %}
      {% set endtime_entity = state_attr(item_entity_id, 'normal_endtime_entity') %}
      {% if not '.' in endtime_entity %}
        {% set endtime_entity = 'input_datetime.' + endtime_entity %}
      {% endif %}
      {% set endtime = state_attr(endtime_entity, 'timestamp') %}
      {% set min_endtime = state_attr(after_entity, 'timestamp') %}
      {% if endtime - min_endtime < min_diff %}
        {{ min_endtime + min_diff }}
      {% else %}
        {{ endtime }}
      {% endif %}
  - unique_id: huetrans_arbeidslys_endtime_auto
    state: "{{ states('sensor.template_huetrans_hoved_arbeidslys_timestamp_helper')|int|timestamp_custom('%H:%M:%S', false) }}"
    attributes:
      friendly_name: 'LysFade: Slutt Arbeidslys (beregnet)'
      editable: "{{ false }}"
      has_date: "{{ false }}"
      has_time: "{{ true }}"
      hour: "{{ states('sensor.template_huetrans_hoved_arbeidslys_timestamp_helper')|int // (60*60) }}"
      minute: "{{ (states('sensor.template_huetrans_hoved_arbeidslys_timestamp_helper')|int % (60*60)) // 60 }}"
      second: "{{ states('sensor.template_huetrans_hoved_arbeidslys_timestamp_helper')|int % 60}}"
      timestamp: "{{ states('sensor.template_huetrans_hoved_arbeidslys_timestamp_helper')|int }}"
  # IPORTANT: This needs to be placed AFTER the auto endtime templates, since this relies on data from them to calculate it's value
  - unique_id: next_transition_hoved
    state: >-
      {% set nowTimestamp = as_timestamp(utcnow()) - as_timestamp(strptime(as_timestamp(utcnow())|timestamp_custom('%Y-%m-%d', true),'%Y-%m-%d')) %}
      {% set ns = namespace(nextTimer='',nextTimestamp=0,firstTimer='',firstTimestamp=0) %}
      {% set ns.nextTimestamp = 60*60*24 %}
      {% set ns.firstTimestamp = 60*60*24 %}
      {% for item in states.pyscript %}
        {% if 'huetrans_hoved' in item.entity_id %}
          {% set endtime_entity = state_attr(item.entity_id, 'endtime_entity') %}
          {% if endtime_entity != None %}
            {% if not '.' in endtime_entity %}
              {% set endtime_entity = 'input_datetime.' + endtime_entity %}
            {% endif %}
            {% set transTimestamp = state_attr(endtime_entity, 'timestamp') %}
            {% if nowTimestamp < transTimestamp %}
              {% if ns.nextTimestamp > transTimestamp %}
                {% set ns.nextTimer = item.entity_id %}
                {% set ns.nextTimestamp = transTimestamp %}
              {% endif %}
            {% endif %}
            {% if ns.firstTimestamp > transTimestamp %}
              {% set ns.firstTimer = item.entity_id %}
              {% set ns.firstTimestamp = transTimestamp %}
            {% endif %}
          {% endif %}
        {% endif %}
      {% endfor %}
      {% if ns.firstTimestamp > ns.nextTimestamp or ns.nextTimestamp == 60*60*24 %}
        {% set ns.nextTimer = ns.firstTimer %}
        {% set ns.nextTimestamp = ns.firstTimestamp %}
      {% endif %}
      {{ ns.nextTimer }}
    attributes:
      icon: 'mdi:car-parking-lights'
      friendly_name: 'Data entity for next transition group with "hoved"'
  - unique_id: current_transition_hoved
    state: >-
      {% set nowTimestamp = as_timestamp(utcnow()) - as_timestamp(strptime(as_timestamp(utcnow())|timestamp_custom('%Y-%m-%d', true),'%Y-%m-%d')) %}
      {% set ns = namespace(currentTimer='',currentTimestamp=0,firstTimer='',firstTimestamp=0,seconds_to_complete_trans=0) %}
      {% set ns.currentTimestamp = 0 %}
      {% set ns.firstTimestamp = 60*60*24 %}
      {% for item in states.pyscript %}
        {% if 'huetrans_hoved' in item.entity_id %}
          {% set ns.seconds_to_complete_trans = 0 %}
          {% for scn in state_attr(item.entity_id, 'Scenes') %}
            {% set ns.seconds_to_complete_trans = ns.seconds_to_complete_trans + scn.timeinseconds + scn.delay|default(0) %}
          {% endfor %}
          {% set endtime_entity = state_attr(item.entity_id, 'endtime_entity') %}
          {% if endtime_entity != None %}
            {% if not '.' in endtime_entity %}
              {% set endtime_entity = 'input_datetime.' + endtime_entity %}
            {% endif %}
            {% set transTimestamp = state_attr(endtime_entity, 'timestamp') - ns.seconds_to_complete_trans %}
            {% if nowTimestamp > transTimestamp %}
              {% if ns.currentTimestamp < transTimestamp %}
                {% set ns.currentTimer = item.entity_id %}
                {% set ns.currentTimestamp = transTimestamp %}
              {% endif %}
            {% endif %}
            {% if ns.firstTimestamp > transTimestamp %}
              {% set ns.firstTimer = item.entity_id %}
              {% set ns.firstTimestamp = transTimestamp %}
            {% endif %}
          {% endif %}
        {% endif %}
      {% endfor %}
      {% if ns.firstTimestamp > ns.currentTimestamp %}
        {% set ns.currentTimer = ns.firstTimer %}
        {% set ns.currentTimestamp = ns.firstTimestamp %}
      {% endif %}
      {{ ns.currentTimer }}
    attributes:
      icon: 'mdi:car-parking-lights'
      friendly_name: 'Data entity for current transition group with "hoved"'
  - unique_id: huetrans_fastelys_morgen_timestamp_helper
    state: >-
      {% set item_entity_id = 'pyscript.huetrans_fastelys_morgen' %}
      {% set after_entity = state_attr(item_entity_id, 'after_entity') %}
      {% set min_diff = state_attr(item_entity_id, 'min_diff') %}
      {% set endtime_entity = state_attr(item_entity_id, 'normal_endtime_entity') %}
      {% if not '.' in endtime_entity %}
        {% set endtime_entity = 'input_datetime.' + endtime_entity %}
      {% endif %}
      {% set endtime = state_attr(endtime_entity, 'timestamp') %}
      {% set min_endtime = state_attr(after_entity, 'timestamp') %}
      {% if endtime - min_endtime < min_diff %}
        {{ min_endtime + min_diff }}
      {% else %}
        {{ endtime }}
      {% endif %}
  - unique_id: huetrans_fastelys_morgen_endtime_auto
    state: "{{ states('sensor.template_huetrans_fastelys_morgen_timestamp_helper')|int|timestamp_custom('%H:%M:%S', false) }}"
    attributes:
      friendly_name: 'LysFade: Slutt Basislys morgen (beregnet)'
      editable: "{{ false }}"
      has_date: "{{ false }}"
      has_time: "{{ true }}"
      hour: "{{ states('sensor.template_huetrans_fastelys_morgen_timestamp_helper')|int // (60*60) }}"
      minute: "{{ (states('sensor.template_huetrans_fastelys_morgen_timestamp_helper')|int % (60*60)) // 60 }}"
      second: "{{ states('sensor.template_huetrans_fastelys_morgen_timestamp_helper')|int % 60}}"
      timestamp: "{{ states('sensor.template_huetrans_fastelys_morgen_timestamp_helper')|int }}"
  - unique_id: huetrans_fastelys_dag_timestamp_helper
    state: >-
      {% set item_entity_id = 'pyscript.huetrans_fastelys_dag' %}
      {% set after_entity = state_attr(item_entity_id, 'after_entity') %}
      {% set min_diff = state_attr(item_entity_id, 'min_diff') %}
      {% set endtime_entity = state_attr(item_entity_id, 'normal_endtime_entity') %}
      {% if not '.' in endtime_entity %}
        {% set endtime_entity = 'input_datetime.' + endtime_entity %}
      {% endif %}
      {% set endtime = state_attr(endtime_entity, 'timestamp') %}
      {% set min_endtime = state_attr(after_entity, 'timestamp') %}
      {% if endtime - min_endtime < min_diff %}
        {{ min_endtime + min_diff }}
      {% else %}
        {{ endtime }}
      {% endif %}
  - unique_id: huetrans_fastelys_dag_endtime_auto
    state: "{{ states('sensor.template_huetrans_fastelys_dag_timestamp_helper')|int|timestamp_custom('%H:%M:%S', false) }}"
    attributes:
      friendly_name: 'LysFade: Slutt Basislys dag (beregnet)'
      editable: "{{ false }}"
      has_date: "{{ false }}"
      has_time: "{{ true }}"
      hour: "{{ states('sensor.template_huetrans_fastelys_dag_timestamp_helper')|int // (60*60) }}"
      minute: "{{ (states('sensor.template_huetrans_fastelys_dag_timestamp_helper')|int % (60*60)) // 60 }}"
      second: "{{ states('sensor.template_huetrans_fastelys_dag_timestamp_helper')|int % 60}}"
      timestamp: "{{ states('sensor.template_huetrans_fastelys_dag_timestamp_helper')|int }}"
  # IPORTANT: This needs to be placed AFTER the auto endtime templates, since this relies on data from them to calculate it's value
  - unique_id: next_transition_fastelys
    state: >-
      {% set nowTimestamp = as_timestamp(utcnow()) - as_timestamp(strptime(as_timestamp(utcnow())|timestamp_custom('%Y-%m-%d', true),'%Y-%m-%d')) %}
      {% set ns = namespace(nextTimer='',nextTimestamp=0,firstTimer='',firstTimestamp=0) %}
      {% set ns.nextTimestamp = 60*60*24 %}
      {% set ns.firstTimestamp = 60*60*24 %}
      {% for item in states.pyscript %}
        {% if 'huetrans_fastelys' in item.entity_id %}
          {% set endtime_entity = state_attr(item.entity_id, 'endtime_entity') %}
          {% if endtime_entity != None %}
            {% if not "." in endtime_entity %}
              {% set endtime_entity = 'input_datetime.' + endtime_entity %}
            {% endif %}
            {% set transTimestamp = state_attr(endtime_entity, 'timestamp') %}
            {% if nowTimestamp < transTimestamp %}
              {% if ns.nextTimestamp > transTimestamp %}
                {% set ns.nextTimer = item.entity_id %}
                {% set ns.nextTimestamp = transTimestamp %}
              {% endif %}
            {% endif %}
            {% if ns.firstTimestamp > transTimestamp %}
              {% set ns.firstTimer = item.entity_id %}
              {% set ns.firstTimestamp = transTimestamp %}
            {% endif %}
          {% endif %}
        {% endif %}
      {% endfor %}
      {% if ns.firstTimestamp > ns.nextTimestamp or ns.nextTimestamp == 60*60*24 %}
        {% set ns.nextTimer = ns.firstTimer %}
        {% set ns.nextTimestamp = ns.firstTimestamp %}
      {% endif %}
      {{ ns.nextTimer }}
    attributes:
      icon: 'mdi:car-parking-lights'
      friendly_name: 'Data entity for next transition group with "Faste lys"'
  - unique_id: current_transition_fastelys
    state: >-
      {% set nowTimestamp = as_timestamp(utcnow()) - as_timestamp(strptime(as_timestamp(utcnow())|timestamp_custom('%Y-%m-%d', true),'%Y-%m-%d')) %}
      {% set ns = namespace(currentTimer='',currentTimestamp=0,firstTimer='',firstTimestamp=0,seconds_to_complete_trans=0) %}
      {% set ns.currentTimestamp = 0 %}
      {% set ns.firstTimestamp = 60*60*24 %}
      {% for item in states.pyscript %}
        {% if 'huetrans_fastelys' in item.entity_id %}
          {% set ns.seconds_to_complete_trans = 0 %}
          {% for scn in state_attr(item.entity_id, 'Scenes') %}
            {% set ns.seconds_to_complete_trans = ns.seconds_to_complete_trans + scn.timeinseconds + scn.delay|default(0) %}
          {% endfor %}
          {% set endtime_entity = state_attr(item.entity_id, 'endtime_entity') %}
          {% if endtime_entity != None %}
            {% if not '.' in endtime_entity %}
              {% set endtime_entity = 'input_datetime.' + endtime_entity %}
            {% endif %}
            {% set transTimestamp = state_attr(endtime_entity, 'timestamp') - ns.seconds_to_complete_trans %}
            {% if nowTimestamp > transTimestamp %}
              {% if ns.currentTimestamp < transTimestamp %}
                {% set ns.currentTimer = item.entity_id %}
                {% set ns.currentTimestamp = transTimestamp %}
              {% endif %}
            {% endif %}
            {% if ns.firstTimestamp > transTimestamp %}
              {% set ns.firstTimer = item.entity_id %}
              {% set ns.firstTimestamp = transTimestamp %}
            {% endif %}
          {% endif %}
        {% endif %}
      {% endfor %}
      {% if ns.firstTimestamp > ns.currentTimestamp %}
        {% set ns.currentTimer = ns.firstTimer %}
        {% set ns.currentTimestamp = ns.firstTimestamp %}
      {% endif %}
      {{ ns.currentTimer }}
    attributes:
      icon: 'mdi:car-parking-lights'
      friendly_name: 'Data entity for current transition group with "Faste lys"'
  # - unique_id: test_temp_diff
  #   state: '{{ (states("sensor.kontoret_temperature")|float(default=20)) - (states("input_number.termostat_kontoret")|float(default=20)) }}'
  #   attributes:
  #     friendly_name: 'Test diff temperatur vs termostat'