Skip to content

Instantly share code, notes, and snippets.

@mdegat01
Created March 17, 2022 00:52
Show Gist options
  • Save mdegat01/f3d1036ce9db7db53d27a026b5fe3ffb to your computer and use it in GitHub Desktop.
Save mdegat01/f3d1036ce9db7db53d27a026b5fe3ffb to your computer and use it in GitHub Desktop.
IDs as variables
template:
binary_sensor:
- trigger:
- id: bedroom__
platform: state
entity_id:
- sensor.bedroom_illuminance_lux_stats
- sensor.bedroom_light_sensor_illuminance_lux
- binary_sensor.daytime
- binary_sensor.fallback_bright_inside
- id: bedroom____
platform: event
event_type: restore_state
binary_sensor:
- name: Bright bedroom
unique_id: "cdd8d74ab466499fb0d45fab796bf552"
device_class: light
state: &bright-sensor-state >-
{% set parts = trigger.id.split('__') %}
{% set room = parts[0] %}
{% set light_factor = parts[1] | int(1) %}
{% set lux = states('sensor.' ~ room ~ '_illuminance_lux_stats') | float(-1.0) %}
{% set raw_lux = states['sensor.' ~ room ~ '_light_sensor_illuminance_lux'] %}
{% set time_threshold = now() - timedelta(hours=1) %}
{% set threshold = state_attr('light.' ~ room ~ '_front', 'brightness') | int(0) / 5 * light_factor + 75 %}
{% set daytime = is_state('binary_sensor.daytime', 'on') %}
{% if daytime and lux >= 0 %}
{{ lux >= threshold }}
{% elif daytime and raw_lux.state | float(-1.0) >= 0 and raw_lux.last_changed >= time_threshold %}
{{ raw_lux.state | float >= threshold }}
{% else %}
{{ is_state('binary_sensor.fallback_bright_inside', 'on') }}
{% endif %}
- trigger:
- id: family_room__
platform: state
entity_id:
- sensor.family_room_illuminance_lux_stats
- sensor.family_room_light_sensor_illuminance_lux
- binary_sensor.daytime
- binary_sensor.fallback_bright_inside
to:
- id: family_room____
platform: event
event_type: restore_state
binary_sensor:
- name: Bright family room
unique_id: "f73cec3a9fcd4891b0da97f9aa6ad8cb"
device_class: light
state: *bright-sensor-state
- trigger:
- id: guest_room__
platform: state
entity_id:
- sensor.guest_room_illuminance_lux_stats
- sensor.guest_room_light_sensor_illuminance_lux
- binary_sensor.daytime
- binary_sensor.fallback_bright_inside
- id: guest_room____
platform: event
event_type: restore_state
binary_sensor:
- name: Bright guest room
unique_id: "6829c0280e944645af821fc6b711e11f"
device_class: light
state: *bright-sensor-state
- trigger:
- id: kids_room__
platform: state
entity_id:
- sensor.kids_room_illuminance_lux_stats
- sensor.kids_room_light_sensor_illuminance_lux
- binary_sensor.daytime
- binary_sensor.fallback_bright_inside
- id: kids_room____
platform: event
event_type: restore_state
binary_sensor:
- name: Bright kids room
unique_id: "0e583f74a9064e76a162c9afe19868c2"
device_class: light
state: *bright-sensor-state
# Since kitchen light is aimed straight at the sensor, they add about 0.6 per brightness instead of 0.2
- trigger:
- id: kitchen__3
platform: state
entity_id:
- sensor.kitchen_illuminance_lux_stats
- sensor.kitchen_light_sensor_illuminance_lux
- binary_sensor.daytime
- binary_sensor.fallback_bright_inside
- id: kitchen__3__
platform: event
event_type: restore_state
binary_sensor:
- name: Bright kitchen
unique_id: "b37ed65104ae4a1cb4d84662443e5bba"
device_class: light
state: *bright-sensor-state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment