Condition in automation is not working

Hi,
I have a strange issue. I am trying to automate my covers and I can control them through homeassistant just fine and I can also read the current position.
Now, what I am trying to do is to trigger an action only if the cover is closed (position is 0). However, the command is also triggered if the position > 0.

I also checked the template tool within developer tools and the state is showing as I would expect there.

  condition:
  - condition: template
    value_template: '{{ state_attr("cover.cover_bedroom_left", "current_position")
      < 1}}'
  action:
  - data:
      entity_id: cover.cover_bedroom_left
      position: 2
    service: cover.set_cover_position

not sure if it matters but the component is controlled via mqtt

Any hints on how to debug further?

Try :

  action:
  - service: cover.set_cover_position
    entity_id: cover.cover_bedroom_left
    data:
      position: '2'

Try this:

"{{ is_state_attr('cover.cover_bedroom_left', 'current_position', 0) }}"

To make it work with what you have though, you probably need to use the | float (or | int) filter:

"{{ state_attr('cover.cover_bedroom_left', 'current_position') | float < 1 }}"

Remember all states in HA are strings unless converted, try : -
“{{ state_attr(‘cover.cover_bedroom_left’, ‘current_position’) | int == 0 }}”
or slightly simpler in manipulation terms : -
“{{ state_attr(‘cover.cover_bedroom_left’, ‘current_position’) == ‘0’ }}”

But I’d go with the first, also though your quote usage is syntactically correct we (on the forum) prefer to use doubles on the outside, singles on the inside

1 Like

But attributes, like the one he’s using, are not necessarily strings, and often aren’t.

@progonn, what does the following return in the Template Editor:

{{ state_attr("cover.cover_bedroom_left", "current_position") is number }}
{{ state_attr("cover.cover_bedroom_left", "current_position") is string }}

Hey.!
You beat me to it (slow one finger typing)

thanks for all the responses so far.

“is number” is giving me true and “is string” is returning false. I also tried converting the attribute to int and even treating it as a string with == “0” but nothing worked. :confused:

Let me guess, you’re clicking the TRIGGER button in the “more info” window of the automation. If you do that (or use the automation.trigger service), it ignores the triggers & conditions and just runs the actions. To test the automation conditions you have to cause one of the triggers to fire.

3 Likes

oh thanks. That was indeed the case. I wasn’t aware it isn’t checking conditions if I trigger the automation using the “more info” window :see_no_evil: