Help with Automation for timed switch in hassio. Total Newb

Hi Guys,

Just wondering if anyone can help me.

I am using a sonoff SV as I only require 12v to be switched. I have set it up so it has 2 switches. A main switch to turn on the christmas audio and a secondary which I am using as an automation timer.

I am trying to run an automation so when I push the secondary switch the audio for my Christmas lights turns on for a set period of time. but if i hit the normal switch it stays on consistently.

I am trying to remove any bugs at the moment as I have the timer working. I am most likely going to get multiple button pushes from the people coming to see the lights which is where I am having the issue.

the bug I am trying to sort is if the secondary button is pushed the audio turns on for a given period but if the secondary button is pushed within the given period of time the timer has no effect and the audio stays turned on.

How do I make the timer persistent so that even if the secondary switch is toggled to off the main switch still turns off after the given period of time but if the secondary switch is toggled back to on the timer starts again?

I don’t want to set a timer on the main switch as i want to be able to turn the audio on constantly if required.

I hope that makes sense.

Here is my mqtt switch config:

switch:
  - platform: mqtt
    name: "Christmas Audio"
    command_topic: "cmnd/christmas_audio/power1"
    state_topic: "stat/christmas_audio/POWER1"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
  - platform: mqtt
    name: "Christmas Audio Timed"
    command_topic: "cmnd/christmas_audio/power2"
    state_topic: "stat/christmas_audio/POWER2"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

Here are the automation’s:

- id: 'Christmas Light Audio On'
  alias: Christmas Audio On
  trigger:
  - entity_id: switch.christmas_audio_timed
    platform: state
    to: 'on'
  action:
  - service: switch.turn_on
    entity_id: switch.christmas_audio

- id: 'Christmas Light Audio Timer'
  alias: Christmas Audio Timer
  trigger:
  - entity_id: switch.christmas_audio_timed
    platform: state
    to: 'on'
    for:
      minutes: '15'
  action:
  - service: switch.turn_off
    entity_id: switch.christmas_audio
  - service: switch.turn_off
    entity_id: switch.christmas_audio_timed

I would appreciate any help.

Cheers,
Patrick

Please read the blue banner at the top of the page and edit your post accordingly.

1 Like

Thanks mate. As I said I am a complete Newb.

I’m trying to get my head around the problem.

There are two switches.

Main switch - audio turns on and off with the state of this switch. Audio output is wired to this switch.

Second switch - audio should come on for 15 minutes when this switch is on. Switching off does nothing. The timer should not reset to start counting 15 minutes again if the switch is pushed more than once.

Is that right?

1 Like

Ho @tom_l

You are correct with the main switch and I am happy with that functionality.

the second switch

Current:
turns the main switch on and if no other state changes it turns off after 15 minutes
turns the main switch on but if the state changes to off it disables the timer, if switched back on the timer starts again for the 15 minutes

What I want:
once state changed to on run a timer for 15 minutes.
if the state changes to off within that 15 minutes continue with the original 15 minutes
if the state changes to on reset timer to an additional 15 minutes

What I can live with (if easier):
if it makes it easier I am happy to make it so every time the second switch is pushed it resets the timer for 15 minutes.

I hope this clears up any confusion with what I need.

Cheers,
Patrick

Ok, this should work:

Define a timer component:

timer:
  xmas_audio_timer:
    duration: '00:15:00'

No need to automate the main switch turning on as it is wired to the audio. However we may need to cancel the timer if the main switch is turned off:

- id: cancel_audio_timer
  alias: 'Cancel audio timer'
  trigger:
    platform: state
    entity_id: switch.christmas_audio
    to: 'off'
  condition:
    condition: state
      entity_id: timer.xmas_audio_timer
      state: 'active'
  action:
    - service: timer.cancel
      entity_id: timer.xmas_audio_timer
    - service: switch.turn_off  ## if it's already off this wont matter
      entity_id: switch.christmas_audio_timed

If the timer expires we want the audio to turn off:

- id: audio_timer_finished
  alias: 'Audio timer finished'
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.xmas_audio_timer
  action:
    - service: switch.turn_off
      entity_id: switch.christmas_audio
    - service: switch.turn_off
      entity_id: switch.christmas_audio_timed

To (re)start the timer with the second switch:

- id: christmas_light_audio_timer
  alias: 'Christmas Light Audio Timer'
  trigger:
    entity_id: switch.christmas_audio_timed
    platform: state
    to: 'on'
  condition:  ## omitting this condition will allow the timer to reset each time the second switch turns on ##
    condition: state
      entity_id: timer.xmas_audio_timer  
      state: 'idle'
  action:
    - service: switch.turn_on   ## if it's already on this wont matter
      entity_id: switch.christmas_audio
    - service: timer.start   ## if it's already on this will reset the timer
      entity_id: timer.xmas_audio_timer

Leave out the condition if you want the timer to reset every time the second switch is turned on.

This is untested and I’ve probably made some mistakes. Let me know how it goes.

1 Like

That was amazing.

I omitted the condition in the last automation for now, but if kids push the button constantly I may add it back in when it all goes live.

This is required as last year was my first year doing a Christmas display and the music on constantly till late starts to do your head in after a few days. Now it will be on constantly through the FM transmitter and only loud when the button is pressed and the fm receiver to the amp is turned on.

Thank you so much for your help. I have learned a lot. I had no idea about timers as a component.

No problem. Glad to help.

It’s definitely worth browsing through the component section if you get the chance. Lots of goodies in there.

1 Like

The issue is knowing how to implement them properly.

I’m stuck on the google maps device tracking at the moment.

Owntracks just does not update from my wife’s phone and i have set the setting the same for both phones and as they are both android it makes sense just to use the google maps option (if I can get it to work).

I am still learning and there is a lot to learn and it is constantly growing and changing.

I was so excited when i got google home integration working and then the update broke it and I had to work out how to fix it as when I followed the instructions it didn’t work. But after an hour or 2 of yelling at the computer it worked.

I’m getting there slowly.

:rofl: Get used to that.

HA is a rapidly evolving system of systems. Sometimes manufacturer’s change things, sometimes the original way things were done in HA doesn’t work out in the long run and breaking changes are needed. Always read the breaking changes for every new HA release.

I cant help with Google maps unfortunately. I don’t use it.

I’m happy for the evolution of the system. But I still get frustrated by the changes when they break things. Don’t get me wrong. I appreciate what the dev’s do and it is amazing to have such an awesome system running on a cheap raspberry pi.

There have been a few breaks that I have had to figure out, and it forces you to learn (and re learn what has vanished from memory).

I need to start reading the breaking changes before doing updates. But usually i just smash the update button without even thinking.

I’ll figure out the google maps in time. Just a lot of the info on the forum with the fixes goes straight over my head.

Thanks again for all your amazing help!

I always check them so I know what I’m letting myself in for.