Skip to content

Instantly share code, notes, and snippets.

@sacdroid
Created August 31, 2021 19:01
Show Gist options
  • Save sacdroid/56b5d9c5f1f3a018d7b18ca95aafbaa5 to your computer and use it in GitHub Desktop.
Save sacdroid/56b5d9c5f1f3a018d7b18ca95aafbaa5 to your computer and use it in GitHub Desktop.
tv time automation
sensor:
- platform: history_stats
name: TV Time Morning
entity_id: media_player.android_tv
state: 'playing'
type: time
start: "{{ now().replace(hour=6).replace(minute=0).replace(second=0) }}"
duration:
hours: 12
- platform: history_stats
name: TV Time Evening
entity_id: media_player.android_tv
state: 'playing'
type: time
start: "{{ now().replace(hour=18).replace(minute=0).replace(second=0) }}"
duration:
hours: 12
- platform: template
sensors:
tv_time_morning_mins:
friendly_name: "Morning TV Time"
value_template: >
{% set time = states('sensor.tv_time_morning') | float %}
{% set minutes = (time * 60) | int %}
{{minutes}}
tv_time_evening_mins:
friendly_name: "Evening TV Time"
value_template: >
{% set time = states('sensor.tv_time_evening') | float %}
{% set minutes = (time * 60) | int %}
{{minutes}}
----------------------------
automation:
- alias: TVTimeOutOffAlert
trigger:
- minutes: /1
platform: time_pattern
- platform: state
entity_id: media_player.android_tv
to: 'playing'
condition:
condition: or
conditions:
- condition: and
conditions:
- condition: template
value_template: "{{ ( states.input_number.tvtime.state | int ) < ( states.sensor.tv_time_morning_mins.state| int ) }}"
- condition: state
entity_id: media_player.android_tv
state: 'playing'
- condition: state
entity_id: input_boolean.parent_override
state: 'off'
- after: '06:00:00'
before: '18:00:00'
condition: time
- condition: and
conditions:
- condition: template
value_template: "{{ ( states.input_number.tvtime.state | int ) < ( states.sensor.tv_time_evening_mins.state| int ) }}"
- condition: state
entity_id: media_player.android_tv
state: 'playing'
- condition: state
entity_id: input_boolean.parent_override
state: 'off'
- after: '18:00:00'
before: '22:30:00'
condition: time
action:
- data:
message: Your TV time is up. Turning Off TV.
service: script.announce_living_room
- service: media_player.turn_off
entity_id: media_player.android_tv
- service: light.turn_on
data_template:
entity_id: light.tv_lightstrip
color_name: "Red"
brightness: 150
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment