Help with alerts and binary sensor value templates

I’ve got a Schlage lock which reports alarm codes when the lock jams from the sensor sensor.lock_front_door_deadbolt_alarm_type

The codes I want to monitor are 9, 17, 23, 26 and when that code occurs I want to trigger an alert

I created an alerts.yaml which I included in my config file with alert: !include alerts.yaml

here’s my alerts.yaml entry

- front_door_jammed_alert:
    name: Front Door lock is jammed
    message: Front Door lock is jammed
    done_message: Front Door lock is fixed
    entity_id: binary_sensor.front_door_jammed_sensor
    repeat: 10
    can_acknowledge: true
    skip_first: true
    notifiers:
      - notify.ios_garrett_iphone

and here’s my binary sensor which tracks the lock code (also in a split config binary_sensors.yaml)

- platform: template
  sensors:
    front_door_jammed_sensor:
      value_template: "{{ states(sensor.lock_front_door_deadbolt_alarm_type) in ['9', '17', '23', '26'] }}"
      friendly_name: Front door lock is jammed

Where am I going wrong? Edit* fixed my YAML and included the config error I’m getting in HA below:

Invalid config for [alert]: expected a dictionary for dictionary value @ data['alert']. Got [OrderedDict([('front_door_jammed_alert', OrderedDict([('name', 'Front Door lock is jammed'), ('message', 'Front Door lock is jammed'), ('done_message', 'Front Door lock is fixed'), ('entity_id', 'binary_sensor.front_door_jammed_sensor'), ('repeat', 10), ('can_acknowledge', True), ('skip_first', True), ('notifiers', ['notify.ios_garrett_iphone'])]))])]. (See /config/configuration.yaml, line 139). Please check the docs at https://home-assistant.io/components/alert/

I don’t think you’re supposed to have a “-” at the beginning of your alert. So it should just be:

front_door_jammed_alert:
  name: Front Door lock is jammed
  Etc....
next_alert:
  name: Next example alert
  Etc...

Hopefully that solves your problem…

need single quotes around your entity id

- platform: template
  sensors:
    front_door_jammed_sensor:
      value_template: "{{ states('sensor.lock_front_door_deadbolt_alarm_type') in ['9', '17', '23', '26'] }}"
      friendly_name: Front door lock is jammed

and as @bradyn12 said, remove the - before front_door_jammed_alert. That turns the alert section into an order dictionary, which is what the error is complaining about. It’s expecting a normal dictionary.

alert:
  front_door_jammed_alert:
    name: Front Door lock is jammed
    message: Front Door lock is jammed
    done_message: Front Door lock is fixed
    entity_id: binary_sensor.front_door_jammed_sensor
    repeat: 10
    can_acknowledge: true
    skip_first: true
    notifiers:
      - notify.ios_garrett_iphone

@bradyn12 ah ok, I thought that because it was in a separate file split from my config.yaml each alert was supposed to have a “-” at the beginning just list sensors do in a seperated sensors.yaml file. Thanks!

awesome, thanks. @petro can you help me understand when/where to use single quotes and when/where to use double? I’ve read through some YAML page(s) and I’m still struggling with writing my own templates from scratch. Here are a few of the resources I’ve been looking at… any other good ones?

1: https://www.home-assistant.io/docs/configuration/yaml/
2: https://learn.getgrav.org/advanced/yaml
3: https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html

@bradyn12 @petro

hmmmmmm. so the UI says the config is valid but no alert being sent to my phone or to the UI. My binary_sensor is correctly switching to “on” but no alert…

This was in the log:

Log Details (ERROR)
Fri Jan 04 2019 20:20:32 GMT-0600 (CST)

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/alert.py", line 192, in watched_entity_change
    await self.end_alerting()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/alert.py", line 211, in end_alerting
    self._cancel()
TypeError: 'NoneType' object is not callable   

as was this

Log Details (ERROR)
Fri Jan 04 2019 20:20:06 GMT-0600 (CST)

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/alert.py", line 190, in watched_entity_change
    await self.begin_alerting()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/alert.py", line 202, in begin_alerting
    await self._notify()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/alert.py", line 240, in _notify
    await self._send_notification_message(message)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/alert.py", line 258, in _send_notification_message
    DOMAIN_NOTIFY, target, {ATTR_MESSAGE: message})
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1098, in async_call
    raise ServiceNotFound(domain, service) from None
homeassistant.exceptions.ServiceNotFound: (ServiceNotFound(...), 'Service notify.notify.ios_garrett_iphone not found')

you can use single quotes and double quotes interchangeably. The only caveat is that you have to encapsulate single quote in double quotes or vice versa.

If you have single quotes on the isnide of your template, you need to use double quotes to encapsulate it.

take a look at the coloring in the strings below. Everything that is red is detected as a string. Single line templates must be a string. So with the provided example template {{ is_state(‘light.mylight’,‘on’) }}, the following is valid for a value_template:

value_template: "{{ is_state('light.mylight','on') }}"

Now if i try to use double quotes inside a double quote, see what the template detects:

value_template: "{{ is_state("light.mylight","on") }}"

it makes incorrect strings. The same goes for the opposite:

value_template: '{{ is_state('light.mylight','on') }}'

but if i place double quotes inside the single quotes:

value_template: '{{ is_state("light.mylight","on") }}'

for mutli line templates you won’t run into this problem and you can use whatever quote you desire. unless you are trying to place a quote inside a string, then you need to follow the rule again.

1 Like

That makes perfect sense, thanks for the explanation. Everything working now; code below:

alerts.yaml

front_door_jammed_alert:
  name: Front Door lock is jammed
  entity_id: binary_sensor.front_door_jammed_sensor
  state: 'on'
  repeat: 3
  skip_first: false
  message: Front Door lock is jammed
  done_message: Front Door lock is fixed
  notifiers:
    - ios_garrett_iphone

binary_sensors.yaml

- platform: template
  sensors:
    front_door_jammed_sensor:
      value_template: "{{ states('sensor.lock_front_door_deadbolt_alarm_type') in ['9', '17', '23', '26'] }}"
      friendly_name: Front door lock is jammed

configuration.yaml

binary_sensor: !include binary_sensors.yaml
alert: !include alerts.yaml

shoutout to @ptdalen for all his work on the door locks code breaking down the myriad alarm codes and types.

1 Like