You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.8 KiB

######################################################################
# @CCOSTAN - Follow Me on X
# For more info visit https://www.vcloudinfo.com/click-here
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
# -------------------------------------------------------------------
# SleepIQ - Bed presence and Sleep Number metrics
# SleepIQ sensors, helpers, and bed-related automations.
# -------------------------------------------------------------------
# Related Issue: 1568, 1584
# - Blog: https://www.vcloudinfo.com/2026/03/sleep-number-health-dashboard-home-assistant.html
# Notes: "Carlo Snore Mode" (input_boolean) can be voice-controlled via Alexa.
# Preset option strings may differ per bed; adjust "Snore"/"Flat"
# below if needed to match your select entity's options.
# Notes: Single-package preference: keep all SleepIQ device-specific YAML
# (helpers/automations) centralized in this file.
# Notes: `group.bed` is occupancy-only so dependent automations get a clean
# on/off state from the two in-bed binary sensors.
# Notes: The SQL summary averages each day's highest non-zero score over the
# latest 14 calendar days; source SleepIQ scores remain the history.
######################################################################
homeassistant:
customize_glob:
"sensor.*_sleep_hours":
unit_of_measurement: hours
icon: mdi:sleep
# customize:
# sensor.Carlo_sleep_hours:
# friendly_name: 'Carlo Slept last night'
# sensor.Stacey_sleep_hours:
# friendly_name: 'Stacey Slept last night'
#-------------------------------------------
sleepiq:
username: !secret sleepiq_username
password: !secret sleepiq_password
#-------------------------------------------
sql:
- name: Sleep Score 14 Day Averages
unique_id: sleep_score_14_day_averages
icon: mdi:sleep
unit_of_measurement: score
query: >-
SELECT
ROUND(AVG(CASE
WHEN daily.entity_id = 'sensor.sleepnumber_carlo_carlo_sleep_score'
THEN daily.daily_score END), 1) AS carlo,
ROUND(AVG(CASE
WHEN daily.entity_id = 'sensor.sleepnumber_carlo_stacey_sleep_score'
THEN daily.daily_score END), 1) AS stacey,
COUNT(CASE
WHEN daily.entity_id = 'sensor.sleepnumber_carlo_carlo_sleep_score'
THEN 1 END) AS carlo_nights,
COUNT(CASE
WHEN daily.entity_id = 'sensor.sleepnumber_carlo_stacey_sleep_score'
THEN 1 END) AS stacey_nights
FROM (
SELECT
states_meta.entity_id,
DATE(FROM_UNIXTIME(states.last_updated_ts)) AS score_day,
MAX(CAST(states.state AS DECIMAL(5,2))) AS daily_score
FROM states
INNER JOIN states_meta
ON states.metadata_id = states_meta.metadata_id
WHERE states_meta.entity_id IN (
'sensor.sleepnumber_carlo_carlo_sleep_score',
'sensor.sleepnumber_carlo_stacey_sleep_score'
)
AND DATE(FROM_UNIXTIME(states.last_updated_ts)) >=
CURDATE() - INTERVAL 13 DAY
AND CAST(states.state AS DECIMAL(5,2)) > 0
GROUP BY
states_meta.entity_id,
DATE(FROM_UNIXTIME(states.last_updated_ts))
) AS daily
column: carlo
# sensor:
# - platform: history_stats
# name: Carlo_sleep_hours
# entity_id: binary_sensor.sleepnumber_carlo_carlo_is_in_bed
# state: 'on'
# type: time
# end: '{{ now() }}'
# duration:
# hours: 24
#
# - platform: history_stats
# name: Stacey_sleep_hours
# entity_id: binary_sensor.sleepnumber_carlo_stacey_is_in_bed
# state: 'on'
# type: time
# end: '{{ now() }}'
# duration:
# hours: 24
input_boolean:
carlo_snore:
name: Carlo Snore Mode
icon: mdi:bed
group:
bed:
entities:
- binary_sensor.sleepnumber_carlo_carlo_is_in_bed
# - sensor.Carlo_sleep_hours
- binary_sensor.sleepnumber_carlo_stacey_is_in_bed
# - sensor.Stacey_sleep_hours
automation:
- alias: "CARLO-HOMEPC - Lock PC and Sleep Monitors When Carlo Is in Bed"
id: 25c69640-4c38-46a2-82ae-5a32c3c6f1b4
mode: single
trigger:
- platform: state
entity_id: binary_sensor.sleepnumber_carlo_carlo_is_in_bed
from: 'off'
to: 'on'
action:
- service: button.press
target:
entity_id: button.carlo_homepc_carlo_homepc_lock
- service: button.press
target:
entity_id: button.carlo_homepc_carlo_homepc_monitorsleep
- alias: "SLEEPIQ - Carlo Snore On (Foundation Right)"
id: 7bc879bb-9cc9-4b30-8eea-4b15a9fb7b25
mode: single
trigger:
- platform: state
entity_id: input_boolean.carlo_snore
from: "off"
to: "on"
action:
- service: select.select_option
continue_on_error: true
target:
entity_id: select.sleepnumber_carlo_foundation_preset_right
data:
option: "Snore"
- alias: "SLEEPIQ - Carlo Snore Off (Foundation Right Flat)"
id: 2f80d533-73f1-4079-9e91-4375cfabf8dc
mode: single
trigger:
- platform: state
entity_id: input_boolean.carlo_snore
from: "on"
to: "off"
action:
- service: select.select_option
continue_on_error: true
target:
entity_id: select.sleepnumber_carlo_foundation_preset_right
data:
option: "Flat"
- alias: "SLEEPIQ - Auto Disable Carlo Snore When Out Of Bed 20m"
id: 962c51d9-3ca7-4ebb-a516-5fc650b4b1a8
mode: single
trigger:
- platform: state
entity_id: binary_sensor.sleepnumber_carlo_carlo_is_in_bed
from: "on"
to: "off"
for: "00:20:00"
condition:
- condition: state
entity_id: input_boolean.carlo_snore
state: "on"
action:
- service: input_boolean.turn_off
target:
entity_id: input_boolean.carlo_snore

Powered by TurnKey Linux.