Saving event data to file?

Hello everyone.
I’m trying to make a sort of attendance system based on NFC tags. The idea would be to give each card a name and use that as its user’s ID. And because tags don’t have entities, I was considering writing the tag_scanned event to a file and process its data afterwards.
However, I can’t seem to find a way to do that. I’ve considered templates but I don’t know what to call for this, and even if there is something, it’s not a persistent state so I’m not sure it would even be possible.
I’ve also considered fetching the data from logs in the HA’s database but as far as I can tell they don’t contain the “data” part of the event, only the context.

Any help at all on this would be greatly appreciated.

I’m not exacxtly sure what your needs are but you might be able to do what you need with the “File” integration.

you can create a file to write data to and then read the data from the file with the sensor.

I create a file to write some propane data to b ut I don’t use that data anywhere except to read myself so I can’t help with that part.

but here is my code for it:

notify:
  - platform: file
    name: Propane Data Record
    filename: propane_data.txt
    timestamp: true
automation:
  - alias: Smoker Propane Data Record
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: sensor.dark_sky_temperature
    action:
      - service: notify.propane_data_record
        data:
          message: >
            temp: {{ states('sensor.dark_sky_temperature' ) }}, 
            grill raw: {{ states('sensor.grill_propane_raw_weight') }}, 
            grill deviation: {{ states('sensor.grill_propane_deviation') }}, 
            grill deviation percent:  {{ states('sensor.grill_propane_percent_deviation') }}, 
            smoker raw: {{ states('sensor.smoker_propane_raw_weight') }},
            smoker deviation: {{ states('sensor.smoker_propane_deviation') }},
            smoker deviation percent:  {{ states('sensor.smoker_propane_percent_deviation') }}

here is the integration docs:

I’m already using the File integration, my issue now is getting the right data written on it. Where you have sensors for various things, I’m trying to find an equivalent for tags. I’d need it to record the tag ID and time of scan (timestamp: true takes care of this).

I’ll try to explain the logic I need better:
trigger: event tag_scanned is detected with device_id=XXX
action: write data from the event that served as trigger to a file, in this case I only need tag_id

I don’t use tags but I would assume there is something generated as an event to trigger an automation so you should be able to extract the trigger event data and write that to the file in the actions.

At least I would think so.

Found it! Turns out, you can indeed pull conditions and such from an event that served as trigger for the automation.

Here be code:

alias: "------------------ ATTENDANCE TEST ----------------------"
description: ""
trigger:
  - platform: event
    event_type: tag_scanned
    event_data:
      device_id: c6afcf7b5ea44864
condition:
  - "{{ trigger.event.data.tag_id in tags }}"
action:
  - variables:
      user_scanned: "{{ tags[trigger.event.data.tag_id].user }}"
      clearance: "{{ tags[trigger.event.data.tag_id].clearance }}"
  - service: notify.attendance_logger
    data:
      message: "{{ user_scanned }}"
  - service: notify.mobile_app_my_oppo
    data:
      message: "{{ user_scanned }}"
variables:
  tagreader:
    5e2f24ddc83d042e65d90e1e9ea29e5b: tagreader_outside
    c6afcf7b5ea44864: OPPO
  tags:
    testtag1:
      user: test_tag_1_user
      clearance: 0
    testtag2:
      user: test_tag_2_user
      clearance: 1
    BLUETAG1:
      user: blue_tag_1_user
      clearance: 0
mode: single

Creating a group/array of variables with attributes and then searching it for the attribute data we want works perfectly as a condition.

attendance_logger is a yaml file I created to store these logs. With “timestamp=true” it has everything I need it to have (for now, at least).

The “tagreader” variable group is just so I can swap between those two devices and can be ignored.

Next I gotta figure out how to group the “tags” variables to use in different automations without editing each one when that group changes. Thank you for the help @finity

2 Likes

У меня похожая задача. Могли бы вы объяснить как это настроить?