Sonoff Countdown Timer

I would like to use a Sonoff POW R2 as a countdown timer.

I would like to be able to push the button on the switch and have it turn on, countdown 2 hours and then switch off. It would also be nice to see the remaining time on the HA dashboard.

Is this possible?

Thanks

You will need a timer

# Example configuration.yaml entry
timer:
  sonoff:
    duration: '02:00:00'

If you want this switch to only ever be on for 2 hours no matter how it turns on, that is easy.

If you want this to only do the 2 hour timer if you push the button…but leave it on always if you turn it on by other means, that is a bit harder. You would have to find a way to detect how the switch changed state (button vs …)

I’m only going to show the first scenario for now.

  1. Create an automation that watches when the switch turns on, then start the timer. Then create an automation to handle the timer event.
# In automations
- alias: Sonoff Timer Start
  trigger:
    platform: state
    entity_id: switch.sonoff_id
  action:
    # Start the timer if switch turns on. 
    # Cancel timer if it turns off before timer expired. 
    - service_template: >-
      {% if trigger.to_state.state == 'on' %}
        timer.start
      {% else %}
        timer.cancel
      {% endif %}
      data:
        entity_id: timer.sonoff

- alias: Sonoff Timer Expire
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.sonoff
  action:
  - service: switch.turn_off
    entity_id: switch.sonoff_id

For lovelace, just add the timer as an entity card. You should be able to pause this timer and the switch won’t turn off until the timer expires.

Making it so this only happens if you push the button…I’m not exactly sure the best way. I’m not sure how you have the sonoff integrated with Home Assistant.

Thank you for your assistance

I get the following error though:

missed comma between flow collection entries at line 146, column 8:
{% if trigger.to_state.state == ’ …
^

I have no idea how to fix this, could you please assist?

did you use ‘Copy to clipboard’ link at the top right corner of the code?

It was a syntax error, google helped me resolve it, so its working now.

Thanks again

The timer is very inaccurate though, I have it set to 2 minutes, sometimes it runs for 2 minutes, next run will be 8 minutes, then 25 minutes etc. Seems completely random.

Any idea how to make the timer more accurate?

so basically it’s not working… great. :\

No it doesnt work.

I need the automation to pick up the switch is on as it does show in lovelace that the switch is on and then run the timer.

show us your lovelace config for that

configuuration.yaml


switch:

  • platform: mqtt
    name: “EV Charge Point”
    state_topic: “stat/EV_Charge_Point/RESULT”
    value_template: “{{ value_json.POWER }}”
    command_topic: “cmnd/EV_Charge_Point/POWER”
    availability_topic: “tele/EV_Charge_Point/LWT”
    qos: 1
    payload_on: “ON”
    payload_off: “OFF”
    payload_available: “Online”
    payload_not_available: “Offline”
    retain: true

and what is the automation that does not work, could you post it here as well?
and please please always enclose your config in backquotes when posting here to preserve formatting.

It would be nice if you could read and follow this.

My apologies, I have remedied my post (I think)

The automation that doesn’t work is the one suggested above

    # In automations
- alias: Sonoff Timer Start
  trigger:
    platform: state
    entity_id: switch.ev_charge_point
  action:
    # Start the timer if switch turns on. 
    # Cancel timer if it turns off before timer expired. 
   - service_template: >-
      '{% if trigger.to_state.state == 'on' %}
        timer.start
      {% else %}
        timer.cancel
      {% endif %}
      data:
        entity_id: timer.sonoff'

- alias: Sonoff Timer Expire
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.sonoff
  action:
  - service: switch.turn_off
    entity_id: switch.ev_charge_point

nope. back quotes around your configs please.

No need, I’ve sorted it

boo, show us the solution!

hi,
i defined the following and getting an error:

  • id: Sonoff Timer Start
    alias: Sonoff Timer Start
    trigger:
    platform: state
    entity_id: switch.sonoff_AAA
    action:
    • service_template: >-
      ‘{% if trigger.to_state.state == ‘on’ %}
      timer.start
      {% else %}
      timer.cancel
      {% endif %}
      data:
      entity_id: timer.sonoff’
  • id: Sonoff Timer Expire
    alias: Sonoff Timer Expire
    trigger:
    • platform: event
      event_type: timer.finished
      event_data:
      entity_id: timer.sonoff
      action:
    • service: switch.turn_off
      entity_id: switch.sonoff_AAA

ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.sonoff_timer_start. Error rendering template for call_service at pos 1: UndefinedError: ‘trigger’ is undefined

let me guess… you’re triggering your automation manually, is that correct?
if yes, I’m not surprised (see Notes).

p.s when posting your config here, please enclose it in three back quotes to preserve formatting as it’s crucial.

yes i would like to trigger it manually.
what is the best way to have it ?
i would like to run it from the lovelace

Automation without a trigger is called a script.
However, you’ll need to change your config so it does not use trigger.
Could you explain what’s your use case?