Combining 2 automations (screens open and close)

The following is one way to do it using trigger ids and a choose action. This can be done using just the UI editor. Triggers can be assigned an id by clicking on the menu button for each triggerand selecting Edit ID.

alias: "02 - Open badkamer jalouzie - 7u45"
description: ""
trigger:
  - platform: time
    at: "07:45:00"
    id: open
  - platform: sun
    event: sunset
    id: close
condition: []
action:
  - choose:
    - conditions:
        - condition: trigger
          id: open
      sequence:
        - device_id: 2ea2deaf37b3050457ea1874dad93fb3
          domain: cover
          entity_id: cover.badkamer_jalouzie
          type: set_position
          position: 100
    - conditions:
        - condition: trigger
          id: close
      sequence:
        - device_id: 2ea2deaf37b3050457ea1874dad93fb3
          domain: cover
          entity_id: cover.badkamer_jalouzie
          type: set_position
          position: 0
mode: single

FYI, trigger ids are reusable so if you have two distinct triggers that you want to execute the same sequence of action, you can simply given them the same id.

Hi Drew @Didgeridrew
Sorry, I posted a reaction, but then I saw a mistake I made. I have to try something …

It looks like I had a typo in my example… In the Action section, the two lines that start with id: each have an additional space in them. I have corrected it in my original post above.

Yes, that’s wat i noticed while i was making my remark which I canceled when i found that. :wink:
I wait to see if the automation is doing what it should.

I’l tried an other automation with a switch to combine the On and Off automation. but here i run into other errors. Hopefully you can take a look at that one as well?

alias: 40 - Test switch On Off at preset times
trigger:
  - platform: time
    at: "17:25:00"
  - platform: time
    at: "17:30:00"
  - platform: homeassistant
    event: start
action:
  - variables:
      t: ({{ now().hour }},{{ now().minute  }})
  - service: light.turn_{{ 'on' if t >= (17,25) or t < (17,30) else 'off' }}
    target:
      entity_id: switch.wcd_lidl_3

I found an example fot this here:
Automation with time

But it does not work. AT 17:25 i get a pop-up with Triggered, but the light connected to the switch does not go on.
BTW, from my dashboard or from the entity this switch is working perfectly.
In the automation overview window i can see that the automation has triggered 4 min ago…
This is what i see in the Trace window:
image

What strikes me is that it says Call a service based on a template. But i use an entity…

alias: example
description: ""
trigger:
  - platform: time
    at: "07:45:00"
  - platform: sun
    event: sunset
condition: []
action:
  - service: cover.set_cover_position
    target:
      entity_id: cover.badkamer_jalouzie
    data:
      position: "{{ iif(trigger.platform == 'time', 100, 0) }}"
mode: single
alias: 40 - Test switch On Off at preset times
trigger:
  - platform: time
    at: 
      - "17:25:00"
      - "17:30:00"
  - platform: homeassistant
    event: start
action:
  - service: "switch.turn_{{ iif( (17,25) <= (now().hour, now().minute) < (17,30), 'on' , 'off') }}"
    target:
      entity_id: switch.wcd_lidl_3

EDIT

Correction. Misspelled iif as iff.


EDIT 2

Correction. Replace service call for controlling lights with service call for controlling switches (because the referenced entity is a switch, not a light

hi @123 Thanks for trying to help me!
I tried your second example (40 - …) but that does not work in my situation.
I noticed that there is an iff function, i guess that should be iif. But i do not know what a iif function exactly is.
(If i change it to if i’l get this error: Error:
Error rendering service name template: UndefinedError: ‘if’ is undefined
Besides, in both examples you use different if-functions: iif and iff…

The Automation Editor accepts it when it is iif, but it does not switch on the light.
I searched the docs for an explanation of iif, but could not find it.

BTW here is the automation as it is now:

alias: 40 - Test switch On Off at preset times
trigger:
  - platform: time
    at:
      - "19:20:00"
      - "19:30:00"
  - platform: homeassistant
    event: start
action:
  - service: >-
      light.turn_{{ iif( (19,20) <= (now().hour, now().minute) < (19,30), 'on' ,
      'off') }}
    target:
      entity_id: switch.wcd_lidl_3

But unfortunately it does not work… Any idea’s?

It’s an Immediate If.

I misspelled it as iff in my second example and have corrected it to iif.

Post the automation’s trace.

This is the Trace:
image
Thanks for pointing me in the immediate if statement. I am not a programmer but have to learn this programming.

When viewing the automation’s trace, click the overflow menu in the upper right hand corner and select Download trace

Screenshot_20220923-134815~2

Copy-paste the contents of the downloaded file into your forum post. It contains far more information about what happened to the automation.


NOTE

This may not be necessary. See my post below.

I think I know what the problem is. The service call is for a light but the entity is a switch.

I have modified my second example to use the service call to control switches.

  - service: "switch.turn_{{ iif( (17,25) <= (now().hour, now().minute) < (17,30), 'on' , 'off') }}"
    target:
      entity_id: switch.wcd_lidl_3

Bingo! The switch automation worked! Thank you for your help!

But unfortunately the first automation, for the cover.badkamer_jalouzie did not trigger at sunset.
But I’l try that one tomorrow, first with opening at a convenient time. And then I’l try a test automation, not using the cover motor, but a light switch or something…

Great! :+1:

Post its trace (file), if one was created at the time of sunset.

In addition, copy-paste the following template into the Template Editor and confirm it reports the correct time for sunset in your location:

{{ (state_attr('sun.sun', 'next_setting') | as_datetime | as_local).time() }}

Yes, the template gives the correct time of sunset.
I’l check the automation tomorrow starting with the opening time.

If the automation produced a trace at sunset today, post the contents of that trace. I can review it and see why it failed to set the cover to position 0.

The time part did work! The jalouzie opened correctly.
But the sunset part, it seems that it was triggered, but the jalouzie did not close…

This is the Trace:

{
  "trace": {
    "last_step": "action/0",
    "run_id": "41de510d25ad495ab2a37f5ea1093f7b",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2022-09-24T17:33:24.956492+00:00",
      "finish": "2022-09-24T17:33:25.029004+00:00"
    },
    "domain": "automation",
    "item_id": "1663960932662",
    "trigger": "sunset",
    "trace": {
      "trigger/1": [
        {
          "path": "trigger/1",
          "timestamp": "2022-09-24T17:33:24.956676+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.20_example_badkamer_jalouzie",
              "state": "on",
              "attributes": {
                "last_triggered": "2022-09-24T13:41:40.048672+00:00",
                "mode": "single",
                "current": 0,
                "id": "1663960932662",
                "friendly_name": "02 - Badkamer jalouzie Open Sluit - (versie 123 Taras)"
              },
              "last_changed": "2022-09-24T17:01:19.843961+00:00",
              "last_updated": "2022-09-24T17:01:19.843961+00:00",
              "context": {
                "id": "01GDR74GK3WKESH802MJ1108J1",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "1",
              "idx": "1",
              "alias": null,
              "platform": "sun",
              "event": "sunset",
              "offset": {
                "__type": "<class 'datetime.timedelta'>",
                "total_seconds": 0
              },
              "description": "sunset"
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2022-09-24T17:33:24.972957+00:00",
          "changed_variables": {
            "context": {
              "id": "01GDR8Z8JWR90FXDWTM4GJ3GNV",
              "parent_id": null,
              "user_id": null
            }
          },
          "result": {
            "params": {
              "domain": "cover",
              "service": "set_cover_position",
              "service_data": {
                "position": 0,
                "entity_id": [
                  "cover.badkamer_jalouzie"
                ]
              },
              "target": {
                "entity_id": [
                  "cover.badkamer_jalouzie"
                ]
              }
            },
            "running_script": false,
            "limit": 10
          }
        }
      ]
    },
    "config": {
      "id": "1663960932662",
      "alias": "02 - Badkamer jalouzie Open Sluit - (versie 123 Taras)",
      "description": "Openen en sluiten badkamer jalouzie (07:40 en Sunset)",
      "trigger": [
        {
          "platform": "time",
          "at": "07:40:00"
        },
        {
          "platform": "sun",
          "event": "sunset",
          "offset": "-00:00:00"
        }
      ],
      "condition": [],
      "action": [
        {
          "service": "cover.set_cover_position",
          "target": {
            "entity_id": "cover.badkamer_jalouzie"
          },
          "data": {
            "position": "{{ iif(trigger.platform == 'time', 100, 0) }}"
          }
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01GDR8Z8JWR90FXDWTM4GJ3GNV",
      "parent_id": null,
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "02 - Badkamer jalouzie Open Sluit - (versie 123 Taras)",
      "message": "triggered by sunset",
      "source": "sunset",
      "entity_id": "automation.20_example_badkamer_jalouzie",
      "context_id": "01GDR8Z8JWR90FXDWTM4GJ3GNV",
      "when": 1664040804.957065,
      "domain": "automation"
    },
    {
      "when": 1664040806.461513,
      "state": "closing",
      "entity_id": "cover.badkamer_jalouzie",
      "icon": "mdi:blinds-vertical-closed",
      "context_event_type": "automation_triggered",
      "context_domain": "automation",
      "context_name": "02 - Badkamer jalouzie Open Sluit - (versie 123 Taras)",
      "context_message": "triggered by sunset",
      "context_source": "sunset",
      "context_entity_id": "automation.20_example_badkamer_jalouzie"
    }
  ]
}

It is an Aqara Zigbee device that works perfectly from the dashboard or a Zigbee battery powered switch. If i interpret the trace correctly, than the sunset is triggered.

I noticed however one other strange thing in the Trace. It has at line 20 this entity:

"entity_id": "automation.20_example_badkamer_jalouzie",   

And the example is a left over from the default configuration of creating an automation…
But that is not what is in the Automation:

alias: 02 - Badkamer jalouzie Open Sluit - (versie 123 Taras)
description: Openen en sluiten badkamer jalouzie (07:40 en Sunset)
trigger:
  - platform: time
    at: "07:40:00"
  - platform: sun
    event: sunset
    offset: "-00:00:00"
condition: []
action:
  - service: cover.set_cover_position
    target:
      entity_id: cover.badkamer_jalouzie
    data:
      position: "{{ iif(trigger.platform == 'time', 100, 0) }}"
mode: single

I suggest you confirm that the service call is able to close the cover.

  1. Copy the service call YAML shown below
  2. Go to Developer Tools > Services
  3. Switch the view to YAML mode
  4. Paste the YAML (overwrite anything that may already be there)
  5. Click Call Service button
  6. Confirm the cover gets closed
service: cover.set_cover_position
target:
    entity_id: cover.badkamer_jalouzie
data:
  position: 0

That works perfectly with position=0 and 100! I had never used this; still learning…
Perhaps a crazy question, but is there perhaps a way to simulate the event sunset at a self chosen time… I will try to use sunset with an offset. I’l try that first with an other automation with a light or switch to understand the offset parameter

With regard to the sunset automation for the cover.badkamer_jalouzie, I have tested with an offset for the sunset and that worked OK! So it should also work this eavening… Lets see…

But I have an other question about the use of variables.
In your example: Use variable for time value
Would it be possible to use variables for both time values (17,25) and (17,30) in:

service: "switch.turn_{{ iif( (17,25) <= (now().hour, now().minute) < (17,30), 'on' , 'off') }}"

Kind regards

Good.

Use the offset option (changing the timezone will also alter the time of sunset/sunrise but you have more control with offset).

Good.

Yes.