hartmood
(Hartmood)
April 14, 2024, 4:34pm
1
Hi,
How can I add a timer here so that the switch automatically turns of after 20 minutes before it switches on again on minute 51.
alias: "Garage Lüfter Timer "
description: ""
trigger:
- platform: time_pattern
minutes: "51"
condition: []
action:
- type: turn_on
device_id: 9ec6c0db4e4e7efab120b8f9a497f7ba
entity_id: b1dcef5154360ea5605caa08dade74d0
domain: switch
francisp
(Francis)
April 14, 2024, 4:38pm
2
Please format your code
Spaces at the beginning of the line are critical in yaml, and people trying to help you may want to copy your code so that they can try it out themselves, so please format it correctly when you are posting.
Use the preformatted text tool (</> in the edit post toolbar). If it is not immediately visible, it will be in the cogwheel menu.
[Code_format]
Alternatively you can use three backticks (the key is on the far left, 2nd row on en keyboards) on their own line above and below the code. That i…
and maybe good to know too:
Background
When you start writing automations, a device trigger seems the obvious choice. Everybody knows what a device is and it comes at the top of the UI dropdown list.
[image]
It works… but it is certainly not a “great way to start” because it is storing up problems for the future. Far better to use entity, with state or numeric_state.
Device_id problems
The yaml will be longer and harder to follow, which may make maintenance difficult, especially a few months down the line when you h…
123
(Taras)
April 14, 2024, 4:41pm
3
alias: "Garage Lüfter Timer "
description: ""
trigger:
- id: 'on'
platform: time_pattern
minutes: "51"
- id: 'off'
platform: time_pattern
minutes: "11"
condition: []
action:
- service: "switch.turn_{{ trigger.id }}"
target:
entity_id: switch.your_switch
EDIT
Correction. Replaced platform
with service
.
1 Like
francisp
(Francis)
April 14, 2024, 4:42pm
4
Seems good to me, just add a second automation
trigger:
- entity_id: b1dcef5154360ea5605caa08dade74d0
for: 00:20
platform: state
to: 'on'
action:
- data:
entity_id: b1dcef5154360ea5605caa08dade74d0
service: switch.turn_off
Better use real entity_id instead of b1dcef5154360ea5605caa08dade74d0
hartmood
(Hartmood)
April 14, 2024, 5:02pm
5
Thanks. Where do I get the trigger.id from?
123
(Taras)
April 14, 2024, 5:31pm
6
The value of trigger.id
will be the value of the id
parameter specified in each one of the two Time Pattern Triggers.
For more information, refer to Available Trigger Data .
123:
trigger:
- id: 'on'
You define it yourself in the above section of the code. If you’re using the visual editor, just click on the 3 dots next to the trigger (When) section > Edit ID. Make sure you only use ‘on’ or ‘off’, otherwise the template in the action part won’t work and you’ll have to set up separate actions manually
123
(Taras)
April 14, 2024, 5:38pm
8
I suggest you simply copy the example I posted directly into a new automation (or overwrite your existing automation) then replace switch.your_switch
with the entity_id of your switch.
For more information, refer to the “How to import an automation from the forum” section of the following post:
Users often feel the urge to exchange automations for sharing, guidance or debugging.
To do so, using screenshots is NOT the correct way.
Basically, the automation GUI is nothing more then a yaml generator, and the forum is very capable of working with that. Therefore the preferred way of exchanging automations is to use YAML.
How to export an automation to the forum:
From withinthe automation editor, select the three little dots in the upper right corner, and choose ‘Edit in YAML’.
[image…
1 Like
hartmood
(Hartmood)
April 14, 2024, 7:29pm
9
This is exactly what I have done and what the error causes in the line with the trigger.id
123
(Taras)
April 14, 2024, 8:30pm
10
You’re right, I made a mistake. I have corrected the example posted above.
Replace this:
- platform: "switch.turn_{{ trigger.id }}"
with this:
- service: "switch.turn_{{ trigger.id }}"
hartmood
(Hartmood)
April 15, 2024, 2:47pm
11
That worked well. Thanks a lot for your kind help. Now I don’t have to use two automations anymore.
1 Like