Skip to content

Commit

Permalink
weather sensor cleanup + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhanifin committed Jul 27, 2023
1 parent 0265917 commit 7a0f859
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 256 deletions.
22 changes: 19 additions & 3 deletions custom_templates/speech.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@
{%- endif %}
{%- endmacro %}

{%- macro _outside_forecast() -%}
{%- set forecast = state_attr("weather.home", "forecast") %}
{%- if forecast[0] is defined %}
{%- set hour = now().hour|int(default=0) %}
{%- if hour < 17 -%}
Today's forecast is {{ states('weather.home')|replace('clear','clear ')|replace('partly','partly ') }}, with a high of {{ forecast[0]['temperature']|round(default=0) }}
{%- if forecast[0]['templow']|round(default=0) > 0 %}, and a low of {{ forecast[0]['templow']|round(default=0) }} degrees{%- endif %}.
{%- else -%}
Tomorrow's forecast is {{ forecast[1]['condition']|replace('clear','clear ')|replace('partly','partly ') }}, with a high of {{ forecast[1]['temperature']|round(default=0) }}
{%- if forecast[1]['templow']|round(default=0) > 0 %}, and a low of {{ forecast[1]['templow']|round(default=0) }} degrees{%- endif %}.
{%- endif %}
{%- endif %}
{%- endmacro -%}

{%- macro _outside_weather() -%}
{%- set conditions = {
"clear-night": "clear night skies",
Expand All @@ -167,10 +181,12 @@
} -%}
{%- set condition = conditions[states('weather.home')] %}
{%- set condition = iif(condition is string, condition, states('weather.home')) %}
{%- set starter = ["Currently", "Right now"]|random %}
{%- set temp = state_attr('weather.home','temperature')|int(default=0) %}

{{- starter }}, outside, it is {{ temp }} degrees, with {{ condition }}.

{{- ['Currently', 'Right now, ']|random }} outside, it is
{{ state_attr('weather.home','temperature')|int(default=0) }} degrees fahrenheit,
with {{ condition }}. {{ states("sensor.forecast") }}
{{ _outside_forecast() }}
{%- endmacro -%}

{%- macro _random_pet() %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ device_class: wind_speed
state_class: measurement
unit_of_measurement: "mph"
state: >
{%- set wind_speed = states("sensor.backyard_fence_wind_average_kmh") | float | default(0) / 1.609 | round(1) %}
{{ wind_speed | round(1,default=0) }}
{%- set wind_speed_kmph = states("sensor.backyard_fence_wind_average_kmh") | float | default(0) | round(1) %}
{{ wind_speed_kmph * 0.6213711922 | round(1,default=0) }}
17 changes: 0 additions & 17 deletions integration_entities/templates/sensors/weather/forecast.yaml

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ state_class: measurement
unit_of_measurement: "°F"
# https://community.home-assistant.io/t/calculating-apparent-feels-like-temperature/370834/18
state: >-
{% set T = state_attr("weather.home","temperature")|float(default=0) %}
{% set RH = state_attr("weather.home","humidity")|float(default=0) %}
{% set WS = state_attr("weather.home","wind_speed")|float(default=0) %}
{% set T = states("sensor.outdoor_temperature")|float(default=0) %}
{% set RH = states("sensor.outdoor_humidity")|float(default=0) %}
{% set WS = states("sensor.outdoor_wind_speed")|float(default=0) %}
{% set WC = T | float %}
{% if T <= 70 and WS >= 3 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ state: >
{%- endif %}
{%- endfor %}
{{ outdoor.humidity | round(0,default=0) }}
attributes:
backyard_fence: >
{%- set last_updated = states.sensor.backyard_fence_humidity.last_updated|default(0) %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,41 @@ icon: mdi:brightness-auto
device_class: illuminance
state_class: measurement
unit_of_measurement: lx
state: |
{% set sun_factor = this.attributes.sun_factor|default(0) %}
state: >
{%- set sun_factor = this.attributes.sun_factor|default(0) %}
{%- set condition_factor = this.attributes.condition_factor|default(0) %}
{%- if sun_factor is defined and condition_factor is defined %}
{#- Take an educated guess #}
{%- set illuminance = (sun_factor|float(default=0) * condition_factor|float(default=0))|round(default=10) %}
{%- set illuminance = iif(illuminance < 10, 10, illuminance) %}
{{- illuminance }}
{%- endif %}
attributes:
current_condition: |
{% set factors = namespace(current_condition="") %}
{#- Retrieve the current condition and normalize the value #}
# Retrieve the current condition and normalize the value.
current_condition: >
{%- set weather_sensors = [
"weather.accuweather",
"weather.openweathermap"
] %}
{%- for sensor in weather_sensors if states(sensor) not in ["unknown","unavailable"] and factors.current_condition == "" %}
{%- set factors.current_condition = states(sensor) %}
{%- endfor %}
{%- set current_condition = factors.current_condition|lower|replace("partly cloudy w/ ","")|replace("mostly cloudy w/ ","")|replace("freezing","")|replace("and","")|replace("-", " ")|replace("_", " ")|replace("(","")|replace(")","")|replace(" ", "") %}
{{- current_condition }}
condition_factor: |
{{- states.weather
| selectattr("entity_id","in",weather_sensors)
| rejectattr("state","eq","unavailable")
| rejectattr("state","eq","unknown")
| map(attribute="state")
| reject("eq","")
| first
| lower
| replace("partly cloudy w/ ","")
| replace("mostly cloudy w/ ","")
| replace("freezing","")
| replace("and","")
| replace("-", " ")
| replace("_", " ")
| replace("(","")
| replace(")","")
| replace(" ", "")
}}
condition_factor: >
{% set factors = namespace(condition="") %}
{%- set current_condition = this.attributes.current_condition|default("") %}
Expand All @@ -50,7 +59,7 @@ attributes:
{{- factors.condition }}
{%- endif %}
sun_factor: |
sun_factor: >
{% set factors = namespace(sun="") %}
{%- set right_now = now() | as_timestamp %}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ state: >
{%- endif %}
{%- endfor %}
{{ outdoor.temperature | round(1,default=0) }}
attributes:
backyard_fence: >
{%- set last_updated = states.sensor.backyard_fence_temperature.last_updated|default(0) %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@ name: Outdoor wind speed
device_class: wind_speed
state_class: measurement
unit_of_measurement: "mph"
state: |
{%- set value0 = states("sensor.backyard_fence_wind_average") %}
{%- set value1 = state_attr("weather.accuweather","wind_speed")|trim %}
{%- set value2 = state_attr("weather.openweathermap","wind_speed")|trim %}
{%- set value = value0 %}
{%- if value0 is not number or value0 <= 0 %}
{%- if value1 is number and value1 > 0 %}
{%- set value = value1 %}
{%- elif value2 is number and value2 > 0 %}
{%- set value = value2 %}
state: >
{%- set outdoor = namespace(speed=0) %}
{%- set sources = ["backyard_speed","openweather_speed","accuweather_speed"] %}
{%- for source in sources %}
{%- set temp = this.attributes[source] %}
{%- if outdoor.speed == 0 and temp is number and temp > 0 %}
{%- set outdoor.speed = temp %}
{%- endif %}
{%- endif %}
{{ value|round(default=0) }}
{%- endfor %}
{{ outdoor.speed | round(1,default=0) }}
attributes:
direction: |
direction: >
{%- set value0 = states("sensor.backyard_fence_wind_direction") %}
{%- set value1 = state_attr("weather.accuweather","wind_bearing")|trim %}
{%- set value2 = state_attr("weather.openweathermap","wind_bearing")|trim %}
Expand All @@ -31,3 +28,15 @@ attributes:
{%- endif %}
{%- endif %}
{{ value|round(default=0) }}
backyard_speed: >
{%- set last_updated = states.sensor.backyard_fence_wind_average.last_updated|default(0) %}
{%- set speed = states("sensor.backyard_fence_wind_average")|float(default=0) %}
{{ iif(as_timestamp(now()) - as_timestamp(last_updated) < 300, speed | round(1), "") }}
openweather_speed: >
{%- set last_updated = states.weather.openweathermap.last_updated|default(0) %}
{%- set speed = state_attr("weather.openweathermap","wind_speed")|float(default=0) %}
{{ iif(as_timestamp(now()) - as_timestamp(last_updated) < 300, speed | round(1), "") }}
accuweather_speed: >
{%- set last_updated = states.weather.accuweather.last_updated|default(0) %}
{%- set speed = state_attr("weather.accuweather","wind_speed")|float(default=0) %}
{{ iif(as_timestamp(now()) - as_timestamp(last_updated) < 300, speed | round(1), "") }}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7a0f859

Please sign in to comment.