[Solved] Permanent automation for a switch?

Hi

I have a little programming issue that I’d like to resolve nicely and not with complicated code :wink: I have a water heater that I want to get on only during night ! My initial plan was to put it on at let say 8pm and then off at 8am. My issue is this one:
Setup is done in a place where we have often electricity cut or problem like that so it might happen pretty regularly that whole system restarts after an electricity cut.
How to be sure each night my water heater will be on just for time specified ? as if there is an electric cut at 7h59pm till 8:04 pm for example it means my automation at 8pm won’t trigger right ?? Is there a way to do some time comparisons ? such as if time is “above” 8pm and “under” 8am then trigger that ???

Thanks

Vincèn

I think you could do something like this:

trigger:
  platform: time
  minutes: '/1'
  seconds: 00
condition:
  condition: and
  conditions:
    - condition: time
      after: '20:00:00'
      before: '08:00:00'
    - condition: state
      entity_id: your.sonoff
      state: 'off'

Thanks @sjee for help about this so I adapted your code at my system like that:

- alias: Chauffe Eau Nuit
  trigger:
    platform: time
    minutes: '/1'
    seconds: 00
  condition:
    - condition: time
      after: '20:00:00'
      before: '08:00:00'
  action:
    service: switch.turn_on
    entity_id: switch.switch_sonoff_4_relais_1
- alias: Chauffe Eau Jour
  trigger:
    platform: time
    minutes: '/1'
    seconds: 00
  condition:
    - condition: time
      after: '08:00:00'
      before: '20:00:00'
  action:
    service: switch.turn_off
    entity_id: switch.switch_sonoff_4_relais_1 

and looks to work as expected (I putted my relay on on Sonoff, then added the automation in HA and restarted HA and then it putted the relay off :yum:

Just need to figure out how to control more than one relay in same time but works already perfect !!

Thanks :wink:

Vincèn

Great. Adding more relays to the same automation is simple on your entity_id you can just add the second relay like this: switch.switch_sonoff_4_relais_1, switch.switch_sonoff_4_relais_2

1 Like

@sjee Thanks for the info and so I could combine easily multiple commands in one action :+1:

action:
    service: switch.turn_on
    entity_id: switch.switch_sonoff_4_relais_1,switch.switch_sonoff_4_relais_3
    service: switch.turn_off
    entity_id: switch.switch_sonoff_4_relais_2,switch.switch_sonoff_4_relais_4

Sorry but I noticed that HA doesn’t like that syntax in fact complaining about duplicate keys in configuration file :frowning: Any suggestions what’s wrong here ??

Thanks

- alias: Chauffe Eau Nuit
  trigger:
    platform: time
    minutes: '/1'
    seconds: 00
  condition:
    - condition: time
      after: '20:00:00'
      before: '08:00:00'
  action:
    service: switch.turn_on
    entity_id: switch.switch_sonoff_4_relais_1,switch.switch_sonoff_4_relais_3
    service: switch.turn_off
    entity_id: switch.switch_sonoff_4_relais_2,switch.switch_sonoff_4_relais_4
- alias: Chauffe Eau Jour
  trigger:
    platform: time
    minutes: '/1'
    seconds: 00
  condition:
    - condition: time
      after: '08:00:00'
      before: '20:00:00'
  action:
    service: switch.turn_on
    entity_id: switch.switch_sonoff_4_relais_2,switch.switch_sonoff_4_relais_4
    service: switch.turn_off
    entity_id: switch.switch_sonoff_4_relais_1,switch.switch_sonoff_4_relais_3

Not sure and I can’t test it right now but I have a space between the comma and the next entity.

switch.switch_sonoff_4_relais_1, switch.switch_sonoff_4_relais_2

Nope it complains about duplicate entries (the ones I had to duplicate for on and off states) and in fact HA executes only the first service action for each trigger and not both :frowning:

2017-10-03 07:14:47 ERROR (Thread-2) [homeassistant.util.yaml] YAML file /home/homeassistant/.homeassistant/automations.yaml contains duplicate key "service". Check lines 10 and 12.
2017-10-03 07:14:47 ERROR (Thread-2) [homeassistant.util.yaml] YAML file /home/homeassistant/.homeassistant/automations.yaml contains duplicate key "entity_id". Check lines 11 and 13.

Try this

  action:
    - service: switch.turn_on
      entity_id: switch.switch_sonoff_4_relais_1, switch.switch_sonoff_4_relais_3
    - service: switch.turn_off
      entity_id: switch.switch_sonoff_4_relais_2, switch.switch_sonoff_4_relais_4

Hum only change is space after the , right ? I tried it but still the same about duplicate entries :frowning:

No, it’s about the - in front of the service calls.

It is :+1: Problem solved and logic functioning now as it should :wink:

Thanks a lot :wink: