Query regarding yaml scripts, rfid tags and logger

Hi,
This is my first post and I hope I am not breaking any rules!

I am trying to set up a bin collection reminder that starts a flashing lamp the evening before bins are due. The lamp should get cancelled when I scan an rfid tag on a bin and should create a log entry in a dedicated log file recording which bin has been put out and when. (I could not find a specific sub category for ‘rfid’ or ‘logger’ so I have tagged this as ‘automation’… seems closest.)

Here’s where I’m at…

  • added three bin tags that do scan and create events (event_type 29 = “tag_scanned”)
  • created helper input_boolean.bin_collection_due_tomorrow
  • The lamp starts flashing when it should
  • The tags scan when scanned

My problems…

  • The lamp does not stop flashing when a tag is scanned
  • The lamp does not stop when the time exceeds the ‘before’ value
  • I have no idea if input_boolean.bin_collection_due_tomorrow is being updated
  • The dedicated scan log file is not being created
  • The system is not creating any generic log errors (it was before some edits/changes)

I have read through lots of posts but can find nothing that helps me and I have probably messed this code up trying changes that seemed to make sense but to no avail. I’m new to HA and though I am no programmer I am more than familiar with python but yaml has my head wrecked and I would appreciate any pointers anyone has to offer…

**This is what I have in automations.yaml (mostly courtesy of ChatGPT) so perhaps some references are totally wrong but it is partially working and not forgetting it has been modified by me to try and get things working . **

 alias: "Alert for Bin Collection"
  trigger:
    platform: time
    at: "23:38:00"  # Start time of the bin collection schedule
  condition:
    condition: time
    after: "16:30:00"  # Start time of the bin collection schedule
    before: "23:59:00"  # End time of the bin collection schedule
    weekday: sun  # (mon,tue etc))
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.bin_collection_due_tomorrow
    - repeat:
        sequence:
          - service: light.turn_on
            data:
              entity_id: light.bin_light_1
          - delay: "00:00:02"  # Adjust the on duration of flashing as needed
          - service: light.turn_off
            data:
              entity_id: light.bin_light_1
          - delay: "00:00:03"  # Adjust the off duration of flashing as needed
        until:
          - condition: state
            entity_id: input_boolean.bin_collection_due_tomorrow
            state: "off"
  initial_state: True  # Default value is True - False disables this automation when HA restarts.

- alias: "Reset Bin Collection Alert"
  trigger:
    platform: event
    event_type: tag_scanned
    event_data:
      tag_id:
        - "024fb0c1-d905-4fa8-a785-bdb0a261aefc"  # Green Bin Tag ID
        - "57c15403-d592-4a72-987a-a558b0b616fa"  # Grey Bin Tag ID
        - "7719c916-07d3-40d5-ab38-d0e29d5220f1"  # General Test Tag
  action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.bin_collection_due_tomorrow
    #- logger.log: "Scanned a tag"

this is what I added to configuration.yaml

logger:
  default: 'INFO'
  logs:
    homeassistant.components.rfid: 'DEBUG'
    tag_scanned: /config/mylogs/rfid_tag_scans.log

You are doing yourself a disservice using ChatGPT… it almost always creates automations that look plausible, but contain errors that are often difficult to find without significant HA experience. If you’re just starting out with HA you will get more out of using the UI Automation editor than you will using any of the LLM AI’s.

Conditions in the condition block are checked immediately after the trigger fires… they are not checked again. If you want the light loop to stop at a certain time add that as a trigger to the automation that controls the input boolean.

alias: "Alert for Bin Collection"
  trigger:
    - platform: time
      at: "16:30:00"  # Start time of the bin collection schedule
  condition: 
    - alias: Check that it is Sunday 
      condition: template
      value_template: "{{ now().strftime('%a') == 'Sun' }}"
  action:
    - service: input_boolean.turn_on
      target:
        entity_id: input_boolean.bin_collection_due_tomorrow
    - repeat:
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.bin_light_1
          - delay: "00:00:02"  # Adjust the on duration of flashing as needed
          - service: light.turn_off
            target:
              entity_id: light.bin_light_1
          - delay: "00:00:03"  # Adjust the off duration of flashing as needed
        until:
          - condition: state
            entity_id: input_boolean.bin_collection_due_tomorrow
            state: "off"
- alias: "Reset Bin Collection Alert"
  trigger:
    - platform: event
      event_type: tag_scanned
      id: "Scanned a Tag"
    - platform: time
      at: "23:59:00"
      id: "Timed Out"
  variables:
    tags:
      - "024fb0c1-d905-4fa8-a785-bdb0a261aefc"  # Green Bin Tag ID
      - "57c15403-d592-4a72-987a-a558b0b616fa"  # Grey Bin Tag ID
      - "7719c916-07d3-40d5-ab38-d0e29d5220f1"  # General Test Tag
  condition:
    - alias: "Check that boolean is still 'on' to avoid false log events"
      condition: state
      entity_id: input_boolean.bin_collection_due_tomorrow
      state: 'on'
    - alias: Check for allowable triggers 
      condition: template
      value_template: "{{ trigger.event.data.tag_id in tags or trigger.platform == 'time' }}"    
  action:
    - service: input_boolean.turn_off
      target:
        entity_id: input_boolean.bin_collection_due_tomorrow
    - service: logbook.log
      data:
        message: "{{ trigger.id }}"
        name: Bin Tags # or Whatever You Want

Thank you Didgeridrew for the prompt response and support!

I had to smile at your opening comment about ChatGPT because it’s on the nail and I have experienced it so many times when looking for some Python code!

Aside from dropping the logging requirement for now, I made one change to the your code because the timeout was not working (the tag_scan was)…

I changed:

      value_template: "{{ trigger.event.data.tag_id in tags or  trigger.platform == 'time'  }}"    

to:

      value_template: "{{  trigger.platform == 'time' or trigger.event.data.tag_id in tags }}"    

I am assuming it was crashing because it was checking for trigger.event.data that didn’t exist because no tag was scanned. Once I reversed the two conditions so that it checked the timeout first it works as expected.

I am forgetting about the log for now as I think the original code suggestion came from ChatGPT and I don’t see a reference to alternate log file in the HA Logger help page so maybe it’s not even a runner.

Again thanks for taking the time to look at this and rewrite the code!

I discovered that the lamp stays on permanently (i.e. not flashing) if HA is rebooted while the alert is active. Out of an abundance of caution I added an automation using the ‘create automation’ gui that turns the lamp off when the system reboots. Which resulted in adding this following code to automations.yaml…

- id: '1715651532608'
  alias: Turn Bin Light Off On Boot
  description: 'Ensure  bin alert lamp is turned off on boot as the light would remain on permanently if HA rebooted while the bin alert was active. '
  trigger:
  - platform: homeassistant
    event: start
  condition: []
  action:
  - type: turn_off
    device_id: 03183...[redacted]...10d4fa8d3
    entity_id: 9ca7e9...[redacted]...ce7bd3b8
    domain: light
  mode: single

This was effective in turning the light off when it got ‘stuck’ on.
Note: it does mean any active alert is effectively cancelled but I think that is the safer option.

FWIW- I have an Adafruit VL53L0X Time of Flight Micro-LIDAR Distance Sensor Breakout connected to a Wemos d1 Mini that measures the distance from the garage wall to the bin. If the bin is present the day before it is supposed to be outside, I get a flashing red indicator. This way anyone can move the bin and if it’s gone, the indicator goes off.

I like it, sounds like a project worth pursuing…
The bin alert seems to be a popular project for newbies like me and I am looking forward to my first actual alert tomorrow night.