Templates are defined as either sensor:
, or binary_sensor:
for true/false types.
Start coding in Dev Tools, Template to develop your template. If you get stumped, post your code attempt and others will help as others have helped me. Here’s some templates I’ve developed in the last 6 months. Nothing battery related as all mine are normal!
# Templates
- platform: template
sensors:
# Sun
next_midnight:
friendly_name: 'Next Midnight'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_dusk) | timestamp_custom('%H:%M')}}
icon_template: mdi:weather-sunset-up
next_dawn:
friendly_name: 'Next Dawn'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_dawn) | timestamp_custom('%H:%M')}}
icon_template: mdi:weather-sunset-up
next_sunrise:
friendly_name: 'Next Sunrise'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom('%H:%M') }}
icon_template: mdi:weather-sunset-up
next_noon:
friendly_name: 'Next Noon'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_dusk) | timestamp_custom('%H:%M')}}
icon_template: mdi:weather-sunset-up
next_sunset:
friendly_name: 'Next Sunset'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom('%H:%M') }}
icon_template: mdi:weather-sunset-down
next_dusk:
friendly_name: 'Next Dusk'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_dusk) | timestamp_custom('%H:%M')}}
icon_template: mdi:weather-sunset-up
sun_elevation:
friendly_name: "Sun Elevation"
value_template: "{{ state_attr('sun.sun', 'elevation') }}"
sun_azimuth:
friendly_name: "Sun Azimuth"
value_template: "{{ state_attr('sun.sun', 'azimuth') }}"
period_of_day:
friendly_name: 'Period of day'
value_template: >-
{% if (as_timestamp(states.sun.sun.attributes.next_dusk)) - (as_timestamp(states.sun.sun.attributes.next_setting)) < 0 %}
Dusk
{% elif (as_timestamp(states.sun.sun.attributes.next_rising)) - (as_timestamp(states.sun.sun.attributes.next_dawn)) < 0 %}
Dawn
{% elif (states.sun.sun.attributes.elevation) < 0 %}
Night
{% else %}
Day
{% endif %}
icon_template: >-
{% if is_state('sun.sun', 'above_horizon') %}
mdi:weather-sunny
{%elif (as_timestamp(states.sun.sun.attributes.next_dusk)) - (as_timestamp(states.sun.sun.attributes.next_setting)) < 0 %}
mdi:weather-sunset-down
{% elif (as_timestamp(states.sun.sun.attributes.next_rising)) - (as_timestamp(states.sun.sun.attributes.next_dawn)) < 0 %}
mdi:weather-sunset-up
{% else %}
mdi:weather-night
{% endif %}
# Weather
norton_court_temperature:
friendly_name: Norton Court Temperature
value_template: >
{{ state_attr('weather.norton_court','temperature') }}
dest_address:
value_template: >-
{{ states('input_select.destination') }}
# Climate
nest_time_to_temp:
friendly_name: Living Room Time to Temp
icon_template: mdi:camera-timer
value_template: >
{{ ((state_attr('climate.living_room','temperature')|float - states('sensor.living_room_temperature')|float) * state_attr('sensor.nest_time_to_temp','rate_of_change')|float/60)|abs|int }}
attribute_templates:
message: |
{% if states('sensor.nest_time_to_temp')|float < 10 %}
IN LESS THAN 10 MIN
{% elif states('sensor.nest_time_to_temp')|float < 16 %}
IN 15 MIN
{% elif states('sensor.nest_time_to_temp')|float < 21 %}
IN 20 MIN
{% elif states('sensor.nest_time_to_temp')|float < 26 %}
IN 25 MIN
{% elif states('sensor.nest_time_to_temp')|float < 31 %}
IN 30 MIN
{% elif states('sensor.nest_time_to_temp')|float < 36 %}
IN 35 MIN
{% elif states('sensor.nest_time_to_temp')|float < 41 %}
IN 40 MIN
{% elif states('sensor.nest_time_to_temp')|float < 46 %}
IN 45 MIN
{% elif states('sensor.nest_time_to_temp')|float < 51 %}
IN 50 MIN
{% elif states('sensor.nest_time_to_temp')|float < 56 %}
IN 55 MIN
{% elif states('sensor.nest_time_to_temp')|float < 61 %}
IN 1 HR
{% elif states('sensor.nest_time_to_temp')|float < 66 %}
IN 1 HR 5 MIN
{% elif states('sensor.nest_time_to_temp')|float < 71 %}
IN 1 HR 10 MIN
{% elif states('sensor.nest_time_to_temp')|float < 76 %}
IN 1 HR 15 MIN
{% elif states('sensor.nest_time_to_temp')|float < 81 %}
IN 1 HR 20 MIN
{% elif states('sensor.nest_time_to_temp')|float < 86 %}
IN 1 HR 25 MIN
{% elif states('sensor.nest_time_to_temp')|float < 191 %}
IN 1½ HR
{% else %}
IN 2+ HR
{% endif %}
time_of_current_change_to_heating: >
{{ states('input_datetime.hvac_current_heat_started') }}
time_of_last_change_to_heating: >
{{ states('input_datetime.hvac_heat_started') }}
time_of_last_change_from_heating: >
{{ states('input_datetime.hvac_heat_ended') }}
elapsed_seconds: >
{{ as_timestamp(states('input_datetime.hvac_heat_ended')) - as_timestamp(states('input_datetime.hvac_heat_started')) }}
temp_at_last_change_to_heat: >
{{ states('input_number.hvac_heat_start_temp') }}
temp_at_last_change_from_heat: >
{{ states('input_number.hvac_heat_end_temp') }}
degrees_of_separation: >-
{{ (states('input_number.hvac_heat_end_temp')|float - states('input_number.hvac_heat_start_temp')|float)|abs|round(1) }}
rate_of_change: >
{% if state_attr('sensor.nest_time_to_temp', 'degrees_of_separation')|float > 0 %}
{{ state_attr('sensor.nest_time_to_temp', 'elapsed_seconds')|float / state_attr('sensor.nest_time_to_temp', 'degrees_of_separation')|float }}
{% else %}
0
{% endif %}
current_temp: >
{{ states('sensor.living_room_temperature') }}
target_temp: >
{{ state_attr('climate.living_room','temperature') }}
last_estimated_time_to_temp: >
{{ ((state_attr('sensor.nest_time_to_temp','temp_at_last_change_from_heat')|float - state_attr('sensor.nest_time_to_temp','temp_at_last_change_to_heat')|float) * state_attr('sensor.nest_time_to_temp','rate_of_change')|float)|abs|timestamp_custom('%H:%M:%S', false) }}
nest_hvac_mode:
friendly_name: Living Room HVAC Mode
value_template: >
{{ states('climate.living_room') }}
attribute_templates:
hvac_off: >
{% if (states('sensor.living_room_hvac_mode') == 'off') %} ✔ {% else %} {% endif %}
hvac_heat: >
{% if (states('sensor.living_room_hvac_mode') == 'heat') %} ✔ {% else %} {% endif %}
hvac_cool: >
{% if (states('sensor.living_room_hvac_mode') == 'cool') %} ✔ {% else %} {% endif %}
hvac_auto: >
{% if (states('sensor.living_room_hvac_mode') == 'auto') %} ✔ {% else %} {% endif %}
nest_time_to_temp_message:
friendly_name: Living Room Time to Temp
value_template: >
{% if state_attr('climate.living_room','hvac_action') == 'off' and states('climate.living_room') == 'heat' %}
HEAT SET TO
{% elif state_attr('climate.living_room','hvac_action') == 'off' and states('climate.living_room') == 'cool' %}
COOLING SET TO
{% else %}
{{ state_attr('sensor.nest_time_to_temp','message') }}
{% endif %}
attribute_templates:
hvac_action: >
{{ state_attr('climate.living_room','hvac_action') }}
# Coffee Machine
coffee_machine:
friendly_name: "Coffee Machine"
unique_id: "kenmore-coffee-machine"
value_template: >-
{% if is_state('switch.coffee_maker_switch', 'off') %}
Off
{% elif states('sensor.coffee_maker_power_electric')|float > 1.0 %}
Brewing
{% elif is_state('input_boolean.coffee_prepared', 'off') %}
Not Ready
{% else %}
Ready
{% endif %}
washer_status:
friendly_name: Washer Status
value_template: >-
{%- if is_state('switch.washer_power_switch','off') %}
Off
{% elif states ('sensor.washer_power_electric_a')|float > 6.7 %}
Spin
{% elif states ('sensor.washer_power_electric_a')|float > 5.75 %}
Wash
{% elif states ('sensor.washer_power_electric_a')|float > 5.5 %}
Drain
{% elif states ('sensor.washer_power_electric_a')|float > 0 %}
Fill
{% else %}
Idle
{%- endif %}
Door Locks
frontdoor_lock_log_date:
friendly_name: "Frontdoor Lock Log"
value_template: '{{ states.sensor.front_door_lock_logging.attributes["TimeStamp"] | timestamp_custom("%d/%m/%y %H:%M:%S") }}'
frontdoor_lock_log_message:
friendly_name: "Frontdoor Lock Log"
value_template: '{{ states.sensor.front_door_lock_logging.attributes["Value"].split("\t")[1].split(":")[1].strip() }}'
frontdoor_lock_log_user:
friendly_name: "Frontdoor Lock Log"
value_template: '{{ states.sensor.front_door_lock_logging.attributes["Value"].split("\t")[2].split(":")[1].strip() }}'
frontdoor_lock_log_code:
friendly_name: "Frontdoor Lock Log"
value_template: '{{ states.sensor.front_door_lock_logging.attributes["Value"].split("\t")[3].split(":")[1].strip() }}'