My MQTT Sensor TEMPLATE comparisson logic not working

Hi HASS fellas!

I have this logic setup in my sensor.yaml file that has been bugging me for days now. Logic is not working (and not really sure if I’m doing it correctly) :frowning:

My setup / scenario:

I have an IoT device that publishes the following json data payload:

{“Time”:“2019-10-09T12:35:07”,“PN532”:{“UID”:“8660A21A”, “DATA”:“”}}
where

  • UID = 86602A21A (is the card ID)
  • DATA= NULL (this is for future use)
  • Time = time on TAP ON or OFF

to the MQTT topic below:

tele/vaultaccess/SENSOR

The payload is coming from an NFC card that is read on TAP ON / TAP OFF.

Monitoring the TAP ON / TAP OFF, I created the following entry in my binary_sensor yaml:

- platform: mqtt
  name: Room Access Status
  device_class: occupancy
  state_topic: tele/vaultaccess/SENSOR
  value_template: "{%if is_state(entity_id,\"on\")-%}OFF{%-else-%}ON{%-endif%}"

The above binary sensor works flawlessly - it toggles ON/OF every time I tap in my card.

Now, I created these entries in my sensor.yaml file

- platform: mqtt
  name:  Check In/ Check Out Time
  state_topic: tele/vaultaccess/SENSOR
  value_template: "{{ as_timestamp (value_json.Time) | timestamp_custom ('%H:%M') }}"

- platform: mqtt
  name: Service Person
  state_topic: tele/vaultaccess/SENSOR
  value_template: >
    {%- if is_state(entity_id, 'on') and value_json.PN532.UID == "8660A21A" %}Alpha
    {%- elif is_state(entity_id,'on') and value_json.PN532.UID == "8660A21B" %}Bravo
    {%- elif is_state(entity_id,'on') and value_json.PN532.UID == "8660A21C" %}Charlie
    {%- elif is_state(entity_id,'on') and value_json.PN532.UID == "8660A21D" %}Delta
    {%- elif is_state(entity_id,'on') and value_json.PN532.UID == "8660A21E" %}Echo
    {%- elif is_state(entity_id,'off') %}Vacant
    {%- else -%}null
    {%- endif -%}

my interpretation here is that (yeah, i am not really sure if it will work (or how will it work)… not really a coder):

  • on initial TAP, the entitity ID will change its state to ON,
  • gets evaluated if ON (TRUE), with value_jason evaluated against the above mapping,
    let’s say if UID == 8660A21A (using the same card, TRUE), then it will show “Alpha”
  • then when a card is tapped in again, the NFC card data will then send the same MQTT payload, toggling the topic state to OFF, then regardless of what UID is provided the value_template will return with “Vacant”

My requirement at the end of the day:

  • if I tapped in my card first time, the system will say “I’m detected” (using the binary sensor)
  • and with the sensor capturing my tap in time and my ID / displaying my name
  • I tapped in again, the binary sensor clears me out, with my ID cleared out, and the time captured.

your guidance and wisdom will be highly appreciated.

On first glance, the sensors you’ve defined appear to support the requirements (with the possible exception of the Service Person sensor). However, let’s carry out this thought experiment: What happens when Alpha arrives and then Beta arrives.

Alpha taps in.

  1. sensor.room_access_status changes state to on.
  2. sensor.service_person detects it is Alpha and changes its state to Alpha.
  3. The check-in sensor displays the check-in time.

Beta taps in.

  1. sensor.room_access_status changes state to off.
  2. sensor.service_person detects it is Beta and changes its state to Beta.
  3. The check-in sensor displays the check-in time.

So the second person to arrive will change the room status from on to off. The third will switch it back to on. I don’t think this is how you want the occupancy detection to work.

Hi Taras,

I agree with your point. That’s actually my next step to explore.

Right now, for a single card “Alpha” user, the name wont show up at all (always showing “Unknown”. Seems like the

    {%- if is_state(entity_id, 'on') and value_json.PN532.UID == "8660A21A" %}Alpha

is not even evaluating the logic.

I did some validation earlier just to make sure the Value Template is working, removing the topic state part, e.g.

    {%- if  value_json.PN532.UID == "8660A21A" %}Alpha

And the value template works OK.

So, back to my challenge, doing the logical AND check does not work…
Any idea what am I missing?

I offer you another approach.

Each NFC card (i.e. each user) is handled by its own binary_sensor. Each sensor reports that particular user’s status (in or out) and when they checked in/out.

Define a binary_sensor for each NFC card (user):

  - platform: mqtt
    name: 'Alpha'
    state_topic: 'user/8660A21A'
    value_template: "{{ 'OFF' if is_state(entity_id, 'on') else 'ON' }}"
    device_class: occupancy
    json_attributes_topic: 'user/8660A21A'

  - platform: mqtt
    name: 'Bravo'
    state_topic: 'user/8660A21B'
    value_template: "{{ 'OFF' if is_state(entity_id, 'on') else 'ON' }}"
    device_class: occupancy
    json_attributes_topic: 'user/8660A21B'

  - platform: mqtt
    name: 'Charlie'
    state_topic: 'user/8660A21C'
    value_template: "{{ 'OFF' if is_state(entity_id, 'on') else 'ON' }}"
    device_class: occupancy
    json_attributes_topic: 'user/8660A21C'

Create one new automation:

- alias: 'NFC demux'
  trigger:
  - platform: mqtt
    topic: tele/vaultaccess/SENSOR
  action:
  - service: mqtt.publish
    data_template:
      topic: 'user/{{trigger.payload_json.PN532.UID}}'
      payload: >-
        {"time":"{{trigger.payload_json.Time}}"}

Optionally create a group containing the binary_sensors:

  users:
    name: Users
    entities:
      - binary_sensor.alpha
      - binary_sensor.bravo
      - binary_sensor.charlie

I’ve tested it and it works. Let me know if you want me to explain its operation.

Look at the states you are creating for the Service Person sensor:
Alpha, Bravo, Charlie, etc, Vacant, null

Now look at the state you’re testing for in the template:

is_state(entity_id,'on')

You’re looking for that sensor’s state to be on.

When will that sensor’s state ever be on?
Never.

Hi Taras, thanks for this option. Let me try this on my setup and will let you know the outcome.

Hi @123 Taras,

THANKS HEAPS!!!

works perfectly, your recommended approach even looked after the multicard/multiuser tapping in and out from the same sensor.

Doing my code adjustment now

cheers!

1 Like