This document contains notes on the related components and functionality I added to Home Assistant (HA).
Note that
- in HA there are multiple ways to do things, and what is described below may not be the best way, or may be outdated by the time I finish writing as HA is evolving all the time.
- the examples below are from my HA implementation, containing my Entity and object names. You may want to adjust these to fit your own implementation.
- I will just cover the basics, if you need more information on how to do things please visit the HA Documentation or HA Forum.
- Under "
Advanced
" I provided my current setup, making use of a custom HA card that allows the button icons to be changed based on the entity state.
This is a button in the HA UI that can be used to trigger the ESP32-Cam to take a photo and upload it to the server. Allowing photos to be triggered from the server end makes it possible to programatically take photos from automations, e.g. to take a timelapse series. Or whatever.
The tricky thing here is to make the HA button behave like a "momentary switch", meaning you press it and moments later it returns to the original "off" state.
This can be done using HA Automations.
- Create an "Input Boolean" named "takegatephoto".
InConfiguration -> Helpers
click "Add Helper", select "Toggle", give it a name, then "Create". - Edit
automations.yaml
, and add theTake Gate Photo
automation.
(See theautomations.yaml
code below, and select the appropriate automation.) - Add a Button to HA Lovelace, with the following settings:
entity: input_boolean.takegatephoto
icon: 'mdi:camera-wireless'
tap_action:
action: toggle
You sometimes may want to prevent the PIR Movement sensor from automatically triggering the camera to take and upload a photo when movement is detected. This is typically the case when your mother-in-law is visting. You want to know when she arrives, but you surely don't want a photo of her in your system.
Note that this will only prevent the PIR sensor from automatically triggering the camera. The camera itself is still functional, it can be triggered "manually" (using MQTT) to take a photo, or you can connect to it (with e.g. a browser or MotionEye) to watch a live video stream.
- Create an "Input Boolean" named "input_boolean.gate_enable_camera".
- Edit
automations.yaml
, and add theGateMonitor EnableDisable Camera
automation. - Add a Button to HA Lovelace, with the following settings:
entity: input_boolean.gate_enable_camera
icon: 'mdi:camera'
Sometimes you may want to disable the PIR Movement Sensor from reporting movement, e.g. when you host a big party (post Corona lockdown) and expect a lot of people to visit.
Or.. you may want HA to disable the sensor automatically when you are home and the house is occupied. This can be done using e.g. a Node Red flow or a HA Automation.
- Create an "Input Boolean" named "input_boolean.gate_enable_pir".
- Edit
automations.yaml
, and add theGateMonitor EnableDisable PIR
automation. - Add a Button to HA Lovelace, with the following settings:
entity: input_boolean.gate_enable_pir
icon: 'mdi:motion-sensor'
You may want to confirm the configuration that is currently active on the ESP32-Cam.
The most simple way is to just add a MQTT sensor in HA for the ESP32-Cam configuration, and then view this sensor status in an Entity card. Unfortunately this is not very ellegant, as the configuration is uploaded in JSON format, and therefore a bit messy to see.
Another solution is to display the sensor attributes in e.g. a HA Markdown Card.
- Create a MQTT sensor in
configuration.yaml
to capture the MQTT message containing the ESP32-Cam configuration.
sensor:
- platform: mqtt
name: 'GateMonitor Config'
unique_id: gatemonitorconfig01
state_topic: 'gate/monitor/config'
json_attributes_topic: 'gate/monitor/config'
device:
identifiers:
- MONITORCAM01
- In Lovelace, add a
Markdown Card
, and add the following under Content:
| | Name | Value |
| --- |:---- | -----:|
{%- for attr in states.sensor.gatemonitor_config.attributes %}
{%- if not loop.last %}
{{ '| {} | {} | *{}* |'.format( loop.index , attr , states.sensor.gatemonitor_config.attributes[attr] ) }}
{% endif -%} |
{%- endfor %}
Note: This is the most simple configuration, just to show the basics, and does not result in a very nicely aligned table. You can do your own formating from here. Adding rows to a markdown table in a loop seems not very well supported (in HA) at the moment. See the way I did it under "Advanced" below for a more complex but better looking result.
Below is the content of the automation.yaml
file, for the buttons mentioned above.
You can either add it using the file editor (copy and paste), or else (try to) add it using the HA Automations Visual Editor.
Remember to change the Entity- and other names to match your own implementation!
# Trigger GateMonitor Camera to take photo via MQTT msg
- alias: Take Gate Photo
trigger:
platform: state
entity_id: input_boolean.takegatephoto
to: 'on'
condition: []
action:
- service: mqtt.publish
data:
topic: gate/camera/cmnd
payload: photo
- delay: 0.9
- service: input_boolean.turn_off
entity_id: input_boolean.takegatephoto
#
# Toggle GateMonitor PIR Enable/Disable status via MQTT msg
- alias: GateMonitor EnableDisable PIR
trigger:
platform: state
entity_id: input_boolean.gate_enable_pir
condition: []
action:
- service: mqtt.publish
data_template:
topic: gate/motion/cmnd
payload: "{{ 'enable' if is_state('input_boolean.gate_enable_pir', 'on') else 'disable' }}"
#
# Toggle GateMonitor CAMERA Enable/Disable status via MQTT msg
- alias: GateMonitor EnableDisable Camera
trigger:
platform: state
entity_id: input_boolean.gate_enable_camera
condition: []
action:
- service: mqtt.publish
data_template:
topic: gate/camera/cmnd
payload: "{{ 'enable' if is_state('input_boolean.gate_enable_camera', 'on') else 'disable' }}"
The sketch can periodically publish board status detail using MQTT. This information can then be displayed in Home Assistant as a HA State identity.
The frequency of this state reporting is configurable using the gate/monitor/cmnd
MQTT topic.
- Topic:
gate/monitor/cmnd
- Payload:
interval:10
(for an interval of 10 seconds) - Payload:
ReportState:false
(disable or enable the periodic reporting of the status)
Information include e.g.
- RSSI
- Uptime
- Last Reset Reason
- SoC Core Temperature
Image of the Status
identity in Home Assistant.
Using the Mosquitto MQTT client (mosquitto_pub
), auto-discovery topics can be published to automatically create the ESP32-Cam device and its related sensors in HA.
(These commands could have been added to the sketch, but as this is a one-time action I decided it is not worth the effort and just clutters the final solution with unnecessary functionality that may or may not be used.)
See the MQTT Readme for more detail.
I wanted buttons that allow the icon to be changed based on the state of the switch. This can be done with the custom Button Card. How to install the custom buttons card is detailed in its Github page.
Note: Use custom HA components at your own discresion and risk.
Above: The buttons to manage the Camera, and the ESP32-Cam current configuration settings.
Steps to use the Lovelace configuration used to create the Lovelace view (page).
- Make sure you have a working implementation of the custom
button-card
- Add a new (blank) view to Lovelace
- Add any card (e.g. Button) temporarily. This will be overwritten in step (5).
- Click "`Show Code Editor'"
- Replace whatever is in the Lovelace editor, with the text below.
- It will add grids, and grids in grids, and buttons in those grids, and lastly a markdown card showing the configuration settings.
- Still a work in progress ...
type: grid
columns: 1
square: false
cards:
- type: grid
columns: 1
square: false
cards:
- type: 'custom:button-card'
entity: input_boolean.takegatephoto
icon: 'mdi:camera-wireless'
size: 10%
tooltip: Take Photo and post to Telegram
tap_action:
action: toggle
- type: grid
columns: 2
square: false
cards:
- type: 'custom:button-card'
entity: input_boolean.gate_enable_camera
name: Gate Cam
show_state: true
size: 15%
show_last_changed: true
tooltip: Enable Movement Sensor to take photo
styles:
name:
- font-size: 14px
label:
- color: grey
- font-size: 11px
state:
- font-size: 11px
state:
- value: 'on'
icon: 'mdi:camera'
styles:
icon:
- color: lightgreen
- value: 'off'
icon: 'mdi:camera-off'
styles:
icon:
- color: red
- type: 'custom:button-card'
entity: input_boolean.gate_enable_pir
name: Gate PIR
show_state: true
size: 15%
show_last_changed: true
tooltip: Enable Movement Sensor
styles:
name:
- font-size: 14px
label:
- color: grey
- font-size: 11px
state:
- font-size: 11px
state:
- value: 'on'
icon: 'mdi:motion-sensor'
styles:
icon:
- color: lightgreen
- value: 'off'
icon: 'mdi:motion-sensor-off'
styles:
icon:
- color: red
- type: markdown
content: >
| | Name |
Value
|
| --- |:---- | -----:|
{%- for attr in states.sensor.gatemonitor_config.attributes %}
{%- if not loop.last %}
{{ '| {} |{}| *{}*
|'.format( loop.index , attr ,
states.sensor.gatemonitor_config.attributes[attr] ) }}
{% endif -%} |
{%- endfor %}