Skip to content
Daniel edited this page Aug 16, 2024 · 7 revisions

Sensors


Alexa Timers

The following template will give you the results of multiple Alexa Timers.

template:
  - sensor:
    - name: "Alexa Timer 1"
      icon: mdi:timer-outline
      state: >
          {% if state_attr("sensor.kitchen_original_next_timer", "sorted_active") != None %}
            {% set sorted_active = state_attr("sensor.kitchen_original_next_timer", "sorted_active") | from_json %}
            {% if sorted_active[0] in sorted_active %}
              {{ (sorted_active[0][1].triggerTime|int /1000 -  (as_timestamp(now()))) | timestamp_custom('%H:%M:%S', false)}}
            {% else %}unavailable{% endif %}
          {% else %}unavailable{% endif %}   
      attributes:
        label: >
          {% if state_attr("sensor.kitchen_original_next_timer", "sorted_active") != None %}
            {% set sorted_active = state_attr("sensor.kitchen_original_next_timer", "sorted_active") | from_json %}
            {% if sorted_active[0] in sorted_active %}
              {{ sorted_active[0][1].timerLabel }}
            {% else %}unavailable{% endif %}
          {% else %}unavailable{% endif %}    
    - name: "Alexa Timer 2"
      icon: mdi:timer-outline
      state: >
          {% if state_attr("sensor.kitchen_original_next_timer", "sorted_active") != None %}
            {% set sorted_active = state_attr("sensor.kitchen_original_next_timer", "sorted_active") | from_json %}
            {% if sorted_active[1] in sorted_active %}
              {{ (sorted_active[1][1].triggerTime|int /1000 -  (as_timestamp(now()))) | timestamp_custom('%H:%M:%S', false)}}
            {% else %}unavailable{% endif %}
          {% else %}unavailable{% endif %}   
      attributes:
        label: >
          {% if state_attr("sensor.kitchen_original_next_timer", "sorted_active") != None %}
            {% set sorted_active = state_attr("sensor.kitchen_original_next_timer", "sorted_active") | from_json %}
            {% if sorted_active[1] in sorted_active %}
              {{ sorted_active[1][1].timerLabel }}
            {% else %}unavailable{% endif %}
          {% else %}unavailable{% endif %}       

image

Though the above will work, likely better off utilizing the Timer component within Home Assistant, that way it will count down and not be dependent on API calls to Alexa to update the timer. Create a timer helper entity, and then you can use the same above logic in an automation to set a timer.

alias: Alexa Kitchen Timer
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.kitchen_original_next_timer
    attribute: total_active
    above: 0
condition: []
action:
  - service: timer.start
    data_template:
      entity_id: timer.kitchen_echo_timer
      duration: >
        {% if state_attr("sensor.kitchen_original_next_timer", "sorted_active")
        != None %}
         {% set sorted_active = state_attr("sensor.kitchen_original_next_timer", "sorted_active") | from_json %}
         {% if sorted_active[0] in sorted_active %}
           {{ (sorted_active[0][1].triggerTime|int /1000 - (as_timestamp(now()))) | timestamp_custom('%H:%M:%S', false)}}
         {% else %}unavailable{% endif %}
         {% else %}unavailable{% endif %} 
mode: single

Legacy Last Called Sensor

Note: When two accounts are configured, all Alexa device names have to be unique across both accounts when calling the service alexa_media.update_last_called with no email account specified which tries to update the last_called device from both accounts. If not, the alexa_media.update_last_called service will fail to complete and the last_called device will not update correctly until the next scheduled poll. Alternatively, you can issue two separate alexa_media.update_last_called service calls, one for each account.

Group-Based and Multi-Account Methods

First, create a group of your Alexa devices.

###configuration.yaml
group:
  echos:
    name: All Echos
    entities:
      - media_player.this_echo
      - media_player.that_echo
      - media_player.his_echo_show
      - media_player.her_echo_show

Then, use a template sensor to evaluate the group. You have two options when creating your template sensor. The first will work for most users:

###configuration.yaml

template:
  - sensor:
      - name: last_alexa
        state: >
          {{ expand('group.echos') | selectattr('attributes.last_called','eq',True) | map(attribute='entity_id') | first }}
        availability: >
          {{ expand('group.echos') | selectattr('attributes.last_called','eq',True) | first is defined }}

If you are using more than one Amazon account use the following template sensor to evaluate the group based on timestamp data:

###configuration.yaml

template:
  - sensor:
      - name: last alexa
        state: >
          {{ expand('group.echos') | selectattr('attributes.last_called_timestamp') | map(attribute='entity_id') | first }}
        availability: >
          {{ expand('group.echos') | selectattr('attributes.last_called','eq',True) | first is defined }}