Automation - syntax correct, loaded OK, triggers, does not do what I want!

Hello.

can anyone shed some light on why an automation is not working.
Here is the code:

- alias: Set AM Start Time based on Delphine's Work Time
  trigger:
    platform: time
    at: "00:00:01"
  condition:
    condition: template
    value_template: "{{ state_attr('calendar.delphine_work_calendar', 'start_time') [0:10] == states.sensor.date.state }}"
  action:
    service: input_select.select_option
    data_template:
      entity_id: input_select.stoveamstart
      option: >-
        {% if (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '16:30:00' %}
          '07:00'
        {% elif (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '10:00:00' %}
          '07:00'
        {% elif (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '13:30:00' %}
          '07:00'
        {% elif (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '06:45:00' %}
          '04:30'
        {% elif (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '08:00:00' %}
          '06:00'
        {% elif (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '08:45:00' %}
          '06:30'
        {% elif (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '09:30:00' %}
          "07:00"
        {% endif %}

I have tested if conditions in the data template. All of the set values are valid options for input_select.stoveamstart.

The current attribute ā€˜start_timeā€™ of calendar.delphine_work_calendar is ā€˜2019-11-19 09:30:00ā€™ - which when used with the [11:19] evaluates to 09:30:00.

{{ (state_attr(ā€˜calendar.delphine_work_calendarā€™, ā€˜start_timeā€™) [11:19]) == ā€˜09:30:00ā€™ }} shows true in the templates dev tool. All of the other conditions evaluate to false currently.

However I try, it does not do what I want.
Call the service input_select.select_option with data entity_id: entity_id: input_select.stoveamstart and time: 07:00.

As you may notice, I have tried changing the ā€˜07:00ā€™ to ā€œ07:00ā€ in the automation - changes nothing.

I really cannot see why itā€™s not working - Anyone like to enlighten me?

Thanks!

Are there any error messages in the log related to the execution of this automation?

If you execute this automation manually, it will skip the trigger and condition and execute the action. If you try it and it works, then it means there may be a problem with the automationā€™s condition.

Try removing the quotes from the values. So:

- alias: Set AM Start Time based on Delphine's Work Time
  trigger:
    platform: time
    at: "00:00:01"
  condition:
    condition: template
    value_template: "{{ state_attr('calendar.delphine_work_calendar', 'start_time') [0:10] == states.sensor.date.state }}"
  action:
    service: input_select.select_option
    data_template:
      entity_id: input_select.stoveamstart
      option: >-
        {% if (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '16:30:00' %}
          07:00
        {% elif (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '10:00:00' %}
          07:00
        {% elif (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '13:30:00' %}
          07:00
        {% elif (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '06:45:00' %}
          04:30
        {% elif (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '08:00:00' %}
          06:00
        {% elif (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '08:45:00' %}
          06:30
        {% elif (state_attr('calendar.delphine_work_calendar', 'start_time') [11:19]) == '09:30:00' %}
          07:00
        {% endif %}

Thereā€™s no else in this list

Thatā€™s a good point, too. :slight_smile:

Thanks everyone. Removing the quotes worked.
OF course, 123 Taras suggestion to look in the logs showed that ā€˜07:00ā€™ was not a valid option.

I had tried triggering manually, without errors.

I also had an else in my data template, but it appears to work fine without it. Is that perhaps because it is the last bit of code in my automations yaml file? I thought I had read that with elif you can end with simply endif.

Adding the else back - but it worked without it.

Thanks to you all for your suggestions.

Your - if then elif elif elif will work fine UNTIL it doesnā€™t match any preceeding statement, then it will crash (the automation) big style.
Besides itā€™s just bad programming practice and you should try to cater for a case you didnā€™t specifically program for

Thank you. clear and useful help! Iā€™ve added the else now.

Okay, just as long as you didnā€™t leave the else statement blank (that will also cause errors)
If you are certain you ā€˜haveā€™ covered all possibilities, then there is no harm in just converting your last elif to an else and removing the evaluation
Good luck

As you rightly said, itā€™s bad programming practice. The calendar entries that are used are manually entered. There could always be a type in the entry, with the start time not being one of the possibilities, then I would run into trouble. My else covers that - to change nothing if one of the possibilities is not met.

1 Like

To help improve legibility you can use this streamlined version:

  action:
    service: input_select.select_option
    data_template:
      entity_id: input_select.stoveamstart
      option: >-
        {% set t = state_attr('calendar.delphine_work_calendar', 'start_time')[11:19] %}
        {% if t == '16:30:00' or
              t == '10:00:00' or
              t == '13:30:00'   %} 07:00
        {% elif t == '06:45:00' %} 04:30
        {% elif t == '08:00:00' %} 06:00
        {% elif t == '08:45:00' %} 06:30
        {% elif t == '09:30:00' %} 07:00
        {% else %} unknown
        {% endif %}
1 Like

Gotta love variables!

1 Like

thatā€™s kind of overstating things a bitā€¦

Without the else statement and if all of the other ā€œif/elifā€ statements fail then the worst that will hapen is the automation will write an error into the log saying something about invalid data for the service call. And of course there will be no action taken since the automation failed.

But to say ā€œcrash big styleā€ sounds way more ominous. :grimacing:

:wink:

But I also agree that you probably should always try to include an ā€œelseā€ for no other reason than itā€™s good programming practice. Oh, and the fact that 6 months from now youā€™ll be looking through the log and see an error and you wonā€™t have any idea where itā€™s coming from and itā€™ll drive you batty trying to figure out whatā€™s causing it. Or at least it would meā€¦:slightly_smiling_face:

Re reading, it is more than a bit ambiguous (I have edited the post to make it clearer).
But ā€˜generallyā€™ people posting questions, seem (not suggesting neal did here) to only tell the part of an automation they are having difficulty with.
The fact that they later add 4 actions to it (and it works) is okay with them. But when there is no ā€˜elseā€™ and the template hits it, then no further actions will be carried out. Iā€™d call that, crashing (the automation) big style, even if it crashes nothing else (it wouldnā€™t)
I am, anyway, always cognisent that the ā€˜nextā€™ reader will just learn the lessons contained within an ā€˜apparentā€™ solution, without thought to the wider implications - especially if it works the two or three times they test it.
Thanks for the opportunity to clarify

Itā€™s a shame that itā€™s not possible to mark two answers as a solution - AFAIK.

This forum is a hive of very useful information and advice - and I thank everyone that contributes. While researching the use of ifā€¦elifā€¦esleā€¦endif - I came across some documentation with an example that did not contain the else. And copied that example, unaware that it could break my automation if none of the if or elif conditions were met.

In order to try to clarify a bit:

My automation was not taking the expected actions due tome putting quote marks around the input_select options. By doing so, my automation started performing as expected.

I had also researched using variables in automations - How long has it been possible to use set t = in a template? I was not aware and did not find the information searching.

So, I came here looking to understand why, after inspecting my code and testing parts of it, the automation did not work. I got the answer to that, and a LOT of VERY useful advice that will enable me to be more efficient in creating automations in the future. I could not have hoped for more.

Mutt - Iā€™m not sure I understand what you find more than a bit ambiguous - I would be happy to disambiguate if I knew - and yes, the next reader will take what he wants rather than what any of us would like them to. You can lead a horse to waterā€¦ but you cannot force it to drink!

Very true my friend :smiley: but with some members entrenchment I do sometimes wish that ā€˜shooting itā€™ was an available option :crazy_face:
Ambiguity - finity and I often butt heads on terminology, and more often than not we both learn something from it. (Iā€™d rather fight with finity than have someone (wrongly) agreeing with me).
Finityā€™s remark was that I ā€˜impliedā€™ the fault would crash HA or some such rather than just NOT continuing the automation. I agreed and have changed my post.
Every post I write, I hope to be of value to at least the next 5 readers that come across it (until HA moves the goalposts, and we have to start again :rofl:)
AND if you learnt more than you intended to (and were grateful for that), (sorry, but I hate the ā€œonly tell me what to put in the templateā€ attitude, cos they will learn nothing) then thatā€™s a happy consequence.
Iā€™m sure both finity and I will be pleased to assist you in future, a few other members - I tend to avoid, they ā€˜takeā€™ to solve this but never learn/expand to solve the next themselves - blackholes of need.
Besides Iā€™m sure you will help out in line with your abilities (which expand with exposure :smiley:)

Edit: 2 solutions, no I think in all fairness Phil provided the solution, at best the other members here ā€˜refinedā€™ it (I generally learn more from the refinements, but without the solution they are pointless :sob: )

1 Like

I started using Home Assistant over a year ago so I can confidently say it has supported variables within a template for ā€˜at least a yearā€™. However, it has probably had that capability since it adopted the Jinja2 templating engine so the real answer is ā€˜for many yearsā€™.

Thanks again for your replies and insight.
This forum is one of the most useful and helpful that I personally use on the net.

XDA developers used to be like this back in the day, but like so many forums, there are soooo many trolls, rude, entitled people and general a**h*les about that itā€™s no longer a source of help / info for me.

Here, I learn every day from helpful and friendly people that just wanna share the good thing they got.

I try to provide some help to others here, but there are so many helping, I nearly always get beaten to it!

We save (most of) the sarcasm for the Discord server :wink:

Maybe, but if your thinking about it you are working the problem, you get quicker and finity has to sleep sometimeā€¦

Thatā€™s my native language :crazy_face: