Skip to content

Instantly share code, notes, and snippets.

@kurankat
Last active February 4, 2020 07:40
Show Gist options
  • Save kurankat/bb19908c6a8733a012676f6424c20402 to your computer and use it in GitHub Desktop.
Save kurankat/bb19908c6a8733a012676f6424c20402 to your computer and use it in GitHub Desktop.
Home Assistant sensor, script and automation to play a sound at each changeover of planetary hour during the day
# Planetary hour calculations in sensors.yaml
- platform: template
sensors:
plan_hr_len:
friendly_name: "Planetary hour length today"
unit_of_measurement: "sec"
value_template: >
{{ state_attr('sensor.daylight', 'today') / 12 * 3600 }}
plan_hr_no:
friendly_name: "Planetary hour number"
entity_id: [sensor.time, sensor.sunrise, sensor.plan_hr_len]
value_template: >
{% if states.sensor.sunrise.state is defined
and states('sensor.plan_hr_len') | int > 0 %}
{{ ( ( as_timestamp(now()) - as_timestamp(states('sensor.sunrise')) )
/ ( states('sensor.plan_hr_len') | float ) )
| round (0, "ceil") }}
{% else %}
Initialising
{% endif%}
plan_hr_name:
friendly_name: "Planetary hour name"
value_template: >-
{% set plan_order = ['Moon', 'Saturn', 'Jupiter', 'Mars',
'Sun', 'Venus', 'Mercury'] %}
{% set day_planets = [0, 3, 6, 2,
5, 1, 4] %}
{% set hour = (( day_planets[now().weekday()] )
+ ( states('sensor.plan_hr_no') | int) -1 )
% plan_order | length %}
{{ plan_order[hour] }}
# Script to play correct chime for each planet using Sonos (in scripts.yaml)
planetary_hour_chime:
alias: "Play Planetary hour chime"
sequence:
- service: sonos.snapshot
data_template:
entity_id: "{{ sonos_entity }}"
- service: sonos.unjoin
data_template:
entity_id: "{{ sonos_entity }}"
- service: media_player.volume_set
data_template:
entity_id: "{{ sonos_entity }}"
volume_level: "{{ volume }}"
- service: media_player.play_media
data_template:
entity_id: "{{ sonos_entity }}"
media_content_id: 'http://hassserver.example.com:8123/local/{{ states("sensor.plan_hr_name") }}.mp3'
media_content_type: 'music'
- delay: "{{ delay }}"
- wait_template: "{{ not is_state(sonos_entity, 'playing') }}"
timeout: '00:06:00'
- service: sonos.restore
data_template:
entity_id: "{{ sonos_entity }}"
# Automation to trigger chime play each changeover of a planetary hour
- id: '999999999999'
alias: Planetary Hour
description: Chime Planetary Hour on Change
trigger:
- entity_id: sensor.plan_hr_name
platform: state
condition:
- after: 07:00
before: '21:05'
condition: time
- condition: state
entity_id: group.inhabitants
state: home
- after: sunrise
before: sunset
before_offset: -0:15
condition: sun
action:
- data:
delay: 00:00:6
sonos_entity: media_player.family_room, media_player.unnamed_room
volume: 0.6
service: script.planetary_hour_chime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment