Can't figure out turn_on_at

I am trying to create an automation with a computed offset from the trigger, the trigger being sunset and the offset changes by month of the year. After failing to get it working I simplified it to the configuration below in attempt to understand it but I can’t even get this to work. The config below produces the error “extra keys not allowed @ data[‘turn_on_at’]” which doesn’t help me at all.

alias: Test automation
description: ""
trigger:
  - platform: sun
    event: sunset
action:
  - variables:
      month: "{{ now().month / 6 | abs }}"
  - service: light.turn_on
    entity_id: switch.sitting_room_table_lamp_local_switch
    data:
      turn_on_at: "{{ (state_attr('sun.sun', 'next_setting') | as_timestamp) + (30 * 60) }}"
mode: single

If someone could explain what is wrong with the above it would help me greatly.

Thanks in anticipation.

Been using ChatGPT ?

2 Likes

Yes as it happens, apart from that do you know the problem?

Do a search on Google for

"turn_on_at" Home Assistant

0 results. why should that be ?

Explain exactly what you want to do and we can help you write the code.

1 Like

Also the fact that it instantiates the variable month then does nothing with it and has entity_id not under either data or target.

To help you in the future, “extra keys are not allowed” means you put something in there that isn’t in the spec. It then tells you which extra thing you added (“@ data[‘turn_on_at’]”).

There is no such thing as turn_on_at that can be used in the light.turn_on service call.

As for the problem you are working on now, if you’re trying to account for the varying lengths of dusk/dawn throughout the year, the Home Assistant docs (see the note in the Sun Trigger section on this page) recommend using sun elevation triggers, not sunrise/sunset with offsets for reasons it seems like you’ve already figured out.

And runs light.turn_on on a switch :roll_eyes:

@dh-1 : key learning from this is that ChatGPT just doesn’t work for HA. Please don’t use it then come here asking for help fixing it, as it often contains subtle errors that take more time and effort to track down than solving the problem from scratch.

If you are going to use it, please at least let us know up front so we can ignore it.

The sun trigger already provides an optional offset for adding the 30 minutes that you are trying to: see here.

alias: Test automation
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "00:30:00"
action:
  - service: switch.turn_on
    target:
      entity_id: switch.sitting_room_table_lamp_local_switch
mode: single

I’d still use elevation instead, though.

1 Like

The problem is ChatGPT; it’s known to produce invalid examples.

Summary of what others have already identified:

  1. turn_on_at doesn’t exist. This alone invalidates the entire approach of triggering at sunset and then attempting to offset the turn on time.

  2. Creates a script variable called month but never uses it. Needlessly uses the abs filter for an integer value that is never negative.

  3. Uses the wrong service call (light.turn_on) to turn on a switch entity. The service call also uses outdated syntax.

I agree with others that you should consider using a Sun Elevation Trigger.

1 Like

OK, thanks everyone, I’ve learned my lesson.

The configuration I posted was not the original (I know I should have been more careful), the variable month had been used originally as a modifer in generating an offset from the trigger (sunset).
light.turn_on was from GPT, again I didn’t look carefully enough.

I used GPT later in the diagnostic process, turn_on_at came from a google search although I can’t find again.

The information: “extra keys are not allowed” means you put something in there that isn’t in the spec, was the most helpful comment, thankyou for that. Oh and I will look at Sun Elevation Trigger as an alternative to what I was trying to do.

I suspect I’m just not up to configuring Home Assistant, a lesson I’m beginning to realise too.

Eh, using ChatGPT for HA is almost always a bust… BUT, that doesn’t mean you’re not up to making HA what you want and need it to be. A lot of us have been using HA for YEARS and, at least for me, I still learn new things about it almost daily. There’s tons of resources available and lots of experts to lean on.

Basically, don’t throw the towel in over one failed automation. Take what you’ve learned and keep experimenting. Oh, and make sure you keep LOTS of backups to rollback to. :smiley: (I honestly cannot tell you how many times I’ve had to restore my config yaml files because of stupid mistakes I made lol).

2 Likes

Don’t give up. I have been using HA for five years, and I’m still learning. I have an immense bookmark list from this forum with items that might come in useful one day.

3 Likes

Don’t put yourself down. It’s a steep learning curve, but once you get your head around the concepts and actually understand what you’re doing, it’s not that hard.

1 Like