Sonoff automation based on time

Im trying to setup my Sonoff switches to turn on and off automatically based on the time of the day, but i cant seem to find a proper code for it

Use a time trigger.

- id: '1568056505078'
  alias: TV hoek aan
  trigger:
  - at: 05:00
    platform: time
  condition: []
  action:
      entity_id: switch.flamingo1
    service: switch.turn_on

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

automation:
- alias: Automation Name
  trigger:
  - platform: time
    at: 17:29:00
  action:
  - service: switch.turn_off
    entity_id: switch.sonoff2
    
switch:
    platform: mqtt
    name: "Sonoff Switch 01"
    command_topic: "cmnd/sonoff1/power"
    state_topic: "stat/sonoff1/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
    
switch 2:
    platform: mqtt
    name: "Sonoff Switch 02"
    command_topic: "cmnd/sonoff2/power"
    state_topic: "stat/sonoff2/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
 
sensor sonoff_pow_watts:
    platform: mqtt
    name: Switch1  Power Usage
    state_topic: "tele/sonoff1/SENSOR"
    value_template: "{{ value_json['ENERGY'].Power }}"
    qos: 2
    unit_of_measurement : "W"

sensor sonoff_pow_volts:
    platform: mqtt
    name: Switch1 Voltage
    state_topic: "tele/sonoff1/SENSOR"
    value_template: "{{ value_json['ENERGY'].Voltage }}"
    qos: 2
    unit_of_measurement : "V"

sensor sonoff_pow_current:
    platform: mqtt
    name: Switch1 Current Amps
    state_topic: "tele/sonoff1/SENSOR"
    value_template: "{{ value_json['ENERGY'].Current }}"
    qos: 2
    unit_of_measurement : "A"
    
    
sensor sonoff_pow_watts 2:
    platform: mqtt
    name: Switch2  Power Usage
    state_topic: "tele/sonoff2/SENSOR"
    value_template: "{{ value_json['ENERGY'].Power }}"
    qos: 2
    unit_of_measurement : "W"

sensor sonoff_pow_volts 2:
    platform: mqtt
    name: Switch2 Voltage
    state_topic: "tele/sonoff2/SENSOR"
    value_template: "{{ value_json['ENERGY'].Voltage }}"
    qos: 2
    unit_of_measurement : "V"

sensor sonoff_pow_current 2:
    platform: mqtt
    name: Switch2 Current Amps
    state_topic: "tele/sonoff2/SENSOR"
    value_template: "{{ value_json['ENERGY'].Current }}"
    qos: 2
    unit_of_measurement : "A"


sensor sonoff_pow_current4:
    platform: influxdb
    name: Switch2 Current Amps
    state_topic: "tele/sonoff2/SENSOR"
    value_template: "{{ value_json['ENERGY'].Current }}"
    qos: 2
    unit_of_measurement : "A"
    

# Text to speech



influxdb:
  host: a0d7b954-influxdb
  port: 8086
  database: homeassistant
  username: grafana
  password: 123
  max_retries: 3
  default_measurement: state

tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

this is the code im using in configuration.yaml but im getting a log error and its not working

At the bottom of your configuration.yaml you have:

automation: !include automations.yaml

this means that you need to put all your automations in this file, but without the automation: at the top of your automation. So put this

- alias: Automation Name
  trigger:
  - platform: time
    at: 17:29:00
  action:
  - service: switch.turn_off
    entity_id: switch.sonoff2

into the automations.yaml file and remove it from your configuration.yaml file.

It would be really helpful if you would just post the error from the log here :wink:

And try defining your switches like this :

switch:
  - platform: mqtt
    name: "Sonoff Switch 01"
    command_topic: "cmnd/sonoff1/power"
    state_topic: "stat/sonoff1/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
    
  - platform: mqtt
    name: "Sonoff Switch 02"
    command_topic: "cmnd/sonoff2/power"
    state_topic: "stat/sonoff2/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

i placed the automation in the automation.yaml and defined the switches as above but nothing seems happening, is the entity id correct ?

The entity_id is not correct, name is “Sonoff Switch 02” , not sonoff2. You will have to see what entity_id HA created from the name “Sonoff Switch 02”, or just use the name “sonoff2” instead of “Sonoff Switch 2”

I assume the entity_id will be switch.sonoff_switch_02, but better check under Developer Tools -> States.

Can you please post your errors from the logs here?

2019-11-18 16:18:13 WARNING (Recorder) [homeassistant.components.recorder] Ended unfinished session (id=39 from 2019-11-13 14:15:22.701273)
2019-11-18 16:18:19 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: Invalid time specified: 58800 for dictionary value @ data['trigger'][0]['at']. Got None. (See /config/configuration.yaml, line 105). Please check the docs at https://home-assistant.io/integrations/automation/
2019-11-18 16:18:20 ERROR (MainThread) [homeassistant.config] Invalid config for [sensor.influxdb]: [name] is an invalid option for [sensor.influxdb]. Check: sensor.influxdb->name. (See /config/configuration.yaml, line 80). Please check the docs at https://home-assistant.io/integrations/sensor.influxdb/
2019-11-18 16:22:26 ERROR (MainThread) [homeassistant.components.hassio] Invalid config for [automation]: Invalid time specified: 59040 for dictionary value @ data['trigger'][0]['at']. Got None. (See /config/configuration.yaml, line 105). Please check the docs at https://home-assistant.io/integrations/automation/
Invalid config for [sensor.influxdb]: [name] is an invalid option for [sensor.influxdb]. Check: sensor.influxdb->name. (See ?, line ?). Please check the docs at https://home-assistant.io/integrations/sensor.influxdb/
2019-11-18 16:22:52 ERROR (MainThread) [homeassistant.components.hassio] Invalid config for [automation]: Invalid time specified: 59040 for dictionary value @ data['trigger'][0]['at']. Got None. (See /config/configuration.yaml, line 105). Please check the docs at https://home-assistant.io/integrations/automation/
Invalid config for [sensor.influxdb]: [name] is an invalid option for [sensor.influxdb]. Check: sensor.influxdb->name. (See ?, line ?). Please check the docs at https://home-assistant.io/integrations/sensor.influxdb/

this is what im getting

123

im also getting this

Could you please post your code? The log says that you used an invalid format for the time.

When you are at it, you can also post your config for influxdb, as there is something wrong there as well.

This is the config.yaml


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

    
switch:
  - platform: mqtt
    name: "Sonoff Switch 01"
    command_topic: "cmnd/sonoff1/power"
    state_topic: "stat/sonoff1/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
    
  - platform: mqtt
    name: "Sonoff Switch 02"
    command_topic: "cmnd/sonoff2/power"
    state_topic: "stat/sonoff2/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
    
    
sensor sonoff_pow_watts:
    platform: mqtt
    name: Switch1  Power Usage
    state_topic: "tele/sonoff1/SENSOR"
    value_template: "{{ value_json['ENERGY'].Power }}"
    qos: 2
    unit_of_measurement : "W"

sensor sonoff_pow_volts:
    platform: mqtt
    name: Switch1 Voltage
    state_topic: "tele/sonoff1/SENSOR"
    value_template: "{{ value_json['ENERGY'].Voltage }}"
    qos: 2
    unit_of_measurement : "V"

sensor sonoff_pow_current:
    platform: mqtt
    name: Switch1 Current Amps
    state_topic: "tele/sonoff1/SENSOR"
    value_template: "{{ value_json['ENERGY'].Current }}"
    qos: 2
    unit_of_measurement : "A"
    
    
sensor sonoff_pow_watts 2:
    platform: mqtt
    name: Switch2  Power Usage
    state_topic: "tele/sonoff2/SENSOR"
    value_template: "{{ value_json['ENERGY'].Power }}"
    qos: 2
    unit_of_measurement : "W"

sensor sonoff_pow_volts 2:
    platform: mqtt
    name: Switch2 Voltage
    state_topic: "tele/sonoff2/SENSOR"
    value_template: "{{ value_json['ENERGY'].Voltage }}"
    qos: 2
    unit_of_measurement : "V"

sensor sonoff_pow_current 2:
    platform: mqtt
    name: Switch2 Current Amps
    state_topic: "tele/sonoff2/SENSOR"
    value_template: "{{ value_json['ENERGY'].Current }}"
    qos: 2
    unit_of_measurement : "A"


sensor sonoff_pow_current4:
    platform: influxdb
    name: Switch2 Current Amps
    state_topic: "tele/sonoff2/SENSOR"
    value_template: "{{ value_json['ENERGY'].Current }}"
    qos: 2
    unit_of_measurement : "A"
    

# Text to speech



influxdb:
  host: a0d7b954-influxdb
  port: 8086
  database: homeassistant
  username: grafana
  password: 123
  max_retries: 3
  default_measurement: state

tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

this is the auto.yaml

- alias: Automation Name
  trigger:
  - platform: time
    at: 16:24:00
  action:
  - service: switch.turn_off
    entity_id: switch.sonoff_switch_02

and here are the influxfb databases

The line just below sensor sonoff_pow_current4: is wrong, it should be platform: mqtt and not platform: influxdb, this should solve the influxdb problem.

For the automation, is this the only automation you have? Can you try putting the time in quotes and see if it works?

thanks it worked

@francisp, @Burningstone & @horror

Thanks for the thread, it made my configuration a breeze!!
Just want to thank you guys :+1: