I am looking for a solution to my automation problem.
I have connected a Sparkfun RFID sensor to my Raspberry Pi 4 and integrated it with the Serial Integration. The read values have non-readable characters before and after the actual key, which I then “clean” with a second script. Thanks to Claude and ChatGPT, I have already come this far. Unfortunately, I am failing to create an entity that compares the cleaned values with the list of allowed values and, if there is a match, jumps to “on” for a second and then switches back to “off”.
I have been searching for the last two days, but have not yet found a solution to my problem. I’m also not an expert in YAML and programming in general, the modifications to the configurations.yaml were made by trial and error with claude.ai, and that’s probably where the problem lies.
Here is my configuration.yaml. The “Template Binary Sensor based on the cleaned tag info” is accepted by Home Assistant without grumbling, but does not switch as I would like it to:
> # Loads default set of integrations. Do not remove.
> default_config:
>
> # Load frontend themes from the themes folder
> frontend:
> themes: !include_dir_merge_named themes
>
> automation: !include automations.yaml
> script: !include scripts.yaml
> scene: !include scenes.yaml
> # Example configuration.yaml entry
>
> # Serial Sensor for RFID Tag Reader
> sensor:
> - platform: serial
> name: "RFID Tag Raw"
> serial_port: /dev/ttyUSB1 # Replace with your actual serial port
> baudrate: 9600
>
> # Clean raw data
> - platform: template
> sensors:
> rfid_tag_cleaned:
> unique_id: rfid_tag_cleaned
> friendly_name: "Cleaned RFID Tag"
> value_template: >
> {% set raw = states('sensor.rfid_tag_raw') %}
> {{ raw | regex_replace('^\\x02', '') | regex_replace('\\x03$', '') | trim }}
>
>
> # Template Binary Sensor based on the cleaned tag info
> binary_sensor:
> - platform: template
> sensors:
> rfid_authorized_tag:
> friendly_name: "Authorized RFID Tag"
> value_template: >
> {% set tag = states('sensor.rfid_tag_cleaned') %}
> {{ tag in [
> '070044E4B81F',
> '05008C8F9C9A',
> '05007E65C0DE',
> '5100C2B4C9EE',
> '5100C2DA8FC6',
> '03003119A883'
> ] }}
> device_class: occupancy
>
> # Automation to turn on a specific binary entity for 10 seconds
>
>
>
> logger:
> default: info
> logs:
> homeassistant.components.template: debug
> homeassistant.components.serial: debug
I would appreciate tips on how I can get closer to my on/off entity, which I can then use in Node-Red to switch my automation on and off.
I dont think a template can have like timeouts and then back to off.
For your template the source template needs to go back to a state that goes to false in the binary sensor. I dont know how your setup works but I think the last scan stays at the template sensor right?
For the delay version my way would be to create an automation which triggers on rfid_tag_cleaned change, then an if-then which checks for the allowed tags and then swtiches on an input booelan, delays and then off
correct, the last scanned rfid-tag value stays where it is, because it’s basically a serial input. so if i would try the solution you suggested it would be possible to make that automation completely in node-red, right? do you have a tip where i could look at a similar automation with the boolean you mentioned?
The problem is that the card reader only sends a change of status via serial when a new chip is read in, so I would like to set the “new chip read” (which is the ID of the RFID-Chip) signal to 0 automatically after a specified time (~1 second) in order to simulate a switch that I can use in further automation.
Components used: RPI4 running HAOS, Zigbee Coordinator stick, RFID card reader (both connected to the RPI), ESP32-c6 which is integrated via Zigbee, with an RGB LED as status display of the automation (will be a kind of alarm system), Zigbee motion detector.
in the environment where the automation is used, neither wifi nor lan are possible, so I have to use wired automation for short distances and zigbee for longer distances. So ESPHome and ESP32 are unfortunately out of the question. Since the whole thing should be as low-budget and stealthy as possible and I already have RPI and the RFID reader, I don’t want to resort to other methods. the whole system should consist of as few parts as possible and must run without a permanent internet connection. This is only established every few weeks for software updates.
I dont use node red but as this is an easy automation, so it shouldn’t be that hard to transport.
For the input_boolean you need to add it as a helper. The friendly name is “Toggle”.
Thank you for the yaml! It’s way more beautiful than my solution was, but unfortunately the problem that the input boolean only switches to “on” once when the first tag is read but not after that or if another tag is read afterwards persists:
it should toggle every time one of the authorized tags is read, even if the same tag is read twice. Are there any suggestions on how I can solve this? best regards and thank you for your help, i really appreciate it!
Thank you for your quick reply, i really appreciate the help you’re providing. unfortunately, the helper stays at “on” after the first read, i’d need it to switch to “off” after a certain time, and to “on” again when a new tag or the same tag as before is scanned
ok, I don’t think I’ve communicated clearly enough how I envisage this, so let’s start again from the beginning: I intend to start/stop an automation loop with the RFID chips. To do this, I need the functionality of a button. However, as the RFID reader only passes on the data of the read chip, I have to simulate this switch via an automation, in this case the input boolean. I am aware that a certain time is not defined, 10s, 1s it doesn’t matter as long as the signal jumps back to “off”. It does this when the first chip is read, but for each subsequent read it stays “off” which is not what should happen… it will probably come down to a combination of the two suggestions to work, but as I know absolutely nothing about programming, I have turned to this forum to get help from some nice people who are obviously much deeper into the subject than I am.
I see. I am also not that deep into the core, but the problem is, that the automation doesn’t recognise the value change as a state change.
I think the problem is that you used
value_template:
and not
state:
so either rewrite the configuration.yaml rfid_tag_cleaned like this:
template:
- sensor:
- name: "Cleaned RFID Tag"
unique_id: rfid_tag_cleaned #not sure if this works as you have defined one already, but if you get an error just leave this one out
state: >
{% set raw = states('sensor.rfid_tag_raw') %}
{{ raw | regex_replace('^\\x02', '') | regex_replace('\\x03$', '') | trim }}
OR just change the value_template to state (also better be safe and change the other ones too).
Then it should work fine. Additionally change the automation mode to mode: queued so if you scan two tags in the given time frame, it triggers two times (if that fits your use case ofc) or any other mode which are explained when you click the three dots on he top right when you edit the automation in UI mode.