Timer Automation with dynamic timer entity

I’ve got some NFC cards that I want to use to start specific timers.

The cards have the attribute timer like so:

type: timer
time: 1200
for: finn
last_used: 
timer: timer.finn_gaming_timer
device_class: None
icon: mdi:nintendo-switch
friendly_name: Finn Timer NFC Card

However I keep getting the error in the logs:

Automation with alias 'Handle Tag Scan' could not be validated and has been disabled: not a valid value for dictionary value @ data['action'][1]['choose'][1]['sequence'][2]['entity_id']. Got '{{ timer }}'

The automation is:

action:
    - variables:
        tag: "{{ trigger.event.data.tag_id }}"
        scannedCard: "binary_sensor.nfc_card_{{ tag | regex_replace(find='-', replace='_') | lower }}"
        for: "{{ state_attr(scannedCard, 'for')|string }}"
        timer: "{{ state_attr(scannedCard, 'timer') }}"
        media_player_entity_id: "{{ media_players[trigger.event.data.device_id] }}"
        media_account: "{% if media_players[trigger.event.data.device_id] == 'media_player.kits_bedroom_speaker' -%} kit {%- else -%} finn {%- endif %}"
    - choose:
      - conditions:
          - condition: template
            value_template: "{{ state_attr(scannedCard, 'type')|string == 'music' }}"
        sequence:
          - choose:
            - conditions:
                - condition: and
                  conditions:
                    - condition: template
                      value_template: "{{ states(media_player_entity_id)|string != 'off' }}"
                    - condition: template
                      value_template: "{{ states(media_player_entity_id)|string != 'idle' }}"
              sequence:
                - service: media_player.media_stop
                  data:
                    entity_id: "{{ media_player_entity_id }}"
          - service: spotcast.start     
            data:
              account: "{{ media_account }}"
              entity_id: "{{ media_player_entity_id }}"
              uri: "{{ state_attr(scannedCard, 'media_content_id')|string }}"
              random_song: true
              shuffle: true
              force_playback: true
      - conditions:
          - condition: template
            value_template: "{{ state_attr(scannedCard, 'type')|string == 'timer' }}"
        sequence:
          - service: notify.mobile_app_sm_n985f
            data:
              message: "{{ media_player_entity_id }} {{ timer }} {{ state_attr(scannedCard, 'time') }}" 
              title: TEST
          - service: script.turn_on
            target:
              entity_id: script.tts_announcement
            data:
              media_player: "{{ media_player_entity_id }}"
              message: "Timer for Finn gaming starts now."
              volume: 0.4
            continue_on_error: true
          - service: timer.start
            entity_id: "{{ timer }}"
            data:
              duration: "{{ state_attr(scannedCard, 'time') }}"
      default:
        - service: script.turn_on
          target:
            entity_id: script.tts_announcement
          data:
            media_player: "{{ media_player_entity_id }}"
            message: "Sorry, that card is invalid. Please try again."
            volume: 0.4
    - delay: 2 # timeout before we allow processing next scan

I really can’t figure out what it is complaining about.
It is saying it got {{timer }} and that is correct, as that is a variable set at the top like the others.

Pulling my hair on this, so help would be greatly appreciated.

templates must go under one of service:, data: or target: So replace this:

          - service: timer.start
            entity_id: "{{ timer }}"
            data:
              duration: "{{ state_attr(scannedCard, 'time') }}"

With:

          - service: timer.start
            target:
              entity_id: "{{ timer }}"
            data:
              duration: "{{ state_attr(scannedCard, 'time') }}"

Or:

          - service: timer.start
            data:
              entity_id: "{{ timer }}"
              duration: "{{ state_attr(scannedCard, 'time') }}"

The first one is the preferred format, the second one is how it used to be done and is still supported for backward compatibility. Though some issues were reported with it (ages ago) and it should not be used if you don’t have to.

1 Like

The first one is what I did, and it just errors:

- service: timer.start
  entity_id: "{{ timer }}"
  data:
    duration: "{{ state_attr(scannedCard, 'time') }}"
Automation with alias 'Handle Tag Scan' could not be validated and has been disabled: not a valid value for dictionary value @ data['action'][1]['choose'][1]['sequence'][0]['choose'][1]['sequence'][1]['entity_id']. Got '{{ timer }}'

The first replacement. i.e. second code snippet in my post.

It seems you did not read what I wrote about:

Here, I’ll make it simple as I can, use this:

          - service: timer.start
            target:
              entity_id: "{{ timer }}"
            data:
              duration: "{{ state_attr(scannedCard, 'time') }}"

Got it working using the following:

- service: timer.start
  data:
    entity_id: >-
      {{ state_attr(scannedCard, 'timer') }}
    duration: >-
      {{ state_attr(scannedCard, 'time') }}

So you used the second, not recommend, replacement?

:unamused:

Your solution also worked :slight_smile:

Thanks for the assistance.

There is a HACS integration called MeasureIt which allows you to measure arbitrary stuff such as your use of the Nintendo. Might save you the need to use NFC to track that.

Thanks very much for the suggestion, does look neat.

Unfortunately there is no integration for the switch, though I’ve just found a parental control one I’m going to try and use.
The primary idea being though is want the kids to have X amount of time, then can use the cards when the are provided to “unlock” the switch or tablet and allow them to play.
Certain times, or as a reward, may provide additional time.

Later will expand for chore rewards.