Timer question in automation

I’ve creaeted an automation for my bed lights it’s as follows:

- alias: 20a - Bed Lights
  id: Bed Lights On
  initial_state: true
  trigger:
    - platform: sun
      event: sunset
  action:
    service: switch.turn_on
    entity_id: switch.bed_lights_2

- alias: 20b - Bed Lights Off
  trigger:
    - platform: state
      entity_id: binary_sensor.bed_sensor_contact_2
      from: "off"
      to: "on"
  action:
    - service: timer.start
      target:
        entity_id: timer.out_of_bed

- alias: "Timer Reset for Bed"
  id: "Bed Reset Timer"
  trigger:
    - platform: state
      entity_id: binary_sensor.bed_sensor_contact_2
      from: "on"
      to: "off"
  action:
    - service: timer.cancelled
      target:
        entity_id: timer.out_of_bed

- alias: "Timerstop"
  id: "Timerstop"
  trigger:
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.out_of_bed
  action:
    - service: switch.turn_off
      entity_id: switch.bed_lights_2

I’m getting an errror on the timer.cancelled portion…

Timer Reset for Bed uses an unknown service
The automation “Timer Reset for Bed” (automation.timer_reset_for_bed) has an action that calls an unknown service: timer.cancelled.
This error prevents the automation from running correctly. Maybe this service is no longer available, or perhaps a typo caused it.
To fix this error, [edit the automation](/config/automation/edit/Bed Reset Timer) and remove the action that calls this service.
Click on SUBMIT below to confirm you have fixed this automation.

I’m glad that HA gives me the info but perhaps I used the timer.cancelled command incorrecdtly? If so how should I use it or is tiimer.cancelled no longer working?

Thank you in advance!!

timer.cancel
is what comes up in dev tools > services.

1 Like

You are right… I was not reading the timer documentation correctly. HA fires the timer.cancelled event and you call it by using timer.cancel :slight_smile:

Now it works as it should!

Thank you!!!

1 Like

If you are interested, here’s how to consolidate the four automations into one.

- alias: Bed Lights
  id: Bed Lights
  trigger:
    - platform: sun
      event: sunset
    - platform: state
      entity_id: binary_sensor.bed_sensor_contact_2
      from:
        - "off"
        - "on"
      to:
        - "on"
        - "off"
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.out_of_bed
  condition: []
  action:
    - if: "{{ trigger.platform == 'state' }}"
      then:
        - service: "timer.{{ iif(trigger.to_state.state == 'on', 'start', 'cancel') }}"
          target:
            entity_id: timer.out_of_bed
      else:
        - service: "switch.turn_{{ iif(trigger.platform == 'sun', 'on', 'off') }}"
          target:
            entity_id: switch.bed_lights_2

I don’t think any of the three triggers can occur simultaneously so there’s probably no need to use mode: queued.

OH I’m interested!!!
Thank you so much!!

That is some VERY nice work…above my pay grade LOL
But I’ll be studying that for sure!!
:slight_smile:

In case anyone is interested here’s the finished code Thank you @123

I created 2 switches so I could put them in lovelace and show them in the frontend…
This is the automation to turn off those switches.

Created the out_of_bed timer in the helper section

- alias: 47 - Turn off door alarms
  description: ""
  trigger:
    - platform: state
      entity_id: binary_sensor.bed_sensor_contact_2
      from:
        - "off"
        - "on"
      to:
        - "on"
        - "off"
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.out_of_bed
  condition: []
  action:
    - if: "{{ trigger.platform == 'state' }}"
      then:
        - service: "timer.{{ iif(trigger.to_state.state == 'on', 'start', 'cancel') }}"
          target:
            entity_id: timer.out_of_bed
      else:
        - service: "switch.turn_{{ iif(trigger.platform == 'sun', 'on', 'off') }}"
          target:
            entity_id:
              - switch.bd_alarm
              - switch.fd_alarm