Simple if/then for automations

I’m new to HA, but not new to programming or home automation in general.

I’m having trouble creating a simple automation that can make a decision based on the current value of some blinds that I’m automating that respond to a button push. (The button push part works great, but trying to do this via separate automations causes conflicts between them and incorrect results…so I’d rather have just one automation take care of it.)

Anyways, it’s a multi-use button push that, when the state is partially open (value 58), will tell the blinds to open fully (value 100) and vice versa. (I have other buttons on my remote for close, etc)

I’ve cobbled together this automation together based on going through the community forums and some of the docs, but it causes a config error when I reload it in HA. Not sure how to get this working. Any help would be appreciated, thanks! :slight_smile:

- id: '1562484782659'
  alias: Hue - Blinds Open Dual-Action
  trigger:
  - entity_id: sensor.main_space_switch
    platform: state
  condition:
  - condition: state
    entity_id: sensor.main_space_switch
    state: 1_click_up
  action:
  - entity_id: cover.kitchen_1
    service: cover.set_cover_position
    data_template: 
      position:  
        {% if (state_attr(''cover.kitchen_1'', ''current_position'')|int) == 58 %} 100
        {% else %} 58 {% endif %}

The line position: needs to be position: >

In addition though, your trigger and condition are a bit long winded, and could be…

- id: '1562484782659'
  alias: Hue - Blinds Open Dual-Action
  trigger:
  - entity_id: sensor.main_space_switch
    platform: state
    to: '1_click_up' 
  action:
  - entity_id: cover.kitchen_1
    service: cover.set_cover_position
    data_template: 
      position: >
        {% if (state_attr('cover.kitchen_1' , 'current_position')|int) == 58 %} 100
        {% else %} 58 {% endif %}
1 Like

most likely the cause of your error was the missing > that @anon43302295 pointed out. That would lead to an error about an empty field.

Thanks for the help, the modified code help. I was trying different variations of things with > and >- and such last night with no luck.

Also, I had to put the “long-winded” code back because the shortened version would not fire with every button press of the same button. For this automation, it needs to fire every time any button is pressed, then it checks which one, and then the actions trigger based on the if then logic.

Ok, so one more quick thing. Based on the success of that automation running and the right code to make it work, I built two more automations to open and close the blinds in increments of 5 (scale of 0 to 100). The first one that does the addition works great, the one that does the subtraction doesn’t actually do the math for some reason. No error, it just doesn’t have the desired result (as in the blinds adjusting).

Any thoughts? (Again, need to keep them “long-winded” to make sure the automations fire every time the buttons are pressed) Thx all!

- id: '1562475424631'
  alias: Hue - Blinds Open By 5
  trigger:
  - entity_id: sensor.main_space_switch
    platform: state
  condition:
  - condition: state
    entity_id: sensor.main_space_switch
    state: 2_click_up
  action:
  - entity_id: cover.kitchen_1
    service: cover.set_cover_position
    data_template:
      position: >
        {% if (state_attr('cover_kitchen_1', 'current_position')|int) <= 95 %} {{ (state_attr('cover.kitchen_1', 'current_position')|int) + 5 }}
        {% else %} {{ state_attr('cover.kitchen_1', 'current_position') }} {% endif %}

- id: '1562481664070'
  alias: Hue - Blinds Close By 5
  trigger:
  - entity_id: sensor.main_space_switch
    platform: state
  condition:
  - condition: state
    entity_id: sensor.main_space_switch
    state: 3_click_up
  action:
  - entity_id: cover.kitchen_1
    service: cover.set_cover_position
    data_template:
      position: >
        {% if (state_attr('cover_kitchen_1', 'current_position')|int) >= 5 %} {{ (state_attr('cover.kitchen_1', 'current_position')|int) - 5 }}
        {% else %} {{ state_attr('cover.kitchen_1', 'current_position') }} {% endif %}

Alright, there was something strange about the 2nd automation that wasn’t working even after a correct copy and paste. I’m not sure what I did to fix it but it’s working now lol. Thanks all!

If you wish, you can streamline the data_templates in the two automations. It also improves their legibility.

For ‘Hue - Blinds Open By 5’

  action:
  - entity_id: cover.kitchen_1
    service: cover.set_cover_position
    data_template:
      position: >
        {% set p = state_attr('cover_kitchen_1', 'current_position')|int %}
        {{ p + 5 if p <= 95 else p }}

For ‘Hue - Blinds Close By 5’

  action:
  - entity_id: cover.kitchen_1
    service: cover.set_cover_position
    data_template:
      position: >
        {% set p = state_attr('cover_kitchen_1', 'current_position')|int %}
        {{ p - 5 if p >= 5 else p }}
1 Like