From 488309d61077ce24e2c16d93cbc21eccc18957b6 Mon Sep 17 00:00:00 2001 From: ccostan Date: Wed, 6 Jun 2018 19:00:28 -0400 Subject: [PATCH] Updated with @Brianhanifin 's excellent code updates! New speech helpers and formatted light codes! --- config/packages/light_sensor.yaml.old | 19 ------------- config/packages/speech_helper_sensors.yaml | 31 ++++++++++++++++++++++ config/script/speech_engine.yaml | 23 +++++++++++----- 3 files changed, 48 insertions(+), 25 deletions(-) delete mode 100755 config/packages/light_sensor.yaml.old create mode 100755 config/packages/speech_helper_sensors.yaml diff --git a/config/packages/light_sensor.yaml.old b/config/packages/light_sensor.yaml.old deleted file mode 100755 index ae45b1a1..00000000 --- a/config/packages/light_sensor.yaml.old +++ /dev/null @@ -1,19 +0,0 @@ -#------------------------------------------- -# Bruh Light Sensor Related Packages -#------------------------------------------- -#------LightSensor Node---http://amzn.to/2oUgj5i -homeassistant: - customize: - sensor.sn1_ldr: - icon: mdi:sensor - friendly_name: Light Sensor - #emulated_hue_hidden: False - hidden: False -#---Sensor for Light----------------------------- -sensor: - - platform: mqtt - state_topic: "home/sensornode1" - name: "SN1 LDR" - ##This sensor is not calibrated to actual LUX. Rather, this a map of the input voltage ranging from 0 - 1023. - unit_of_measurement: "LUX" - value_template: '{{ value_json.ldr }}' diff --git a/config/packages/speech_helper_sensors.yaml b/config/packages/speech_helper_sensors.yaml new file mode 100755 index 00000000..3630ebb9 --- /dev/null +++ b/config/packages/speech_helper_sensors.yaml @@ -0,0 +1,31 @@ +#------------------------------------------- +# @BrianHanifin's Light sensor speech helper. - https://gist.github.com/brianhanifin/1f9436c7b6c28917c9be02605b6cff74 +#------------------------------------------- +#------LightSensor Node---http://amzn.to/2oUgj5i +homeassistant: + customize: + sensor.lights_on: + icon: mdi:sensor + friendly_name: Lights on + #emulated_hue_hidden: False + hidden: true +#---Sensor for Light----------------------------- +sensor: + - platform: template + sensors: + lights_on: + value_template: > + {% macro get_lights_on() -%} + {%- for group in states.group|groupby('state') -%} + {%- for entity in group.list -%} + {%- if entity.state == 'on' + and entity.name.split(' ')[1]|lower == 'lights' + and entity.name.split(' ')[0]|lower != 'interior' + and entity.name.split(' ')[0]|lower != 'all' + -%} + {{ entity.entity_id }}{{ ' ' }} + {%- endif -%} + {%- endfor -%} + {%- endfor -%} + {%- endmacro %} + {{ get_lights_on()|trim|replace(' ', ',') }} diff --git a/config/script/speech_engine.yaml b/config/script/speech_engine.yaml index ef3a58fd..85da7506 100755 --- a/config/script/speech_engine.yaml +++ b/config/script/speech_engine.yaml @@ -74,16 +74,27 @@ speech_engine: {%- macro outside_weather() -%} Outside, it is going to be {{ states('sensor.dark_sky_minutely_summary') }} {%- endmacro -%} - + + {%- macro friendly_list(list) -%} + {%- set temp = list|replace('_',' ')|replace(' ,',',')|replace(',',', ')|trim|title -%} + {%- if list.split(',')|length > 1 -%} + {%- set temp = temp+'^' -%} + {%- set lastitem = temp.split(',')|last -%} + {{ temp|replace(lastitem, ' and' + lastitem)|replace('^','') }} + {%- else -%} + {{ temp }} + {%- endif -%} + {%- endmacro -%} + {%- macro light_check() -%} - {% if states.group.all_lights.state != 'off' -%} - There are - {%- set lights_on_count = sensor.lights_on_friendly|length + 1 -%} + {%- if states.sensor.lights_on.state|length != 0 -%} + {%- set lights_on_count = states.sensor.lights_on.state.split(',')|length -%} + {%- set lights_on_friendly = friendly_list(states.sensor.lights_on.state|replace('light.','')|replace('switch.','')|replace('light','')|replace('lamp','')) -%} {%- if lights_on_count == 1 -%} - The {{ sensor.lights_on_friendly }} is the only light on right now. + The {{ lights_on_friendly }} light is on. {%- else -%} There are {{ lights_on_count }} lights on right now. - The {{ sensor.lights_on_friendly }} are on. + The {{ lights_on_friendly }} lights are on. {%- endif -%} {%- endif -%} {%- endmacro -%}