Sensor template - How do I combine different entities

Hi! I’m trying to make my motion sensor (Hue) and vibration (Xiaomi/Aqara) sensor to work together to count how many times my cat has visited the litterbox.

Been using only the vibration sensor which works fine together with custom:mini-graph-card in lovelace. But, right now it counts when I empty the litterbox as a visit. So I’ve placed a Hue Motion sensor on the wall. Which works perfect in an automation, now it only tells me to empty the litterbox when the cat has visited instead of doing it all the time the litterbox is touched.

This is the code I’m using to display when the litterbox has been visited by my cat. What should I add to the code below to only count when “binary_sensor.hue_motion_sensor_3_motion” hasn’t felt any motion?

  - platform: template
    sensors:
      kattladan_on:
        friendly_name: "Kattlådan"
        value_template: >-
            {% if is_state('binary_sensor.kattladan', 'on') %} 1 {% else %} 0 {% endif %}

I was thinking of writing it like this, but it hasn’t gotten me any further…

  - platform: template
    sensors:
      kattladan_on:
        friendly_name: "Kattlådan"
        value_template: >-
            {% if is_state('binary_sensor.kattladan', 'on') and is_state('binary_sensor.hue_motion_sensor_3_motion', 'off') %} 1 {% else %} 0 {% endif %}

If I were to build this I would have used a template binary sensor. More or less like this (untested):

binary_sensor:
  - platform: template
    sensors:
      litterbox_used:
        value_template: "{{ is_state('binary_sensor.kattladan', 'on') and is_state('binary_sensor.hue_motion_sensor_3_motion', 'off') }}"

You can validate the value template expression in the developer tools template editor.

Wow, that was all I needed. Thank you metbril, I really appreciate it.

For anyone interested in doing the same thing as me, I’ve included my code for the card. There might be some unnecessary code since I’m not that good. But at least it works :smile: 16

cards:
color_thresholds:
  - color: 'rgba(0,0,0,0.3)'
    value: -1
  - color: '#40e090'
    value: 0.5
  - color: '#82eb82'
    value: 0
decimals: 0
entities:
  - entity: binary_sensor.litterbox_used
    show_state: true
state_map:
  - value: 'off'
    label: Av
  - value: 'on'
    label: På
hour24: true
hours_to_show: 24
name: "\U0001F431"
points_per_hour: 1
show:
  graph: bar
  icon: false
  labels: false
  state: true
style: |
  ha-card{
    padding: 20px;
    padding-bottom: 2px;
  }
  ha-card{
    background: linear-gradient(180deg, rgba(232,248,243,0.1), rgba(255,255,255,0.3)) !important;
  }
  ha-card{
    color: #ccc !important
  }
  ha-card{
    --iron-icon-fill-color: rgba(255,255,255,0.85);
    color: #009999
  }
  div.name.flex{
    color: #000;
    text-align: center;
    margin: 0 auto;
    font-size: 24px;
  }
  .state__value.ellipsis{
    display: none !important;
  }
  .name > span {
    opacity:1 !important;
  }
title: "\U0001F431"
type: 'custom:mini-graph-card'