diff --git a/blog/2020-11-10-using-variables-as-configuration-for-home-assistant-automation/index.md b/blog/2020-11-10-using-variables-as-configuration-for-home-assistant-automation/index.md new file mode 100644 index 0000000..65bf162 --- /dev/null +++ b/blog/2020-11-10-using-variables-as-configuration-for-home-assistant-automation/index.md @@ -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! +``` + +It is very useful and fun feature but at first I didn’t 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 I’m 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`. I’m 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 I’m 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 +``` \ No newline at end of file diff --git a/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/bed_sensors.png b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/bed_sensors.png new file mode 100644 index 0000000..05759be Binary files /dev/null and b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/bed_sensors.png differ diff --git a/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/cardboard.jpg b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/cardboard.jpg new file mode 100644 index 0000000..55b202f Binary files /dev/null and b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/cardboard.jpg differ diff --git a/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/index.md b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/index.md new file mode 100644 index 0000000..b2547a8 --- /dev/null +++ b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/index.md @@ -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 don’t 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? Let’s take a closer look at its internals. + + + +![internals](internals-1.jpg) + +See that little glass tube? That’s 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 sensor’s board instead of the reed switch. + +![image](solder.jpg) + +I’ve 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')}}" +``` \ No newline at end of file diff --git a/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/internals-1.jpg b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/internals-1.jpg new file mode 100644 index 0000000..be2cf47 Binary files /dev/null and b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/internals-1.jpg differ diff --git a/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/seat_sensor.jpg b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/seat_sensor.jpg new file mode 100644 index 0000000..0ea839f Binary files /dev/null and b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/seat_sensor.jpg differ diff --git a/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/solder.jpg b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/solder.jpg new file mode 100644 index 0000000..7b3157e Binary files /dev/null and b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/solder.jpg differ diff --git a/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/wires.jpg b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/wires.jpg new file mode 100644 index 0000000..efd9dc5 Binary files /dev/null and b/blog/2020-12-29-bed-presence-detection-with-aqara-window-and-door-sensor-and-a-cheap-car-part/wires.jpg differ diff --git a/blog/2021-01-06-send-and-receive-sms-in-home-assistant-with-gsm-modem/income_trigger.png b/blog/2021-01-06-send-and-receive-sms-in-home-assistant-with-gsm-modem/income_trigger.png new file mode 100644 index 0000000..1de8067 Binary files /dev/null and b/blog/2021-01-06-send-and-receive-sms-in-home-assistant-with-gsm-modem/income_trigger.png differ diff --git a/blog/2021-01-06-send-and-receive-sms-in-home-assistant-with-gsm-modem/index.md b/blog/2021-01-06-send-and-receive-sms-in-home-assistant-with-gsm-modem/index.md new file mode 100644 index 0000000..58af3c4 --- /dev/null +++ b/blog/2021-01-06-send-and-receive-sms-in-home-assistant-with-gsm-modem/index.md @@ -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 don’t 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. + + + +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`. \ No newline at end of file diff --git a/blog/2021-01-06-send-and-receive-sms-in-home-assistant-with-gsm-modem/parse_action.png b/blog/2021-01-06-send-and-receive-sms-in-home-assistant-with-gsm-modem/parse_action.png new file mode 100644 index 0000000..71e1373 Binary files /dev/null and b/blog/2021-01-06-send-and-receive-sms-in-home-assistant-with-gsm-modem/parse_action.png differ diff --git a/static/img/blog/2020/11/variables.jpeg b/static/img/blog/2020/11/variables.jpeg new file mode 100644 index 0000000..d5c2e26 Binary files /dev/null and b/static/img/blog/2020/11/variables.jpeg differ diff --git a/static/img/blog/2020/12/bed.jpeg b/static/img/blog/2020/12/bed.jpeg new file mode 100644 index 0000000..f3df5fa Binary files /dev/null and b/static/img/blog/2020/12/bed.jpeg differ diff --git a/static/img/blog/2021/01/modem.jpg b/static/img/blog/2021/01/modem.jpg new file mode 100644 index 0000000..9f3a058 Binary files /dev/null and b/static/img/blog/2021/01/modem.jpg differ