~/blog/posts on  live via 󱄅 impure (blog-env)
cat window-notification.md

Window Notification

I attempt to add a custom HomeAssistant notification to tell me when I should open up my upstairs windows.

I generally don't like AC, as I live in Michigan and I would like to enjoy the heat while it's here. This does not mean I like to go upstairs and have it feel like I've opened up an oven's door. As an attempt to reduce that uncomfortable feeling, I want to set up an automation to notify me when I should open the windows.

To do this there's two pre-reqs:

  • A Thermostat
  • A way to get the outside temperature, I use the National Weather Service integration

Hardware #

While I do own a Nest, there's a two problems:

  1. It requires the cloud, which I try to avoid
  2. It's downstairs

Instead, I bought a Tuya compatible thermostat from Amazon. I also use the HACS Plugin Local Tuya to communicate with my Tuya devices directly instead of through the cloud

Be aware: This thermostat with Local Tuya outputs in C, there is extra HA configuration to get it as F.

Setup #

Note #1: I recommend connecting your Tuya API with Local Tuya. This generally makes the install process a little easier as it prefills some of the required fields, like Local Key.

Note #2: In the past month moved all my IoT devices into their own VLAN, which doesn't support the Tuya API due to the discovery being blocked, I'm editing this section to have more detail. (2023-12-28)

The process is the same as adding any device through Local Tuya. That being entities are not fun, as generally it's a slightly informed guessing game. There are three values: two numbers and a boolean.

Device ID (IoT Tuya) #

  1. Login to https://auth.tuya.com/
  2. Sidebar, Cloud -> Development
  3. Table, Open Project
  4. Top bar, Devices
  5. Table, find Sensor
  6. Copy Device ID

Local Key (IoT Tuya) #

  1. Login to https://auth.tuya.com/
  2. Sidebar, Cloud -> API Explorer
  3. It should load into IoT Core, else change in top left dropdown
  4. Sidebar, Device Management -> Query Device Details
  5. Paste Device ID + Submit (hidden by a cookie warning)
  6. Find local_key key in the JSON dictionary it returns

HA Local Tuya Add #

  1. Name: {anything}
  2. Host: {static_ip}
  3. Device ID: {saved from earlier}
  4. Local Key: {saved from earlier}
  5. Protocol Version: 3.3
  6. Submit

Temperature #

The raw value is in C and has a scaling factor of .1, The number will be an integer (no decimals) and will have three digits

  • Name: Upstairs Temperature
  • Unit of Measurement: blank
  • Device Class: temperature
  • Scaling: .1

Humidity #

I don't have an example of the raw value so use the process of elimination. I believe it's either 0-100 or 0-.1

  • Name: Upstairs Humidity
  • Unit of Measurement: %
  • Device Class: humidity
  • Scaling: blank

Boolean #

I presume the boolean is is_fahrenheit. This has no impact so I didn't add it.


Fahrenheit #

As stated above, Temperature is always being coming in a Celsius, so I had to convert it into Fahrenheit for my own use by using a sensor template

I added this block to my main configuration, reloaded the configuration and then the upstairs_temperature_template entity exists

configuration.yaml
sensor:
 - platform: template
   sensors:
      upstairs_temperature_template:
        friendly_name: "Upstairs Temperature"
        unit_of_measurement: '°F'
        value_template: >-
          {% set t = states('sensor.upstairs_temperature') | float %}
          {{((t)*9/5)+32}}  

Main Automation #

The Trigger #

There's three potential triggers you could use:

  1. When the outside temperature changes
  2. When the upstairs temperature changes
  3. On a timer

After some thought I will go with a timer. The Tuya Thermostat updates too often and the outside temperature updates every 10 minutes. Using a timer, or Time Pattern in HomeAssistant terms, is more natural to me as it's works the same as a Cron.

Trigger, Time Pattern:

  • Hours: *
  • Minutes: /10
  • Seconds: blank

This will run every 10 minutes

Note: Changed to a hardcoded Time trigger in the

next post

The Conditions #

To make this more difficult on myself, joking, I have specific specifications when I want the notification:

a. Once per day b. When the upstairs temperature is above 75F c. When the outside temperature is below the upstairs temperature d. Not in the middle of the night

a. Once Per Day #

I made a toggle helper entity, inside setting > devices & services helpers, and named it Temperature Alert.

Condition, State

  • Entity: input_boolean.temperature_alert
  • Attribute: blank
  • State: Off

b. Upstairs is above 75F #

Condition, Numerical State

  • Entity: sensor.upstairs_temperature_template
  • Above:
    • Fixed Number
    • 75
  • Below: blank

c. Outside is below Upstairs #

Condition, Template Condition

{{ states('sensor.upstairs_temperature_template') > states('sensor.KMOP_temperature') }}

d. Not in the Middle of the Night #

Condition, Time

  • After
    • Fixed Time
    • 4:30pm
  • Before
    • Fixed Time
    • 11:59pm

Note: Removed in the next post

The Actions #

Two actions need to happen:

a. My iPhone should get a notification through the HomeAssistant App b. The Temperature Alert boolean should be toggle on

a. Notification #

Action, Call a Service

  • Service: Notifications: Send Notification via phone_entity
  • Message: Open the Windows

a. Boolean #

Action, Call a Service

  • Service: Input Boolean: Turn On
  • Targets: input_boolean.temperature_alert

Raw YAML


Reset Boolean Automation #

We need to reset the Temperature Alert boolean everyday, this is easy in comparison to the main automation

The Trigger #

Trigger, Time:

  • Fixed Time
  • Time: 1:30am

The Action #

Action, Call a Service

  • Service: Input Boolean: Turn Off
  • Targets: input_boolean.temperature_alert

Raw YAML


Switch #

After swapping all my devices to a VLAN, I've had to re-add all my LocalTuya devices without help from their Discovery. Here were my steps.

Note: this pre-assumes you've done the work to get the normal Tuya integration set up. That is have a iot.tuya account and app.

Conclusion #

While the general idea works there is room for improvement; Mainly because I don't want to open my windows when the temperature is dropping to 51F, which happens a lot in Michigan. I've made this a series and will hopefully re-visit this idea.

Edit: markdownlint, textlint and meta TOML -> YAML

~/blog/posts on  live via 󱄅 impure (blog-env)
_