Template Sensor Help - Compare two values to give a third

Hi All,

I have 2 binary sensors which are connected to my house alarm, I would like to create a third binary sensor which detects when the alarm is set in night time mode. The two binary sensors operate as follows:

Full set - ON when the alarm is set to away mode
any set - ON when the alarm is set to part, night OR away mode.

I would like a third sensor which turns on when full set is OFF but any set is ON, as this would indicate the alarm is in night mode.

Can anyone help with the config? I have this but its not working

- platform: template
  sensors:
   template_alarm_status:
    value_template: >-
     {%- if is_state("binary_sensor.alarm_any_set", "off") %}
       Alarm Dis-Armed
     {%- elif is_state("binary_sensor.alarm_fully_set", "on") %}
       Manually Locked
     {%- elif is_state("binary_sensor.alarm_any_set", "on","binary_sensor.alarm_fully_set", "off") %}
       Alarm Night Set
     {%- else -%}
        Unknown Status
     {%- endif %}
    icon_template: >-
     {%- if is_state("binary_sensor.alarm_any_set", "off") %}
       mdi:home-lock-open
     {%- elif is_state("binary_sensor.alarm_fully_set", "on") %}
       mdi:home-lock
     {%- elif is_state("binary_sensor.alarm_any_set", "on","binary_sensor.alarm_fully_set", "off") %}
       mdi:home-lock
     {%- else -%}
       mdi:lock-question
     {%- endif %}

This single sensor will only be on if

This sentence leads me to believe you want a 3rd binary_sensor that is only on/off for night mode…

binary_sensor:
- platform: template
  sensors:
   night_mode:
    value_template: >
      {{ is_state('binary_sensor.alarm_any_set','on') and is_state('binary_sensor.alarm_fully_set', 'off') }}

If you are trying to combine them all into a single sensor…

sensor:
- platform: template
  sensors:
    template_alarm_status:
      value_template: >
        {%- if is_state("binary_sensor.alarm_any_set", "on") and is_state("binary_sensor.alarm_fully_set", "off") %}
          Alarm Night Set
        {%- elif is_state("binary_sensor.alarm_any_set", "off") %}
          Alarm Dis-Armed
        {%- elif is_state("binary_sensor.alarm_fully_set", "on") %}
          Manually Locked
        {%- else -%}
          Unknown Status
        {%- endif %}
      icon_template: >
        {%- if is_state("binary_sensor.alarm_any_set", "on") and is_state("binary_sensor.alarm_fully_set", "off") %}
          mdi:home-lock
        {%- elif is_state("binary_sensor.alarm_any_set", "off") %}
          mdi:home-lock-open
        {%- elif is_state("binary_sensor.alarm_fully_set", "on") %}
          mdi:home-lock
        {%- else -%}
           mdi:lock-question
        {%- endif %}

Thanks for the reply,

I have tried your config but I get this error in the log, any ideas?

Update for sensor.template_alarm_status fails
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 221, in async_update_ha_state
    await self.async_device_update()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 347, in async_device_update
    await self.async_update()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/sensor/template.py", line 215, in async_update
    setattr(self, property_name, template.async_render())
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/template.py", line 140, in async_render
    return self._compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 5, in top-level template code
  File "/usr/local/lib/python3.6/site-packages/jinja2/sandbox.py", line 427, in call
    return __context.call(__obj, *args, **kwargs)
TypeError: is_state() takes 3 positional arguments but 5 were given

You are doing something wrong. The code i provided does not use is_state with 5 positional arguements. Your old code does, so you must have copied incorrectly or didn’t copy it at all. Try it again, do not try to write this yourself, just copy what I wrote and overwrite what you have.

This is the line that would be messing up