Simple "if...then...else"

Hello ,
can You please add with a yaml format for an automation that would have to fullfill the task according to the schematics here attached ?
Thank You in advance for your Support.

group:
  everyone:
    entities:
      - TRACKER-1
      - TRACKER-2

automation:
  alias: switch based on temperature when home
  trigger:
    platform: state
    entity_id: group.everyone
  action:
    service: >
      {% if trigger.to_state.state == 'home' %}
        {% if states('REAL_FEEL_SENSOR')| int < 10 %}
          switch.turn_on
        {% else %} switch.turn_off {% endif %}
      {% else %}
        {% if states('REAL_FEEL_SENSOR')| int < 4 %}
          switch.turn_on
        {% else %} switch.turn_off {% endif %}
      {% endif %}
    entity_id: SWITCH

(replace everything in capitals with the correct entity_ids)

I have to ask though, are you sure you want to trigger on the presence detection, rather than the temperature?

Hello mf_social,

the trigger for the switch is the temperature. however the value of the below limit temperature is set by the presence or absence of one of the trackers at the location.
is this what Your syntax is taking into account please?

Best regards,

That’s not the logic you put in your flow chart.

This would be more suitable…

group:
  everyone:
    entities:
      - TRACKER-1
      - TRACKER-2

automation:
  alias: switch based on temperature when home
  trigger:
    - platform: numeric_state
      entity_id: REAL_FEEL_SENSOR
      below: 10
    - platform: numeric_state
      entity_id: REAL_FEEL_SENSOR
      below: 4
  action:
    service: >
      {% if ( is_state('group.adults', 'home') and
        states('REAL_FEEL_SENSOR')|int < 10 ) or
        ( is_state('group.adults', 'not_home') and
        states('REAL_FEEL_SENSOR')|int < 4 ) %}
          switch.turn_on
      {% else %} switch.turn_off {% endif %}
    entity_id: SWITCH
1 Like

mf_social,
yes this makes sense :slight_smile:

yes, my logic schematics should have had an “and” between the trackers and the below temperatures.

thank You for Your remark. i will try the automation you provided.
Thankk You for it.
Best regards,

1 Like

Can someone please help me with my script? I’m trying to create a script that will open my garage door if it’s closed and close it if it’s open. To me, it sounds simple enough. As an FYI, I created a sensor to find that state of the garage door:

#Garage Door State
  - platform: template
    sensors:
      garage_door_status:
        friendly_name: 'State of Garage door'
        value_template: '{{ states.cover.garage_door_opener.state }}'

The sensor is working fine as it shows the state changing in “History.”

For the script, I used:

alias: Garage Door Button
sequence:
  - device_id: 4bfcf5dd82a8c5d1812884fe0e6f1e0c
    domain: cover
    entity_id: cover.garage_door_opener
    type: "{{ 'open' if garage_door_status == 'close' else 'close' }}"
mode: single

This resulted in an error:

Invalid config for [script]: value must be one of [‘close’, ‘close_tilt’, ‘open’, ‘open_tilt’, ‘stop’] for dictionary value @ data[‘type’]. Got None. (See /config/configuration.yaml, line 10).

I’ve also tried a more complicated statement:

garage_door:
  alias: Garage Door Button
  sequence:
  - device_id: 4bfcf5dd82a8c5d1812884fe0e6f1e0c
    domain: cover
    entity_id: cover.garage_door_opener
    type: >
      {% if garage_door_status == 'closed' %}
        open
        {% else %}
          {% if garage_door_status == 'open' %}
            close
          {% endif %}
      {% endif %}
  mode: single

Which resulted in the same error. Can you tell me what I’m possibly doing wrong?

You can’t use templates with device triggers/conditions/actions, use a service template.

Thanks, @anon43302295 for the response. However, I’ve been trying everything and to no avail! I did try a service (and I hope this is what you meant) entry in my script last night:

garage_door_button:
  alias: Garage Door Button
  sequence:
  - service: >
      {% if cover.garage_door_opener == 'closed' %}
        cover.open_cover
      {% else %}
        cover.close_cover
      {% endif %}
  data: {}
  entity_id: cover.garage_door_opener
  mode: single

But, it produce the same exact error. If this is not what you meant, could you please elaborate or even provide some code and directions? I am getting more proficient with Home Assistant, but some verbiage confuses me as well as errors I receive when creating code.

You can’t possibly be getting the exact same error as the previous error referred to the ‘type’ key which is part of the device action that you are no longer using.

The code you posted looks fine to me, but if you are getting an error you’ll need to post it.

Thanks, @anon43302295, I just tried again, using the exact code above. Please see the log below:

    Invalid config for [script]: value must be one of ['close', 'close_tilt', 'open', 'open_tilt', 'stop'] for dictionary value @ data['type']. Got None. (See /config/configuration.yaml, line 10).
    Invalid config for [script]: expected a dictionary. Got OrderedDict([('default_config', {}), ('tts', [OrderedDict([('platform', 'google_translate')])]), ('group', OrderedDict([('whoishome', OrderedDict([('name', 'Who is Home'), ('entities', ['device_tracker.life360_ea', 'device_tracker.life360_ma'])]))])), ('automation', [OrderedDict([('id', '1605087667281'), ('alias', 'Last Person to Leave Home'), ('description', ''), ('trigger', [OrderedDict([('platform', 'state'), ('entity_id', 'group.whoishome'), ('from', 'home')])]), ('c.... (See /config/configuration.yaml, line 10).
    Invalid config for [script]: [data] is an invalid option for [script]. Check: script->data. (See /config/configuration.yaml, line 10).

You have much bigger problems with your configuration than this script.

First error refers to a different script to this one.

Second error shows that you’re including top level configuration keys under a script key.

Third error shows the error for this script, which is that the last three lines need to be indented 2 spaces more.

I was thinking it has something to do with the code and, more specifically, indentations. Do you know why I would have the top two errors surface only when using the code with the IF/ELSE statements? When I use the code below, these errors are not present:

garage_door_button:
  alias: Garage Door Button
  sequence:
    - service: cover.open_cover
      data: {}
      entity_id: cover.garage_door_opener
  mode: single

I tried the code again with the IF/ELSE statements, but indented the bottom three lines two more spaces each and the same error showed up, but now there’s an additional error:

Invalid config for [script]: [mode] is an invalid option for [script]. Check: script->sequence->0->mode. (See /config/configuration.yaml, line 10). 

As a test, I erased the entire text from /config/scripts.yaml except for this code (this is all that’s included in that file):

garage_door_button:
  alias: Garage Door Button
  sequence:
  - service: >
      {% if cover.garage_door_opener == 'closed' %}
        cover.open_cover
      {% else %}
        cover.close_cover
      {% endif %}
    data: {}
    entity_id: cover.garage_door_opener
    mode: single

And, the error that I receive now is:

    Invalid config for [script]: value must be one of ['close', 'close_tilt', 'open', 'open_tilt', 'stop'] for dictionary value @ data['type']. Got None. (See /config/configuration.yaml, line 10).
    Invalid config for [script]: expected a dictionary. Got OrderedDict([('default_config', {}), ('tts', [OrderedDict([('platform', 'google_translate')])]), ('group', OrderedDict([('whoishome', OrderedDict([('name', 'Who is Home'), ('entities', ['device_tracker.life360_ea', 'device_tracker.life360_ma'])]))])), ('automation', [OrderedDict([('id', '1605087667281'), ('alias', 'Last Person to Leave Home'), ('description', ''), ('trigger', [OrderedDict([('platform', 'state'), ('entity_id', 'group.whoishome'), ('from', 'home')])]), ('c.... (See /config/configuration.yaml, line 10).
    Invalid config for [script]: [data] is an invalid option for [script]. Check: script->data. (See /config/configuration.yaml, line 10).
    Invalid config for [script]: [mode] is an invalid option for [script]. Check: script->sequence->0->mode. (See /config/configuration.yaml, line 10).

When I use the code below, there are no errors at all.

garage_door_button:
  alias: Garage Door Button
  sequence:
    - service: cover.open_cover
      data: {}
      entity_id: cover.garage_door_opener
  mode: single

The only difference(s) that I can see is the IF/ELSE parts.

All I can say without looking at your entire configuration is that the error messages are not consistent with what you are saying is happening :man_shrugging:

Thanks for your time and energy on this, @anon43302295! Should I check anywhere else specific that could be interfering with the code I want to use? Is there anything else I can try to accomplish my need? (And, I don’t think I ever explained what I’m hoping to accomplish. I purchased a few Amazon Dash Buttons and was looking to use one as a garage door opener using Dasshio. I can get it to open or close if I specify to open or close that “cover.” However, I wanted one button to be able to do both, determined on the state of the door. In other words, I want the button to open the door if it is closed or close it if the door is open.)

The problem sitting on the other side of the internet is that the only part of your configuration that I know about is this bit, so I don’t know how your configuration is split up, I don’t know what else you have in each yaml file so I don’t really know where to start. The only thing I can suggest is that you paste your configuration.yaml completely (obviously sanitised of any private information), and whatever file this script is in in its entirety including the name of the file and we can hopefully work it out from there :man_shrugging:

Hi @anon43302295, I think I’m getting somewhere now… I played around with your suggestion above re: the indentations for the bottom three lines and only added two spaces to the top two lines instead of all three. So, the new code is:

garage_door_button:
  alias: Garage Door Button
  sequence:
  - service: >
      {% if cover.garage_door_opener == 'closed' %}
        cover.open_cover
      {% else %}
        cover.close_cover
      {% endif %}
    data: {}
    entity_id: cover.garage_door_opener
  mode: single

That finally got rid of all of the errors I would get when I used Server Controls -> Check Configuration. I would then Reload Scripts, and still no errors.

However, whenever I run the script, I get a different error now:

homeassistant.exceptions.HomeAssistantError: Error rendering service name template: UndefinedError: 'cover' is undefined

Is this new error something you can further assist with?

First line of the template should be…

{% if states('cover.garage_door_opener') == 'closed' %}

WOOT! It worked perfectly! Thanks, @anon43302295! With your suggestion of the line indentations and the states(‘cover.garage_door_opener’) code change above, I was able to turn an Amazon Dash button into a garage door opener. I definitely would not have been able to accomplish this without your assistance. I think it was in large part the fault of the indentations and I would never have been able to guess that.

By any chance, is there a resource you can point me to that outlines the rules of indentations for Home Assistant? It might be more of a coding rule, or maybe general rules for Python? I have coded a bit before, but have never been stumped by indentations!

2 Likes

Basically it works like this:

The word before the colon is a key, what comes after the colon is a value. Now that may be a single value for the key

key: value

Or a list of values (lists are denoted with a -, and indented 2 spaces below their parent):

key:
  - value 1
  - value 2

Or it may be a number of key value pairs (which are indented by 2 spaces to show that they are the values of the ‘parent’ key:

parent_key:
  child_key_1: value 1
  child_key_2:
    - list_item_1
    - list_item_2

So if you apply that to an automation you have the top level key automation:, then a list of all your automations, so let’s build out the structure visually for two automations…

automation:
  - [automation 1]
  - [automation 2]

Now each automation needs a trigger and an action (other keys that are top level of an automation are alias etc, so these keys will all be indented to the same level

automation:
  - alias: Automation one
    trigger:
      [key value pairs for trigger]
    action:
      [key value pairs for action] 
  - alias: Automation two
    trigger:
      [key value pairs for trigger]
    action:
      [key value pairs for action] 

So now we add the key value pairs for each of the triggers, which will be indented 2 spaces below their respective parents. The keys we use will vary depending on the type of trigger we use or the type of action etc, so a time trigger needs an ‘at’ and a state trigger needs an entity_id and maybe a to or a from etc

automation:
  - alias: Automation one
    trigger:
      platform: time
      at: '00:00:00'
    action:
      [key value pairs for action] 
  - alias: Automation two
    trigger:
      platform: state
      entity_id: light.1
      to: 'on' 
    action:
      [key value pairs for action] 

Then let’s add the key value pairs for the actions. For automation two we’ll add a list of actions that will be carried out in the sequence they appear in the list…

automation:
  - alias: Automation one
    trigger:
      platform: time
      at: '00:00:00'
    action:
      service: light.turn_on
      entity_id: light.20
  - alias: Automation two
    trigger:
      platform: state
      entity_id: light.1
      to: 'on' 
    action:
      - service: notify.you
        data:
          message: "Light 1 is on, closing the blinds" 
      - service: cover.close_cover
        entity_id: cover.something 

So, to break down the indentation for the whole thing

  • automation: Far left edge, top level key

  • first hyphen 2 spaces in to begin the list of automations with automation one

  • alias: trigger: and action: of automation one, all aligned with each other as they are children of the automation key

  • time: and at: 2 spaces in from trigger as they are children of the trigger

  • service: and entity_id: 2 spaces in from action as they are children of the action key

  • next hyphen, 2 spaces from the left edge as it’s a child of the automation key at the top level, indicating the next automation in the list.

  • alias, trigger and action, as above

  • platform, entity_id and to, child keys for the trigger

  • service and data, these keys are the first action, note that ‘message’ is a child key of ‘data’ for the notify service

  • then we close the blinds!

Hope this explains it well enough :slightly_smiling_face:

5 Likes