Proof read this template?

I understand simple templates, but complex templates are over my head. While back I attempted, but never got it working. Tonight I used chatGPT to help me, was wondering if the template below is correct. If I’m wasting my time with gpt, please lmk. Have a better idea/way, please share. Many thanks!

Request to GPT:
Home assistant needs to use imap for my email account, and monitor for an email from [email protected] or from [email protected] Then search for a date in the body of the email that comes immediately after the text “xxxxx-8603” But the thing is, the date is preceded by the day. i.e. Friday, April 21.
When an email arrives from either sender, with “energy savings day” in the subject line, (search those emails for the date) then add that found date to a sensor.

Template by GPT:

sensor:
  - platform: template
    sensors:
      email_date:
        friendly_name: "Email Date"
        value_template: >-
          {% set keyword = 'xxxxx-8603' %}
          {% set email = states('sensor.imap_email_content') %}
          {% set date_pattern = '(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), ([A-Z][a-z]+ [0-9]{1,2})' %}
          {% set date_match = email|regex_search(date_pattern) %}
          {% if date_match %}
            {{ date_match.group(0) }}
          {% else %}
            No date found in email.
          {% endif %}

Automation by GPT:

automation:
  - alias: "Update Email Content Sensor"
    trigger:
      platform: state
      entity_id: sensor.imap_email_content
    condition:
      - condition: template
        value_template: >-
          {{ '[email protected]' in states('sensor.imap_email_sender') or '[email protected]' in states('sensor.imap_email_sender') }}
      - condition: template
        value_template: >-
          {% set keyword = 'xxxxx-8603' %}
          {{ keyword in states('sensor.imap_email_content') }}
    action:
      - service: homeassistant.update_entity
        entity_id: sensor.email_date

You clearly missed the pinned message and announcement… ChatGPT is confidently wrong when it comes to HA, in part because it’s designed that way, and in part because it was trained on utter shite.

4 Likes

Thank you for the link regarding GPT. Clearly I did miss the message-
I’ve had success (albeit somewhat limited) with code for other systems so I thought I’d give it a go.

Perhaps one day I’ll be a HA guru, until then guess I’ll have to leave portions of HA to those with the knowledge.
Thanks for the read!