- "{{ is_true }}" condition True but condition never runs?

Hi,

Beginner in home assistant.
I am trying to understand automations and variables.
I have read this:
automation trigger variables

I think this would work as:
First trigger is evaluated 30 minutes before sunset… The variable is always True so the conditions says: “carry on”
Second trigger is evaluated before the first one since the offset is 1 hour before sunset.
I pasted the whole line:
is_true: "{{ states('sensor.condition') in ['cloudy', 'rainy'] }}"
in the developer tools template section and it gives: is_true: “True”.
This makes me think that this variable is_true evaluated to True and thus one hour before sunset the condition should be met and the action should be taken.

However. When I look in the traces the path always runs the first trigger and never the second one?

Can someone tell me why the rainy cloudy variable is never evaluated?

trigger:
  - platform: sun
    event: sunset
    offset: "-00:30:00"
    variables:
      is_true: true
  - platform: sun
    event: sunset
    offset: "-01:00:00"
    variables:
      is_true: "{{ states('sensor.condition') in ['cloudy', 'rainy'] }}"
condition:
  - "{{ is_true }}"
action:
  - service: script.sfeerlampen
    data: {}
mode: single

Try this:

trigger:
  - platform: sun
    event: sunset
    offset: "-00:30:00"
  - platform: sun
    event: sunset
    offset: "-01:00:00"
condition:
  - "{{ states('sensor.condition') in ['Cloudy', 'Rainy'] }}"
action:
  - service: script.sfeerlampen
    data: {}
mode: single

[edited for idiocy]

1 Like

But this would run nothing when it is not cloudy or rainy?
The condition is False when not cloudy or rainy. In that case I would like the lights turn on with an offset of -30

correct; it wasn’t clear to me that you only wanted the weather condition to apply to ‘sunset - 1hr’.

Try this:

trigger:
  - platform: sun
    event: sunset
    offset: "-00:30:00"
  - platform: sun
    event: sunset
    offset: "-01:00:00"
condition:
  - or:
      - condition: sun
        before: sunset
        before_offset: "00:30:00"
      - and:
          - "{{ states('sensor.condition') in ['Cloudy', 'Rainy'] }}"
          - condition: sun
            before: sunset
            before_offset: "01:00:00"
action:
  - service: script.sfeerlampen
    data: {}
mode: single

…so the automation triggers at both times, but only continues if it’s 30 mins pre-sunset or it’s 1hr pre-sunset with the defined weather conditions

The in test is case sensitive… make sure the state of your sensor and the comparison values are the same case…

Normally I would make sure everything is lowercase by using the lower filter just to be sure:

  - platform: sun
    event: sunset
    offset: "-01:00:00"
    variables:
      is_true: "{{ states('sensor.condition') | lower in ['cloudy', 'rainy'] }}"
condition:
  - "{{ is_true }}"

Just to clarify, I have tested this trigger variable/condition method on my instance and it does work.

Thanks but when using the developer tools state tab and entering sensor.condition I get

options:
  - clear
  - cloudy
  - fog
  - rainy
  - snowy
  - lightning
device_class: enum
entity_picture: https://www.buienradar.nl/resources/images/icons/weather/30x30/cc.png
friendly_name: Condition

So I do not think the problem is in the case.

Although the strange thing is that when I check in my dashboard the condition says Rainy.
I will give it a try

It is. Weather states are lowercase. Check the weather docs.

Because you’re confusing internal state with presentation. “Rainy” is the localised text based on the internal state rainy; partlycloudy becomes “Partly cloudy” . Not everyone in the world speaks English, which is why it’s done this way.

1 Like

Thanks for your help
With the filter is works!
So the solution is to use this:

is_true: "{{ states('sensor.condition') | lower in ['cloudy', 'rainy'] }}"

Although I am still not sure why. My code had ‘cloudy’ and ‘rainy’ with lowercase ‘c’ and ‘r’ and did not work.
Now with the filter it DOES work. That suggest that the state delivers Rainy or Cloudy.

Thanks for your help jchh. I did not try your solution since the solution with the filter looks cleaner.

1 Like