main
Stuart Lamont 3 years ago
parent 999ab07b85
commit dbcf07cdf1

@ -19,12 +19,6 @@ Reference materials are available at:
[Dad Joke, Quote and Random Fact](./fun.md)
[Pending Updates](./pending%20updates.md)
## Script Contents
The Basis of each Script in Home Assistant is:
```
@ -43,3 +37,9 @@ data:
```
Where we'll then swap out `<YOUR TEMPLATED MESSAGE HERE>` for what we want to actually say.
In this case, I'm using the Alexa Media ACS add-on, but you could reasonably use this with Google home, or ANY text to speech engine from Home Assistant, including
## TO-DO's
- Turn multi-value outputs into an array of dictionaries/tuples and run through the template in a for loop.
- i.e. Indoor Temperatures & pending updates.
-

@ -89,157 +89,6 @@ The forecast high is {{ high_temp }} °C
With a Low of {{ low_temp }} °C
```
__Dining Room Temperature__:
__Service Call__:
```
service: notify.alexa_media_office_echo_dot
data:
message: "
{% set current_temp = state_attr ('weather.home','temperature') | float %}
{% set wind_bearing = state_attr('weather.home', 'wind_bearing') | float %}
{% set wind_speed = state_attr('weather.home','wind_speed') | float %}
{% set current_condition = states('weather.home') %}
{% set current_time = states('sensor.time') %}
{% if current_condition == 'partlycloudy' %}
{% set current_condition = 'Partly Cloudy' %}
{% endif %}
{% set directions = {
(0.0, 11.25): 'North',
(11.25, 33.75): 'North North East',
(33.75, 56.25): 'North East',
(56.25, 78.75): 'East North East',
(78.75, 101.25): 'East',
(101.25, 123.75): 'East South East',
(123.75, 146.25): 'South Eeast',
(146.25, 168.75): 'South South East',
(168.75, 191.25): 'South',
(191.25, 213.75): 'South South West',
(213.75, 236.25): 'South West',
(236.25, 258.75): 'West South West',
(258.75, 281.25): 'West',
(281.25, 303.75): 'West North West',
(303.75, 326.25): 'North West',
(326.25, 348.75): 'North Nort West',
(348.75, 360.0): 'North'
} %}
{% set hour = current_time.split(':')[0] | int %}
{% set minute = current_time.split(':')[1] %}
{% if hour >= 6 and hour < 12 %}
Good morning!
{% elif hour >= 12 and hour < 18 %}
Good afternoon!
{% elif hour >= 18 or hour < 6 %}
Good evening!
{% endif %}
{% set suffix = 'AM' if hour < 12 else 'PM' %}
{% set hour_12 = hour if hour <= 12 else hour - 12 %}
It's currently {{ hour_12 }}:{{ minute }} {{ suffix }}.
Outside, It's {{ current_condition }} and {{ current_temp }} degrees Celsius.
{% set weather = state_attr('weather.home', 'forecast') %}
{% set high_temp = weather[0].temperature %}
{% set low_temp = weather[0].templow %}
{% for entry in weather %}
{% if entry.temperature > high_temp %}
{% set high_temp = entry.temperature %}
{% endif %}
{% if entry.templow < low_temp %}
{% set low_temp = entry.templow %}
{% endif %}
{% if entry.condition == 'rainy'%}
It looks like there's rain expected at around {{ entry.datetime.split('T')[1][:2] }} o'clock. You might want to take an umbrella if you're leaving the house.
{% endif %}
{% endfor %}
{% if high_temp > 30 %}
It's going to be a hot one
{% elif 20 < high_temp < 30 %}
It should be a warm day
{% elif 15 < high_temp < 20 %}
It's going to be a pretty mild temperature today
{% elif 10 < high_temp < 15 %}
It seems like it'll be a bit chilly today
{% elif 0 < high_temp < 10 %}
It's going to be a cold day
{% else %}
It's going to be freezing today
{% endif %}
The forecast high is {{ high_temp }} °C.
With a Low of {{ low_temp }} °C .
{% if low_temp > 30 %}
Wow. That's hot!
{% elif 20 < low_temp < 30 %}
it's not going to go below 20 degrees today.
{% elif 10 < low_temp < 20 %}
it might get a little bit chilly.
{% elif 0 < low_temp < 10 %}
That's cold.
{% else %}
Absolutely freezing!
{% endif %}
{% if wind_speed > 50 %}
Its very windy outside.
{% endif %}
{% for rng, direction in directions.items() %}
{% if wind_bearing >= rng[0] and wind_bearing < rng[1] %}
The wind is currently blowing from the {{ direction }} direction, at {{ wind_speed }} kilometres per hour
{% endif %}
{% endfor %}
Inside it's currently {{ states('sensor.temperature_dining') }} °C in the Dining Room,
{{ states('sensor.lounge_ac_inside_temperature') }} °C in the Lounge,
{{ states('sensor.temperature_office')}} °C in the office,
and {{ states('sensor.master_bedroom_purifier_temperature') }} °C in the Master Bedroom.
The {{ state_attr( 'sensor.quote_sensor', 'category' ) }} Quote of the day is by {{ state_attr( 'sensor.quote_sensor', 'author' ) }}.
They said. {{ states('sensor.quote_sensor') }}
Here is a Dad Joke.. {{ states('sensor.dad_joke_of_the_hour') }}
And a random fact.. {{ states('sensor.random_fact') }}
{% if states('update.home_assistant_operating_system_update') == 'off' %}
There's a Home Assistant O S Update pending.
The Installed version is {{ state_attr('update.home_assistant_operating_system_update', 'installed_version') }}
The Available version is {{ state_attr('update.home_assistant_operating_system_update', 'latest_version') }}
{% endif %}
{% if states('update.home_assistant_core_update') == 'off' %}
There's a Home Assistant Core Update pending.
The Installed version is {{ state_attr('update.home_assistant_core_update', 'installed_version') }}
The Available version is {{ state_attr('update.home_assistant_core_update', 'latest_version') }}
{% endif %}
{% if states('update.esphome_update') == 'off' %}
There's an E S P Home Update pending.
The Installed version is {{ state_attr('update.esphome_update', 'installed_version') }}
The Available version is {{ state_attr('update.esphome_update', 'latest_version') }}
{% endif %}
```
__Abstract it all:__
Break it all into Scripts and trigger them in sequence with the Automation.

@ -1,6 +1,6 @@
__Service Call__:
```
```yaml
service: notify.alexa_media_office_echo_dot
data:
message: "

@ -105,6 +105,8 @@ sensor:
## Announcing Jokes, facts, and quotes
### Starting with the Quote
So for the quote, I want to know the "Category" of quote.
There's a list of categories in the [API Documentation](https://api-ninjas.com/api/quotes).
Expect results like:
@ -125,13 +127,28 @@ And lastly, obviously, the quote:
```
They said. {{ states('sensor.quote_sensor') }}
```
Put altogether, it looks like this.
```
The {{ state_attr( 'sensor.quote_sensor', 'category' ) }} Quote of the day is by {{ state_attr( 'sensor.quote_sensor', 'author' ) }}.
They said. {{ states('sensor.quote_sensor') }}
```
### Now for a Dad Joke
This one is easy. We only need the state of the sensor entity we created:
```
Here is a Dad Joke.. {{ states('sensor.dad_joke_of_the_hour') }}
```
The double stop in the template here adds an additional slight pause to the end of "Here is a Dad Joke". It just seemed to flow better out of the Text To Speech that way
### Lastly, our Random Fact
Again, very simple, only needing to read the State of our Random Fact Entity.
```
And a random fact.. {{ states('sensor.random_fact') }}
```
Again, the double stop seemed to make the timeing better.
### Put All Together
```

@ -0,0 +1,24 @@
# Home Assistant Jinja 2 Templating to announce some inside Temperatures
This process is fairly simple. We don't need to iterate over arrays or anything like that unless we want to get complicated.
For my use case and for the YouTube Video, I chose to read out the temperatures in 4 rooms:
This table shows the rooms and the sensor ID's I chose, your smart home will differ.
| | | |
|--|--|--|
|Dining Room| sensor.temperature_dining| Aqara Temperature and Humidity Sensor
|Lounge Room| sensor.lounge_ac_inside_temperature| Temperature Sensor built into my Loungeroom Air Conditioner
|Office | sensor.temperature_office| Aqara Temperature and Humidity Sensor
|Master Bedroom| sensor.master_bedroom_purifier_temperature| Temp and Humidity Sensor built into my XiaoMi Air Purifier
You should choose the data points that matter the most to you.
You can add humidity data if you want
### The code to announce the states.
```jinja
Inside it's currently {{ states('sensor.temperature_dining') }} °C in the Dining Room,
{{ states('sensor.lounge_ac_inside_temperature') }} °C in the Lounge,
{{ states('sensor.temperature_office')}} °C in the office,
and {{ states('sensor.master_bedroom_purifier_temperature') }} °C in the Master Bedroom.
```

@ -4,25 +4,25 @@
### Get the current Temperature from Home Assistant
We're casting this as a float, probably no need, but I'd rather be safe.
```
```jinja
{% set current_temp = state_attr ('weather.home','temperature') | float %}
```
### Get the current weather condition from Home Assistant as well
```
```jinja
{% set current_condition = states('weather.home') %}
```
### Fix the current Condition
When the Current weather condition is "Partly Cloudy" the default weather integration returns `partlycloudy` and it sounds odd when read out by a Text to Speech engine.
To fix it I added:
```
```jinja
{% if current_condition == 'partlycloudy' %}
{% set current_condition = 'Partly Cloudy' %}
{% endif %}
```
## Announce the current condition and Temperature
```
```jinja
Outside, It's {{ current_condition }} and {{ current_temp }} degrees Celsius.
```
@ -30,14 +30,14 @@ Outside, It's {{ current_condition }} and {{ current_temp }} degrees Celsius.
For weather information, I want to know the High and Low temp. This data is strangely not included by default in the default weather integration, so we have to figure it out ourselves.
### Start by Extracting the `forecast` array data into a variable
```
```jinja
{% set weather = state_attr('weather.home', 'forecast') %}
```
### Instantiate a `high_temp` and `low_temp` variable
We're assigning the temperature from the 0th record in the array to get started.
```
```jinja
{% set high_temp = weather[0].temperature %}
{% set low_temp = weather[0].templow %}
```
@ -46,7 +46,7 @@ We're assigning the temperature from the 0th record in the array to get started.
I've also added an `if` statement in here to announce if any of the forecast conditions are `rainy` and the `hour` that the rain is forecast for and to tell us to take an umbrella.
I've surrounded that code block with `###########` for clarity
```
```jinja
{% for entry in weather %}
{% if entry.temperature > high_temp %}
{% set high_temp = entry.temperature %}
@ -79,7 +79,7 @@ Change them as you need to
else: the `high_temp` variable must be < 0°C
`< 0°C` say "It's going to be freezing today"
```
```jinja
{% if high_temp > 30 %}
It's going to be a hot one
{% elif 20 < high_temp < 30 %}
@ -98,7 +98,7 @@ else: the `high_temp` variable must be < 0°C
### Announce the `high_temp` and `low_temp` values
Again, built for °C
```
```jinja
The forecast high is {{ high_temp }} °C.
With a Low of {{ low_temp }} °C .
```
@ -116,7 +116,7 @@ Remember these are for the `low_temp` variable, so it's not going to get colder
Puntuation like `.`, `!`, `?` and `,` is important in the text to speech statments so the timing sounds "natural"
```
```jinja
{% if low_temp > 30 %}
Wow. That's hot!
{% elif 20 < low_temp < 30 %}
@ -131,7 +131,7 @@ Puntuation like `.`, `!`, `?` and `,` is important in the text to speech statmen
```
## Now we can put it all together:
```
```jinja
{% set current_temp = state_attr ('weather.home','temperature') | float %}
{% set current_condition = states('weather.home') %}

Loading…
Cancel
Save

Powered by TurnKey Linux.