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)
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:
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.
sensor.room_access_status changes state to on.
sensor.service_person detects it is Alpha and changes its state to Alpha.
The check-in sensor displays the check-in time.
Beta taps in.
sensor.room_access_status changes state to off.
sensor.service_person detects it is Beta and changes its state to Beta.
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.
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.