Help With Kids Bedtime Automation

Hey all,

I have an automation that I use to let my kids know when it’s bedtime. I have it set up right now like what I have below and it works great but with 1 pretty big issue. All the logic appears to work as expected, however if for example the nap switch is on, my daughters bedtime is supposed to be 8:00 so Alexa will tell her it’s bedtime at 8:00. The issue is that at 7:30, the logic hits the { else } " " part where she should say nothing, but she ends up saying something like “If you want me to say something say simon says followed by what you want me to say.” It’s like shes doesn’t like getting a blank tts command.

For this I have 2 questions.

  1. How could I rework this automation to do what I want without the blank tts part?
  2. How would I make this work if I wanted to add a second child to the automation?

I could make each child have multiple automations, one at 7:30, one at 8:00 with the condition of if the switch is off or on, but I don’t want to have to end up with 2 different automations for each kid, we have 3 and that seems like too many automations for something that I am hoping to get into a single one, 3 at the most. If it’s not possible, please let me know.

My current automation is below:

- id: '1568077354633'
  alias: Kid 1's Bedtime
  trigger:
  - at: '19:30'
    platform: time
  - at: '20:00'
    platform: time
  condition: []
  action:
  - data_template:
      data:
        type: tts
      message: '{% if is_state("switch.kid1nap", "on") and (now().hour == 20
        ) %} It''s 8 0 0, time for bed, Kid1 {% elif is_state("switch.kid1nap",
        "off") and (now().hour == 19 ) %} Kid1 you didn''t nap today, your bedtime
        is 7 3 0 tonight {% else %} " " {% endif %}'
      target:
      - Dad's Echo Spot
      - Mom's Echo Show
      - Office Ecobee4
      - Living Room Echo
      - Playroom Echo Dot
      - Kids Bathroom Echo Dot
      title: Kid1  Bed Time
    service: notify.alexa_media
  - delay: 01:00:00
  - alias: ''
    data:
      entity_id: switch.kid1nap
    service: switch.turn_on

Just remove the {% else %} " " section.

You don’t have to define an else in an if statement.

I know the answer but I have a question.

How can this test ever be true?

is_state("switch.kid1nap", "off") and (now().hour == 19 )

The automation triggers at 19:30 and 20:00 so how can now().hour == 19 ever be true?

Thanks for the reply, I had tried that but I guess it still implies it because Alexa still gives the same simon says message.

That particular template will only show the hour portion of the current time. You can test it in your template editor with {{ now().hour }}

Yes, I had to change it to that because when I had the full time in there it wasn’t triggering right. Now the triggering works as I want it to, I just don’t want Alexa talking when the simon says message is blank. I’m trying to see if there’s a better way to structure my automation to avoid that.

Gotcha.

OK, the answer is that once the automation reaches the action portion, you are committed to supply a valid service and valid options for that service … so you have to prevent it from reaching the action. You do that with the condition.

In other words, the condition should prevent the automation from reaching the action when there is nothing to say. That means the tests you are currently performing within the action will need to be duplicated in the condition. So when the first test is false AND the second test is false, the condition evaluates to false and the action is not executed.

So I basically just need to use the same checks I have in my if/then statement as a condition? I was thinking that, but was worried that would be messy. Thinking about it now, I may be able to simplify the actual action if I write the condition correctly. Sorry for thinking out loud, or whatever you would call it on a keyboard lol but I think you may have just triggered something in my brain that helped me figure this out. I’ll try it and report back. Thank you!

I’ve seen this technique used for automations where the action’s service won’t accept a do-nothing command (like what you’re using). Faced with this situation, the strategy is to simply use the condition to fail gracefully and avoid executing the action portion.

There are services that can quietly accept a do-nothing command. For example, let’s say I had some sort of complex template to determine which light to turn on (using light.turn_on service). However, the ‘else’ needed to be ‘no light at all’ then I can specify a non-existent entity (entity_id: light.zilch) because Home Assistant won’t complain about it and the light.turn_on service will simply fail gracefully.

As you’ve discovered, the service you are using is not one of those that will fail gracefully if supplied with nothing (or even an empty string). So we just try to avoid calling the service when there’s nothing to say.