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

thats perfect. thank You. do you know if the refresh rate on an automation can be set? or if there is a setting on the devices position refresh rate? the automation works but it is laggy. the detection of the gps positon is made by the phone, and the switch is a tplink plug.

The automation will trigger the moment the state of the device tracker updates, so no need for a refresh rate there.

As for the device tracker, you’ll have to look at the settings for whatever software you’re using.

Or consider using a proximity based tracker for the automation, like ping or Bluetooth.

i see.
thank You very much for Your support.

2 Likes

Hello. I added to my automation a secondary action that informs me about the outcome of te action taken. Ow on each execution of the automation i receive the notification. Is there a way to receive the notification only when the outcome changes( when passing from if to 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: