#Automation for Code Cracker Game

1 messages · Page 1 of 1 (latest)

dreamy bloom
#

So I created a bunch of input_select Options for two cards in my Home assistant dashboards. One inputs a set of five colors, the other (on a different dashboard) sets the correct answer. The automation is supposed to take the input-guess, compare it to the correct answer (when input_button.submit_guess is pressed) and give a result in 'number of correct colors' as well as 'number of correct positions' and increase the counter of guesses by +1. All entities for the input helpers needed are created correctly. I can put a correct code in a card in the Game-Master card and I can put a guess of five colors in the Game-Card. The counter does however not increase and I do not get anything from the card showing correct colors and correct positions. I have the feeling the automation isn't triggered when the input_button is pressed but I don't get any los to check. I have no idea how to code and had Chat-GPT wrie the code for me (i know, i know...). So my best guess is that the code is just gibberish...but since I have no idea how it is supposed to look like, I can't troubleshoot. I'll post the code for the automation in the next post.

slow wing
#

it doesn't understand that you need namespace

#
alias: Code-Cracker
description: Compares guess to correct input triggert by input_button.press
triggers:
  - event_type: input_button_pressed
    event_data:
      entity_id: input_button.submit_guess
    trigger: event
variables:
  info: >
    {% set ns = namespace(codes=[], guesses=[], count=0) %}
    {% for i in range(1, 6) %}
      {% set code = states('input_select.code_slot_' ~ i) %}
      {% set ns.codes = ns.codes + [ code ] %}
      {% set guess = states('input_select.guess_slot_' ~ i) %}
      {% set ns.guesses = ns.guesses + [ guess ] %}
      {% set ns.count = ns.count + 1 if code == guess else 0 %}
    {% endfor %}
    {{ dict(codes=ns.codes, guesses=ns.guesses, correct_place=ns.count, correct_color=ns.guesses | intersect(ns.codes) | length) }}
actions:
  - target:
      entity_id: input_number.correct_place
    data:
      value: "{{ info.correct_place }}"
    action: input_number.set_value
  - target:
      entity_id: input_number.correct_color
    data:
      value: "{{ info.correct_color }}"
    action: input_number.set_value
  - target:
      entity_id: input_number.attempt_number
    data:
      value: "{{ (states('input_number.attempt_number')|int) + 1 }}"
    action: input_number.set_value
  - data:
      message: >
        CODE-KNACKER ▶ code={{ info.codes }}, guess={{ info.guesses }}, cp={{ info.correct_place
        }}, cc={{ info.correct_color }}
      level: debug
    action: system_log.write
mode: restart