{% if ... elif ... else %} with no else - what do you do?

Not quite. There’s a gap between 41 and 70 that should not result in the (else) lights_75 case. I just closed it by bringing that first test down to >40.

Condition the gap, so it doesn’t run the action if it is within the gap :wink:

@anon43302295, that is another good solution using a condition but I am interested in if there is a solution when it is not the last action in an automation.

Let’s say for some reason I then wanted to to check if was St. Georges day(!), or something else more useful :slight_smile: and not even date related, or even something that didn’t even need a condition to be met. (A real world example could be a delay until the birthday greeting TTS was finished.)

That’s why I asked for the example code.

Like I don’t know what your birthday greeting script does, or what your St George’s day script would do, but if this is all part of an announcement over tts for example, I would run a ‘say’ script and put the logic for what needs saying and when in that.

Like my announcements script uses the status of a random binary sensor to decide whether or not to add an ‘interesting fact’ to the end of each message, you could easily use the same principle to work out if it’s somebody’s birthday (or saints day!) and just have it built in to the message.

Your TTS script was one of the first really cool things I saw when I joined this forum and I did indeed nick most of it to good effect :slight_smile:

I became slightly disenchanted with the whole TTS in HA thing though due to it not being able to queue messages so getting message clash and also announcements always being in a predefined order does get a bit samey.

This prompted me to come up with a different method which currently might just about be as good as yours (at best) but I think will be more flexible if ever there is a way to queue messages and if I can put the work in to get around the 256 character input text limit. MQTT seems the best option at the moment for that but there is always something more interesting to do with HA than rework existing working features!

Heh, there are ways to queue the messages, just depends how much effort you want to put in. You could use a counter for example, and increment the counter with each message and get the number ‘in the queue’ to wait for the message to be spoken at the start of the script, and use a wait template.

Randomising the order of the sections of speech shouldn’t be too difficult either, the real ball-ache being how much time it takes to refine these things and try to cover every eventuality :slight_smile:

1 Like

please let me join this here, with a related question. I think I got it working, but I am not using the fail safe dummy script yet, and not sure if I even need to.
consider this:

service_template: >
  script.{% if is_state('input_boolean.notify_announcement', 'on') %}announce_presence
         {% elif is_state('input_boolean.notify_presence', 'on') %}notify_presence
         {% endif %}

If the announcement is on, it takes precedence over notify even if that is on. If announcement is off, evaluate if notify is on and if yes, notify.
If none are on, do nothing.
Would I need the ‘else’ here?

action:
  - service_template: >
      script.{% if is_state('input_boolean.notify_announcement', 'on') %}announce_presence
             {% elif is_state('input_boolean.notify_presence', 'on') %}notify_presence
             {% else %}dummy
             {% endif %}

or would this be better:

action:
  - condition: template
    value_template: >
      {{ is_state('input_boolean.notify_announcement', 'on') or 
         is_state('input_boolean.notify_presence', 'on') }}
  - service_template: >
      script.{% if is_state('input_boolean.notify_announcement', 'on') %}announce_presence
             {% elif is_state('input_boolean.notify_presence', 'on') %}notify_presence
             #{% else %}dummy
             {% endif %}

bottom one would be better, but you wouldn’t even need the elif. Because your or statement in the value_template would filter it down to those 2 options. Just keep in mind, with your current logic, if both are on, only 1 will fire (announce_presence).

1 Like

you mean like this:

action:
  - condition: template
    value_template: >
      {{ is_state('input_boolean.notify_announcement', 'on') or 
         is_state('input_boolean.notify_presence', 'on') }}
  - service_template: >
      script.{% if is_state('input_boolean.notify_announcement', 'on') %}announce_presence
             {% else %}notify_presence
             {% endif %}

clean!

Yep, it’s always going to be one or the other.

thx again, like it asasap :wink:

hello,i have this 5 automations,someone could tell me how simplify them in 1 or a sensor or something,thanks

- id: 'Tiempo Riego 15C'
  alias: Tiempo Riego 15C
  initial_state: true
  trigger:
  - platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.riego_tardes.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  condition:
  - condition: template
    value_template: "{{ states.sensor.temperatura_exterior.state | int <= 20 }}"
  action:
  - data:
      entity_id: input_number.riego_zona_2
      value: 15
    service: input_number.set_value   
  - data:
      entity_id: input_number.riego_zona_1
      value: 15
    service: input_number.set_value 
  - data:
      entity_id: input_number.riego_zona_3
      value: 15
    service: input_number.set_value 
- id: 'Tiempo Riego 20C'
  alias: Tiempo Riego 20C
  initial_state: true  
  trigger:
  - platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.riego_tardes.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  condition:
  - condition: template
    value_template: "{{ states.sensor.temperatura_exterior.state | int > 20 }}"    
  action:
  - data:
      entity_id: input_number.riego_zona_2
      value: 25
    service: input_number.set_value   
  - data:
      entity_id: input_number.riego_zona_1
      value: 25
    service: input_number.set_value 
  - data:
      entity_id: input_number.riego_zona_3
      value: 25
    service: input_number.set_value 
- id: 'Tiempo Riego 25C'
  alias: Tiempo Riego 25C
  initial_state: true  
  trigger:
  - platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.riego_tardes.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  condition:
  - condition: template
    value_template: "{{ states.sensor.temperatura_exterior.state | int >= 25 }}"    
  action:
  - data:
      entity_id: input_number.riego_zona_2
      value: 30
    service: input_number.set_value   
  - data:
      entity_id: input_number.riego_zona_1
      value: 30
    service: input_number.set_value 
  - data:
      entity_id: input_number.riego_zona_3
      value: 30
    service: input_number.set_value 
- id: 'Tiempo Riego 30C'
  alias: Tiempo Riego 30C
  initial_state: true  
  trigger:
  - platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.riego_tardes.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  condition:
  - condition: template
    value_template: "{{ states.sensor.temperatura_exterior.state | int >= 30 }}"  
  action:
  - data:
      entity_id: input_number.riego_zona_2
      value: 35
    service: input_number.set_value   
  - data:
      entity_id: input_number.riego_zona_1
      value: 35
    service: input_number.set_value 
  - data:
      entity_id: input_number.riego_zona_3
      value: 35
    service: input_number.set_value     
- id: 'Tiempo Riego 35C'
  alias: Tiempo Riego 35C
  initial_state: true  
  trigger:
  - platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.riego_tardes.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"    
  condition:
  - condition: template
    value_template: "{{ states.sensor.temperatura_exterior.state | int >= 35 }}"  
  action:
  - data:
      entity_id: input_number.riego_zona_2
      value: 40
    service: input_number.set_value   
  - data:
      entity_id: input_number.riego_zona_1
      value: 40
    service: input_number.set_value 
  - data:
      entity_id: input_number.riego_zona_3
      value: 40
    service: input_number.set_value

i tried this one,but only executed the first 2,some idea,thanks

- id: 'prueba'
  alias: prueba
  initial_state: true
  trigger:
  - platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.riego_tardes.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  condition: []
  action:    
  - data: {}
    entity_id: input_number.riego_zona_2
    data_template:
      value: >
        {% if states.sensor.temperatura_exterior.state | int <= 20 %} 15
        {% elif states.sensor.temperatura_exterior.state | int > 20 %} 25
        {% elif states.sensor.temperatura_exterior.state | int >= 25 %} 30
        {% elif states.sensor.temperatura_exterior.state | int >= 30 %} 35
        {% elif states.sensor.temperatura_exterior.state | int >= 35 %} 40
        {% endif %}
    service: input_number.set_value  

try this:

value: >
  {% if states.sensor.temperatura_exterior.state | int <= 20 %} 15
  {% elif 20 < states.sensor.temperatura_exterior.state | int < 25 %} 25
  {% elif 25 <= states.sensor.temperatura_exterior.state | int < 30 %} 30
  {% elif 30 <= states.sensor.temperatura_exterior.state | int < 35 %} 35
  {% else %} 40
  {% endif %}

thats working,thanks a lot bro