Help adding a nfc tag to script

I have the following script that runs when I tell my Google Device “I’m going to bed”.

I keep forgetting to take my pill late at night. I have an NFC tag on my pill bottle that I named “PM” and added to Home Assistant.

I would like to add it to the script so the script will NOT run UNLESS I have scanned that NFC tag within an hour or two before. My bedtime varies, and I’m lazy and only take that pill when I get up to go to bed and first go to the kitchen - that’s when I take that pill and then I tell Google. But sometimes I take the pill and get distracted (dogs, cats, whatever) and a half hour, hour, can go by before I tell Google I’m going to bed.

I can’t figure out, though, how to add it. I appreciate any help!

alias: Withay bedtime
sequence:
  - type: turn_off
    device_id: 6913ea93ddd2995d6f40a19d0ae307d8
    entity_id: light.left_cabinet_lights
    domain: light
  - type: turn_off
    device_id: 11bc4142e6a7b10295d53c54b22d92dc
    entity_id: light.right_cabinet_lights
    domain: light
  - service: alarm_control_panel.alarm_arm_home
    data:
      code: "XXXX"
    target:
      entity_id: alarm_control_panel.home_alarm
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 547227899e48fdc98b5e0577d718f2d7
    entity_id: switch.sink_light
    domain: switch
mode: single
icon: mdi:bed

You can either use a condition or a wait for trigger

I guess I knew that, but I’m not sure HOW. I write these in yaml.

Ahh, that makes sense.

NFC tags aren’t entities in HA, so you can’t access them directly using something like a state condition.

The simplest solution would be to set up an input_datetime for “tag last scanned”, and then a separate automation which updates that input_datetime whenever you scan the tag. Then the condition in your script can check whether the input_datetime is within the last two hours.

Another option would be to switch things around, so your script is triggered when you scan the NFC tag, and then it uses a wait for condition to wait until you tell google that you’re going to bed. The downside of this is that if you restart HA between taking your pills and going to bed, then the script won’t trigger, and you’d have to manually trigger it (or scan the NFC tag again).

Oh that’s a clever solution, yeah. I can’t see me restarting HA that late, so this could work. Thanks!