Condition time wrong validation when after and before are used

Hello,

i have a Problem with the time condition.

I used the following condition in an automation:

  - condition: time
    after: "23:51:59"
    before: "22:59:00"

the Time it was triggered was:
'23:51:59.146700"

and the trace result gives the following result:

after:
  __type: <class 'datetime.time'>
  isoformat: '23:51:59'
now_time:
  __type: <class 'datetime.time'>
  isoformat: '23:51:59.146700'
before:
  __type: <class 'datetime.time'>
  isoformat: '22:59:00'
result: true

Why did he says that the result is true?

When I split it into two separate conditions it works fine

Because it is true. When you use both before and after the span can cross the the midnight boundary.

So 23:51:59.146700 is after 23:51:59 and before 22:59:00 on the next day.

https://www.home-assistant.io/docs/scripts/conditions/#time-condition

Note that this will never be true:

  - condition: time
    before: "22:59:00"
  - condition: time
    after: "23:51:59"

The current time can not be both before "22:59:00" and after: "23:51:59".

What time range do you actually want?

From the beginning of the day to 11pm is just:

  - condition: time
    before: "23:00:00"

Yes you are right, I found it by my self, and had to realize that this was miss configured. Shame on me.

But my failure was on another side.
I run the automation manualy and in this case he ignored the condition in total.
The trace says that the condition was not running but the action was running.

Is it the normal case, that the conditions are not active at manual autmation run? (run now)

Correct, unless you run the automation from Developer Tools → Actions and do this:

1 Like

A really big thank you to you.

I get the target near step by step.

But the next crazy thing (for me) happens.

I have a variable defined which value is the result of an condition (True)

variables:
  windows: !input windows
  windows_are_open: "{{ windows | select('is_state', 'on') | list | count > 0}}"

and use a template condition


conditions:
  - "{{ not(windows_are_open) }}"

The trace result is the following:

result: true
entities: []

Step configuration:

'{{ not(windows_are_open) }}'

windows are a collection of window sensors and the condition always say the result is true with both situation (window opened and closed)
I also send the variable value via mesage to my phone (as action) and the value is correct. Only the condition always says true

I can not find my issue.
I am pleased for your help

Try it like this:

'{{ not windows_are_open }}'

no effect/change

It seams that the Variable contains a string “True” or “False” and the condition always thinks ok there is a string and a non empty string is always true

Ah. Try this:

'{{ not windows_are_open|bool }}'

https://www.home-assistant.io/docs/configuration/templating/#numeric-functions-and-filters

Nope has no effect

Share the full automation config.

It is a blueprint but at the end it is the same.

blueprint:
  name: Test
  description: test
  domain: automation
  input:
    windows:
      name: Window sensors
      description: Sensors for window open detection
      selector:
        target:
          entity:
            - device_class:
                - window
            - domain:
                - binary_sensor
                - sensor
    start_time:
      name: Start time
      description: When he automation starts to check.
      default: "13:00:00"
      selector:
        time:
    end_time:
      name: End time
      description: When he automation ends to check.
      default: "19:00:00"
      selector:
        time:
variables:
  windows: !input windows
  windows_are_open: "{{ windows | select('is_state', 'on') | list | count > 0 }}"
triggers:
  - trigger: time
    at: !input start_time
conditions:
  - condition: time
    after: !input start_time
    before: !input end_time
  - "{{ not windows_are_open|bool }}"
actions:
  - action: switch.toggle
    metadata: {}
    data: {}
    target:
      entity_id: switch.steckdose_plug_p3
  - action: notify.mobile_app_iphone_xxx
    data:
      message: "ddycsdc<{{ windows_are_open }}><{{ windows_are_open | int }}>"
mode: single

After debugging a while i think it is related to the target input.

windows | select('is_state', 'on')

is always empty.

How can i access the entities of a target selector (in this case i use labels) ?
And another thing related to this, how can i test target selectors in the template section of the dev-tools ? There I used label_entities and it worked but it seems working different to the targets selector in a blueprint

Why not just use an Entity selector…?

Target selectors are more of a specialized tool and, IMO, rarely the right choice for most use cases. You can find an explanation of the target object structure in the following article in the forum Cookbook:

Ok reply to the first question, I use labels because aof flexebility. I often had to replace, move, extend, … devices in my HA setup. So I organized my howl device managment by labels. At the moment this is the state to go with.

I found a little solution (which i will link to below). There is a little variable template block which setts a variabe with all underling entities by a given target selector.

It would be great to get this funcionality as a root method/function of Home Assistant. Something like target_entities() !

So here is the solution code:

variables:
  covers_target: !input windows_target
  covers: >
    {%- set ns = namespace(ret=[]) %}
    {%- for key in ['device', 'area', 'entity', 'label'] %}
      {%- set items = covers_target.get(key ~ '_id', [])  %}
      {%- if items %}
        {%- set items = [ items ] if items is string else items %}
        {%- set items = items if key == 'entity' else items | map(key ~ '_entities') | sum(start=[]) %}
        {%- set ns.ret = ns.ret + [ items ] %}
      {%- endif %}
    {%- endfor %}
    {{ ns.ret | sum(start=[]) }}

Found on Home Assistant: using target in blueprints - Evan Moses