Email content automation not working

I’m trying to trigger an automation which turns on a switch when an email arrives with a specific text in the subject.
I’ve defined a sensor with a value_template in my config which seems to work fine:

 sensor:
   - platform: imap_email_content
     server: imap.gmail.com
     name: parking_emails
     port: 993
     username: [email protected]
     password: mypassword
     senders:
       - [email protected]
       - [email protected]
     value_template: >-
       {% if 'You have a new booking' in subject %}
         parking_booking
       {% elif 'you have a new JustPark booking' in subject %}
         parking_booking
       {% endif %}

When an email arrives with either of the above texts in the subject I can see the value of parking_emails change from “unknown” to “parking_booking” in the HA console.

So I have automation defined as follows:

- alias: Parking booking email arrives
  trigger:
    platform: state
    entity_id: sensor.parking_emails
    from: unknown
    to: parking_booking
  action:
  - service: switch.turn_on
    entity_id: switch.parking_message

But the automation trigger never gets fired - the switch never gets turned on.

Any idea what the problem is?

Could it be that the from state is not unknown. You do not have that text as part of your template.

In the action you do not reset the state to unknown.

OK I’ve revised the automation to remove the “from:” element. So now it’s:

- alias: Parking booking email arrives
  trigger:
    platform: state
    entity_id: sensor.parking_emails
    to: parking_booking
  action:
  - service: switch.turn_on
    entity_id: switch.parking_message

It’s still not getting triggered though.
???

should this

{% elif 'you have a new JustPark booking' in subject %}
be this
{% elif 'You have a new JustPark booking' in subject %} 

Yes that part is correct - it’s a lower case “y”. That part of the logic is working fine - the sensor is being set to “parking_booking” - see here for the log which confirms this:
Imgur

I have nothing more to contribute. Sorry I couldn’t help.

Did you reload automations/restart after the changes? Can you confirm that the automation has is turned on under Developer Tools → States?

I restart the server between changes.
I just noticed that the automation is not listed at all under Developer Tools → States. So I restructured the syntax of the automation syntax to this:

- alias: Parking booking email arrives
  trigger:
  - platform: state
    entity_id: sensor.parking_emails
  action:
  - service: switch.turn_on
    entity_id: switch.parking_message

…and now it’s working. I must confess I really struggle with yaml syntax, and really don’t understand what was wrong with the original syntax.
In any case, many thanks for leading me down the right path!