Migrate more blog posts
All checks were successful
Build release image / build (push) Successful in 1m30s

This commit is contained in:
yehor 2025-05-20 13:24:39 +03:00
parent 7445bd02a2
commit e02ced287b
14 changed files with 299 additions and 0 deletions

View File

@ -0,0 +1,211 @@
---
slug: using-variables-as-configuration-for-home-assistant-automation
title: "Using variables as configuration for Home Assistant automation"
tags: [smart home, Home Assistant, home automation]
image: /img/blog/2020/11/variables.jpeg
---
Home Assistant 0.115 got the ability to use variables in automation and scripts. You can declare variables even using templates and use them across the script or automation.
Here is an example from the release notes:
```yaml
automation:
trigger:
platform: sun
event: sunset
offset: -00:30
variables:
notification_service: notify.paulus_iphone
action:
- service: "{{ notification_service }}"
data:
message: Beautiful sunset!
```
<!-- truncate -->
It is very useful and fun feature but at first I didnt realize how powerful it is. Today I want to show you an example how you can use variables as some sort of configuration for automation to avoid creating a lot of `if`s and automation to handle identical tasks.
I have a lot of ZigBee wall switches. Previously I had a separate automation to handle each of them. Every automation was handling:
- First button press toggle the lights with max brightness
- Second button press toggle the lights with low brightness
- First button long press increase the brightnress
- Second button long press decrease the brightness
After implementing `variables` for automation I decided to store the mapping of device id, event (represents the button pressed and the type of press: long or short), corresponding brightness and light `entity_id` in `variables` like this:
```yaml
variables:
device_map:
00:15:8d:00:02:cb:1a:9b:
light_entity_id: light.kitchen
brightness_buttons:
1001: -30
1002: 50
2001: 30
2002: 100
00:15:8d:00:02:c9:39:62:
light_entity_id: light.hallway
brightness_buttons:
1001: -30
1002: 55
2001: 30
2002: 100
00:15:8d:00:02:e9:fe:9e:
light_entity_id: light.living_room
brightness_buttons:
1001: 30
1002: 100
2001: -30
2002: 60
00:15:8d:00:02:cb:64:b4:
light_entity_id: light.hallway
brightness_buttons:
1001: 30
1002: 100
2001: -30
2002: 60
00:15:8d:00:02:c9:37:7d:
light_entity_id: light.bedroom
brightness_buttons:
1001: 30
1002: 100
2001: -30
2002: 24
00:15:8d:00:02:58:61:74:
light_entity_id: light.bathroom
brightness_buttons:
1001: 30
1002: 100
2001: -30
2002: 40
```
So now I can take the brightness value from my `device_map` by device id and event. Also, I can take `entity_id` of the light each wall switch should control.
Also when Im toggling the light I need to use `brightness_pct` service data to set the brightness, but when changing the brightness I need to use `brightness_step_pct`. So I added another variable to set the brightness service data name:
```yaml
variables:
brightness_attr: '{% if (trigger.event.data[''event''] == 2001) or (trigger.event.data[''event'']== 1001) %}brightness_step_pct{% else %}brightness_pct{% endif %}'
```
So when the button held the event is `2001` or `1001` (first and second button) and I need `brightness_step_pct`. Im getting `brightness_pct` in another case.
Now we can use all these variables in `action`:
```yaml
action:
- data:
'{{brightness_attr}}': '{{ device_map[trigger.event.data[''unique_id'']].brightness_buttons[trigger.event.data[''event'']]}}'
entity_id: '{{ device_map[trigger.event.data[''unique_id'']].light_entity_id}}'
service: light.{% if (trigger.event.data['event'] == 2001) or (trigger.event.data['event']== 1001) %}turn_on{% else %}toggle{% endif %}
```
Here we are using the most of variables we are getting the right brightness value by the `unique_id` of our device and by the `event` that is representing the button and the type of press:
```
{{ device_map[trigger.event.data[''unique_id'']].brightness_buttons[trigger.event.data[''event'']]}}
```
Also, we are getting light `entity_id` by `unique_id` of the switch:
```
{{ device_map[trigger.event.data[''unique_id'']].light_entity_id}}
```
And the full automation:
```yaml
- id: wall_switch_handler
alias: Wall switch handler
trigger:
- event_data:
event: 1001
event_type: deconz_event
platform: event
- event_data:
event: 1002
event_type: deconz_event
platform: event
- event_data:
event: 2001
event_type: deconz_event
platform: event
- event_data:
event: 2002
event_type: deconz_event
platform: event
condition:
- condition: template
value_template: '{{''wall_switch'' in trigger.event.data[''id'']}}'
action:
- data:
'{{brightness_attr}}': '{{ device_map[trigger.event.data[''unique_id'']].brightness_buttons[trigger.event.data[''event'']]}}'
entity_id: '{{ device_map[trigger.event.data[''unique_id'']].light_entity_id}}'
service: light.{% if (trigger.event.data['event'] == 2001) or (trigger.event.data['event']
== 1001) %}turn_on{% else %}toggle{% endif %}
variables:
brightness_attr: '{% if (trigger.event.data[''event''] == 2001) or (trigger.event.data[''event'']
== 1001) %}brightness_step_pct{% else %}brightness_pct{% endif %}'
device_map:
00:15:8d:00:02:cb:1a:9b:
light_entity_id: light.kitchen
brightness_buttons:
1001: -30
1002: 50
2001: 30
2002: 100
00:15:8d:00:02:c9:39:62:
light_entity_id: light.hallway
brightness_buttons:
1001: -30
1002: 55
2001: 30
2002: 100
00:15:8d:00:02:e9:fe:9e:
light_entity_id: light.living_room
brightness_buttons:
1001: 30
1002: 100
2001: -30
2002: 60
00:15:8d:00:02:cb:64:b4:
light_entity_id: light.hallway
brightness_buttons:
1001: 30
1002: 100
2001: -30
2002: 60
00:15:8d:00:02:c9:37:7d:
light_entity_id: light.bedroom
brightness_buttons:
1001: 30
1002: 100
2001: -30
2002: 24
00:15:8d:00:02:58:61:74:
light_entity_id: light.bathroom
brightness_buttons:
1001: 30
1002: 100
2001: -30
2002: 40
initial_state: true
mode: parallel
max: 10
```
I have not only wall switches so Im using template condition here to make sure the switch is the wall switch:
```yaml
condition:
- condition: template
value_template: '{{''wall_switch'' in trigger.event.data[''id'']}}'
```
Also, this automation should be able to run in parallel to make it possible to handle several switch clicks at the same time in different rooms by different persons:
```yaml
mode: parallel
max: 10
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 149 KiB

View File

@ -0,0 +1,43 @@
---
slug: bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part
title: "Bed presence detection with Aqara window and door sensor and a cheap car part"
tags: [smart home, Home Assistant, home automation, DIY, Aqara, Xiaomi]
image: /img/blog/2020/12/bed.jpeg
---
Bed presence detection could be very useful in home automation to control the lights and appliances you dont need at night, but want to be ready in the morning. And today we will build a bed presence sensor out of a very universal device for DIY IoT. It is an Aqara ZigBee window and door sensor. Why it is so universal? Lets take a closer look at its internals.
<!-- truncate -->
![internals](internals-1.jpg)
See that little glass tube? Thats a [reed switch](https://en.wikipedia.org/wiki/Reed_switch). It operates under a magnetic field generated by the magnet inside the second part of the sensor. So, in general, it just closes the contact between two points of the electrical circuit. We can unsolder it and replace it with something else. A switch, for example. Or with a car seat pressure sensor, like this one:
![Car seat sensor](seat_sensor.jpg)
It could be found on Amazon or our favorite Chinese store just for 5$. It has two wires that could be closed or opened according to pressure presence. So all we need is to solder this sensor to the Aqara sensors board instead of the reed switch.
![image](solder.jpg)
Ive removed the button cup from the top of the sensor to leave a hole for wires. Just remember to pair your sensor with whatever you are using as a ZigBee hub before doing this.
![image](wires.jpg)
All we need to do now is to put our new sensor under the mattress. I have a bed with wood slats that is narrower than the pressure sensor so I decided to put the sensor on a cardboard sheet
![image](cardboard.jpg)
I made two sensors to separately detect the bed presence of me and my wife. You need to experiment with the sensor position to get the most accurate results. Also, you need to remember that the “open” state of the sensor means “the bed is not occupied” and “closed” means that “the bed is occupied”.
![Bed sensors in Home Assistant UI](bed_sensors.png)
The third sensor on the screenshot is a Home Assistant [template binary sensor](https://www.home-assistant.io/integrations/binary_sensor.template/) that represents unconditional bed occupation. In other words, it is `on` when someone is in bed, and `off` otherwise:
```yaml
binary_sensor:
- platform: template
sensors:
bed_occupancy:
friendly_name: "Bed occupancy"
device_class: occupancy
value_template: "{{is_state('binary_sensor.bed_occupancy_door_side', 'off') or is_state('binary_sensor.bed_occupancy_window_side', 'off')}}"
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

View File

@ -0,0 +1,45 @@
---
slug: send-and-receive-sms-in-home-assistant-with-gsm-modem
title: "Send and receive SMS in Home Assistant with GSM modem"
tags: [smart home, Home Assistant, home automation, SMS, GSM]
image: /img/blog/2021/01/modem.jpg
---
Yes, it is possible and you dont need to build anything from sources. Sending SMS from your home could be useful, for example, to send emergency alerts. But what about receiving and parsing SMS messages? Well, I used it to integrate my car security system with my Home Assistant. Now my Home Assistant could start the engine of my car automatically to warm it up before driving to work.
<!-- truncate -->
First of all, we need to set up notification service via GSM modem following the official [documentation](https://www.home-assistant.io/integrations/sms). This will allow us to send SMS messages by calling the notify service, for example in automation action:
```yaml
action:
- service: notify.sms
data:
message: Hi!
```
But also this integration allows reading SMS messages sent to the modem phone number by listening to the `sms.incoming_sms` event.
```yaml
trigger:
- platform: event
event_type: sms.incoming_sms
```
![Automation trigger in UI](income_trigger.png)
In automation `action`, we can now parse the message by searching keywords in it:
```yaml
action:
- choose:
- conditions:
- condition: template
value_template: >-
{{'engine is off' in trigger.event.data['text']}}
sequence:
- ...
```
![Parsing action](parse_action.png)
Here we are checking for the `engine is off` text in incoming SMS to perform some action. For example, to set the value of some `input_boolean`.

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB