Sunrise/sunset automation with sonoff--tasmota mqtt

I’ve been searching the threads to find an answer, but I can’t seem to find my mistake. I’m using a Sonoff basic with Tasmota installed and integrated via mqtt. I can see the sonoff via the mosquitto broker add-on log. I also can see that the sonoff has signed on through the sonoff console. I have a card on the home asssistant front end where I can toggle the sonoff on and off.

Here’s my problem: I’m trying to set up an automation to turn some lights on at sunset and off at sunrise, but I’m making a mistake somewhere, but I can’t seem to find it. When I click on Check Config, I get no errors. Here is my automation:
sonoff is named ‘lights’

automation:

  - alias: 'Sconce Lights on 45 minutes before sunset'
    initial_state: 'off'
    trigger:
      - platform: sun
        event: sunset
        offset: '-0:45:00'
    action:
      service: switch.turn_on
      entity_id: switch.lights
      
  - alias: 'Sconce Lights off 45 minutes after sunrise'
    initial_state: 'on'
    trigger:
      - platform: sun
        event: sunrise
        offset: '+00:45:00'
    action:
      service: switch.turn_off
      entity_id: switch.lights

Any advice would be much appreciated. --Thanks

on a different take, i set my tasmotized sonoff switch based on sunrise/sunset with some minutes offset directly on my device.

Yes, there is an offset in there, but I don’t “think” that’s the issue. Are you giving me a clue???
My offset is based on this example: https://www.home-assistant.io/docs/automation/examples/

i meant i configured my switch to turn on/off based on sunrise/sunset using the built in timer in tasmota

1 Like

I for some reason (it’s been more than a year since I did this one) had to use a template on the elevation, rather than an offset:

- alias: Front Lights
  initial_state: true
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: -1.0
  action:
  - service: light.turn_on
    entity_id: light.front_garden_lights
  - delay: 01:30:00
  - service: light.turn_off
    entity_id: light.front_garden_lights

Wish I could remember why…

My automation for sunrise / Sunset:

- id: '1577888355160'
  alias: switch Downstairs light on at sundown
  description: ''
  trigger:
  - event: sunset
    platform: sun
  condition: []
  action:
  - device_id: 38c4932f91a14099b81b6f470b85b64d
    domain: switch
    entity_id: switch.tasmota1
    type: turn_on

- id: '1577977311924'
  alias: Switch off upstairs lights after sunrise
  description: ''
  trigger:
  - event: sunrise
    platform: sun
  condition: []
  action:
  - device_id: d878a76849ff4eb2b1c0d6df80f0358b
    domain: switch
    entity_id: switch.upstairs_living_room_light
    type: turn_off
  - device_id: 6c5585e7cddc4ad98f2ffaab8bade5a8
    domain: switch
    entity_id: switch.tasmota1
    type: turn_off

I believe this should be set to “true”. That way the automation it set to run upon a HA restart.

Hope it’s of help.

Sorry I’ve been silent. I appreciate all the feedback. I’ve been trying different scenarios, but still no cigar…
My latest attempt looks like this. I’ve only included the first automation as the other two have an identical format.
I’m really struggling to understand the issue. The automation does not fire. I don’t see any errors in the log, nor do I see any record of any attempt in the logs. I’m quite befuddled with this.

I added an automation id, as recommended in another forum post, but no change.
I changed the service being called to Mqtt.publish, in case I was trying to use the wrong service in my previous attempts, no change.
Per recommendation from Akriss, I changed the initial_state to ‘true’, then “true”, then true, but still no change.
I also added data: thinking that maybe I was missing a simple thing—this was recommended in a similar post elsewhere. Still no change.

Any inspiration??

  - id: 123456
    alias: 'Sconce Lights on 45 minutes before sunset'
    initial_state: 'true'
    trigger:
      - platform: sun
        event: sunset
    action:
      data:
      service: mqtt.publish
      topic: 'cmnd/sonoff/POWER'
      payload: OFF
      entity_id: switch.lights

@Enginerd - the issue is you are publishing to the wrong topic. State is upper case, command is lower case. Also, wouldn’t you be better off defining the light as a light first, then turn it on with the automation? See example below:

light:
  - platform: mqtt
    name: "Front Garden Lights"
    command_topic: "cmnd/sonoff2/power"
    state_topic: "stat/sonoff2/POWER"
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

My automation is a bit different, but yours would work similarly:

- alias: Front Lights
  initial_state: true
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: -1.0
  action:
  - service: light.turn_on
    entity_id: light.front_garden_lights
  - delay: 01:30:00
  - service: light.turn_off
    entity_id: light.front_garden_lights

I finally figured it out. Stupid mistake, but I want to post so any other newbies like me can learn from my mistake. When entering the device_id, I did not have quotes around the name of the device I’m using to call the service. Pretty simple, but it makes the difference between something “working” or not.

Thanks everyone for your comments and suggestions. Several good examples to use. Thanks again.