What's wrong trigger based sensor with automation

Is there anyone who could help me?
What I did:
Made the lines below in folder: templates.yaml

- trigger:
    - platform: time
      at: '00:00:00'
    - platform: event
      event_type: event_template_reloaded
  sensor:
    - name: 'Verlichting Schutting On'
      unique_id: '7781ebf3-23c6-437a-8c2f-2005de732261'
      state: "{{ (today_at('13:10:00') + timedelta(minutes = range(0,1) | random)).isoformat() }}"
      device_class: timestamp
    - name: 'Verlichting Schutting Off'
      unique_id: '842754c9-4047-48cd-8762-c2b7b8201f07'
      state: "{{ (today_at('13:15:00') + timedelta(minutes = range(0,1) | random)).isoformat() }}"
      device_class: timestamp

After this I made an automation like below:

alias: "Verlichting schutting automatisch "
description: Automatisch verlichting schutting aan random uit
trigger:
  - id: "on"
    platform: time
    at: sensor.verlichting_schutting_on_2
  - id: "off"
    platform: time
    at: sensor.verlichting_schutting_off_2
action:
  - service: switch.toggle{{ trigger.id }}
    target:
      entity_id: switch.verlichting_schutting

I also tried switch.turn_{{ trigger.id }}

When I switch the automation manualy the light is going on so thats seems to be okay.
It looks like the custom made sensor is not being activated.

Off course I already restart Home Assistant.

Somebody who could say whats wrong?

sensor.verlichting_schutting_on_2
                               ^^
                               ||
      Do you have more than one sensor with the same name?

Go to Developer Tools > States and find all sensors containing the words “Verlichting Schutting” then check their entity_ids.

Yes I’ve got to sensors with the same name.
The one without 2 isn’t working anymore that’s the reason for the 2.

If it isn’t working, why does it still exist in your system? Delete it.

I know you can use an input_datetime here, but I doubt you can use a sensor here.

Can you also tell us what you want to achieve, because it seems to me that you are making it unnecessarily complicated?

You are completely right. Just deleted

It’s permitted if its device_class is timestamp and its state value is a datetime string in ISO format.
Time Trigger - Sensors

Now that you have deleted it, it’s possible to rename the entity_id of Verlichting Schutting On

from sensor.verlichting_schutting_on_2

to

sensor.verlichting_schutting_on

You can rename it from the entity’s configuration screen. Here’s an example:


On a separate issue, I don’t know if you are aware but this will always report 0.

range(0,1) | random

First off all I want to learn programming :wink:
The idea is to switch the light (it’s a switch, that’s the reason to use switch.) on a specific time and switch random off in the evening.
The advantage of this method is that it is less vulnerable to a restart compared to a delay

To achieve this I want to use triggers.

I want to make it more complex to set on monday, tuesday wednesday etc. different times.
This because we go later to bed in the weekend.

Now the automation works, but not how I mentioned before.

alias: "Verlichting schutting automatisch "
description: Automatisch verlichting schutting aan random uit
trigger:
  - id: "on"
    platform: time
    at: sensor.verlichting_schutting_on
  - id: "off"
    platform: time
    at: sensor.verlichting_schutting_off
action:
  - if:
      - condition: trigger
        id:
          - "on"
    then:
      - service: switch.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: switch.verlichting_schutting
  - if:
      - condition: trigger
        id:
          - "off"
    then:
      - service: switch.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.verlichting_schutting

The second automation you posted refers to sensor.verlichting_schutting_on. Didn’t you say “The one without 2 isn’t working anymore”?

Yes, thanks.
Just a little bit lazy and don’t want to wait a long time :wink: #testing

Yes, on your recommendation I changed the name :smiley:
You are right to make it as clean as possible.

OK, now I understand what you meant. I didn’t realize you had already followed through on my advice and renamed the entity_ids.

This should now work:

alias: "Verlichting schutting automatisch "
description: Automatisch verlichting schutting aan random uit
trigger:
  - id: "on"
    platform: time
    at: sensor.verlichting_schutting_on
  - id: "off"
    platform: time
    at: sensor.verlichting_schutting_off
condition: []
action:
  - service: "switch.turn_{{ trigger.id }}"
    target:
      entity_id: switch.verlichting_schutting

Just remember that you cannot test this automation using its “Run” command.

You can also have a look here:

Thanks Taras, what you just send works!

Do you have tips for me to get more in detail with this kind of programming?
In the original post I missed the condition: , changed the original switch.toggle{{ trigger.id }} and changed the entity_ids.

Also thanks for your kindly reminder! #runcommand

For now I’m very happy with this.
I’ll check this weekend to expand this automation with different time’s at specific days.:grinning: :grinning:

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.