Storing calendar ATTRIBUTE to variable and checking if STATE contains a string

I have 2 different automations I am working on that I am looking for guidance. To explain, I am taking a google calendar and using the calendar events to broadcast (tts.google_translate_say) to my speaker devices what the calendar event is. That way I can hear when an event is coming up when I am in my home.

The yaml:

alias: Test
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.home_assistant
condition:
  - condition: state
    entity_id: calendar.home_assistant
    attribute: message
    state: test
action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.home_assistant_group
      message: Test calendar automation from home assistant
      cache: true
mode: queued
max: 50

Situation 1:
I would like to be able to look at entity_id: calendar.home_assistant and have the automation read what the event attribute: message is, and just TTS announce what it is and be done with it. My issue is that I am unsure how to do this. Currently, I have a condition set up that looks for the exact name of the event (in this case, state: test), and if it matches, then it performs an action and does TTS on a custom message I write. My default knowledge tells me I should store the attribute: message to a variable, and have the action message announce this variable. Is that the correct way to do that? How would you then store it to variable? Basically I want to announce every calendar event that calendar.home_assistant has inside it because I will have events for Home, Work, and Games. If I can do this, I won’t have to create 3 automations, and I can just have 1 which covers any events listed on this calendar.

Situation 2:
For calendar events, the title of the event in google calendar is the messages state. So I am wondering how I can look at:

state: testing

and write something to check if the state CONTAINS a string in it. Currently, based on the above, it is only going to match the condition if the state is exactly “testing”. I want to be able to check if the state CONTAINS the string “test”. That way I can capture any event with “test” in it", because I may have events where the title says “tested” or “testing”.

this will read the data contained inthe “message” attribute of the “calendar.home_assistant” entity:

action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.home_assistant_group
      message: "{{ state_attr('calendar.home_assistant', 'message') }}"
      cache: true

this wil find the string “test” in the state of the “calendar.home_assistant” entity:

"{{ 'test' in states('calendar.home_assistant') }}"

however, I’m confused about what “state” you are talkning about.

I think the the state of a calendar entity is either “on” or “off” depending if the calendar event is active at that time or not.

Where exactly are you seeing the “state: test” that you are referring to? is it an attribute? if so I don’t see it in my google calendar entities

Right here

So that is telling it to use the state of the attribute and not the actual state of the calendar (I know it’s bit confusing).

so try this:

"{{ 'test' in state_attr('calendar.home_assistant', 'message') }}"

I’m confused. Is this not matching the title of the calendar event?

image

the title of the calendar event should be the “message” attribute…I think…

therefore it’s the “message” attribute state that you are looking for not the actual state of the entity.

Isn’t that what my screenshot is though? It’s the “State” of the “Message” originating from the “Home Assistant” entity.

yes but it’s the value of the “message” attribute of the entity not the value of the state of the entity itself.

so you need to use “state_attr(…)” and not “states(…)” to extract the value.

maybe I’m missing the question?

Where do I see what you’re looking at?

go to developers tools on the left menu. click on states at the top then filter to the calendar entities (or just scroll to it).

Just adding a direct link:
Open your Home Assistant instance and show your state developer tools.

That link doesn’t work for my HA instance but I don’t use the default config or the supervisor so I don’t know what it relies on to work.

Oh, weird. Are you up-to-date? I think some of the endpoints changed in the last few months. That link should just link to whatever URL you’ve specified + the endpoint for the given page (i.e. http://homeassistant.local:8123/developer-tools/state in my case):

image

In any case, I think it should work for anybody running a standard setup that is fairly up-to-date. Definitely good to know that it doesn’t work for everybody though!

alias: Test
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.home_assistant
condition:
  - condition: state
    entity_id: calendar.home_assistant
    attribute: message
    state: "On"
action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.home_assistant_group
      message: "{{ state_attr('calendar.home_assistant', 'message') }}"
      cache: true
mode: queued
max: 50

So you’re saying this is more correct? Having a condition of the MESSAGE state being ON for the ENTITY so it can progress through the automation to TTS and start the ACTION?

Because right now I see the automation trigger, but the action never starts.

yeah, up to date except for most recent.

the URL was http://192.168.1.11:8123/_my_redirect/developer_states and I got a 404 error.

nope.

alias: Test
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.home_assistant
condition:
  - condition: state
    entity_id: calendar.home_assistant
    attribute: message
    state: "test"
action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.home_assistant_group
      message: "{{ state_attr('calendar.home_assistant', 'message') }}"
      cache: true
mode: queued
max: 50

How can I set the condition state to match anything so that way it will work with any calendar event I make? Right now it’s only looking for calendar events that I name “test”. Is there a way to set it so that it doesn’t matter what the state is set to? aka: it will do any calendar event i make.

Basically just this here. Is there a wildcard I can set for the condition state? Unless I don’t need to specify a condition? But I did try that and didn’t see any TTS happening.

alias: Test
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.home_assistant
condition:
  - condition: state
    entity_id: calendar.home_assistant
    attribute: message
    state: _ANY EVENT HERE_
action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.home_assistant_group
      message: "{{ state_attr('calendar.home_assistant', 'message') }}"
      cache: true
mode: que

You don’t need a condition. You can check if the automation is being triggered in the automations page:

If it is being triggered but you still aren’t hearing the TTS, you can check the trace by clicking on the relevant automation and then clicking “Traces” in the top right. You should be able to check each step for errors from there.

It may be helpful to simplify the automation as well while debugging it. You could try updating a helper with the current time instead of of calling the TTS service, or just making the TTS service call message something like “test”. This just helps debug pieces of it, so you don’t get focused on the trigger when the issue is actually with the service call or something like that.

Also, not sure if just a copy-paste error, but your mode is invalid.

Yes that is a copy paste error. I will try what you suggested and report back.