Help with automation during the day

Sometimes we get up early in the morning, and is still dark so we switch on some lights
Later when the sun comes up there is a lot of sunlight in the living room so we to forget to turn them off. (not visible) For the time being I don’t have any sensors that could help on this

I am trying to make an automation that will turn the light off

I need some help (in a condition ?) in the following code in order to define that "if the light is on for more than one hour during the day then turn it off. I also don’t know what kind of sensor would help me on this or if I could achieve my goal with the “sun” platform
any ideas please?

###  Floorlight off in the morning ###
- id: Floorlight off in the morning
  alias: Floorlight off in the morning
    - platform: time
      after: '05:50:00'
    - service: switch.turn_on
      entity_id: switch.sonoff_10001bb5c7

For starters, if you intend to have the automation turn off the light, your example is calling the wrong service. :slight_smile:

I would suggest using the sun’s elevation as the trigger.

Sun Elevation Trigger

I’ve taken the example in the documentation and modified it. It triggers when the sun’s elevation is 5 degrees above the horizon. You’ll probably need to experiment with the value to find the correct one for your needs.

- alias: "Light off when sun is up"
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    above: 5.0
    service: switch.turn_off
    entity_id: switch.sonoff_10001bb5c7

I will test it tomorrow. I will check it with a s sonoff switch I am using.
I don’t know if there is a way to check if the

value_template: "{{ state.attributes.elevation }}"
    above: 5.0

is correct although. Right now if I place the code in developer tools - template
I get the following
"“Error rendering template: UndefinedError: ‘state’ is undefined”’
I will let you know tomorrow

That template is valid only when used by the trigger at the time it evaluates the value_template.

I just tested it (checking when the sun’s elevation goes below a given value) and it works.

- alias: "test 55"
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: 29.25
    service: persistent_notification.create
      title: test 55
      message: "Elevation is now {{trigger.to_state.attributes.elevation}}"

Just in order to understand what exactly is your input in the template so as to test the particular code?
Or you test it in the automation.yaml?

The template is telling Home Assistant to look at a particular state attribute (elevation) of a particular entity (sun.sun). Home Assistant evaluates the template in the trigger using the entity specified in the trigger. If you want to test the template in the template tool itself, you need to define the entity. So you’d need something like this:

{{ states.sun.sun.attributes.elevation }}

If someone believes my explanation above isn’t totally accurate, please feel free to correct me. It’s the best way I could think to explain it.

@Makis these would both be good for you to read:
State objects

For a detailed explanation, see tediore’s post above.

Like I stated earlier, that particular template is only valid at the time the automation’s trigger is evaluated. The state in state.attributes.elevation is understood, by Home Assistant, to refer to the entity sun.sun. Therefore, in order to test it I had to create the automation and wait until the sun’s actual elevation decreased below 29.25 degrees.

Here’s an alternative trigger:

    platform: template
    value_template: "{{ state_attr('sun.sun', 'elevation') | float > 5 }}"

This one’s template can be tested in the Template Editor.

my Day is based of my working hours and not based about when the sun goes down I’ve gone with a time based “mode” model inspired by @Vasiley
4 states through the day morgens, tag, abend, nacht and scenes with corresponding names.
so I have a couple of automations like

- service: scene.turn_on
    entity_id: "scene.klo_{{states('sensor.house_mode_state')|default(tag) }}"

Thank you all.

@Tediore @123
everything seems more clear now, although I do have problem to understand the syntax of the templates (needs a lot of study-practice)

2 more questions If I may (not relative with subject)

  1. Is there a way to save in the template editor (dev tools) the above examples you gave me? now it has by default some others

  2. In general is it safe to edit in automation.yaml an automation I wrote with the automation editor?
    The first time I tried to make an automation was with the editor, but I seem to prefer to start from scratch than to start in the editor and after to make modifications in the automation.yaml

very interesting way of coding but I don’t understand right now the “input_boolean.boolean” although I did read about it, the same goes for all the template coding.

  1. The Template Editor does not let you save its contents. Just paste the template examples into a file somewhere so that you can use it as a reference.

  2. I don’t use the Automation Editor. I create them directly in automations.yaml using Visual Studio Code (a text editor designed for programming) and an extension for it called Home Assistant Config Helper. If you are using HASS.IO then there are also plugins available for editing.

As a general note, the Automation Editor currently has certain limitations that do not exist when you edit automations directly with a text-editor (perhaps someday those limitations will be eliminated).

Remember that the sun will have an elevation of 5 at two points during the day: once on the way up and again on the way down. I’d recommend using the sunrise/sunset events with a time offset:

My front porch lights turn on 15 minutes before sunset every night.

      - platform: sun
        event: sunset
        offset: '-00:15:00'

I believe the Taras proffered template is the one the OP went with (so should be marked as solution) and as it would only trigger when going to an elevation greater than 5 degrees, it will only ever trigger once per day.
Altough I agree if you are using such in say a binary sensor you should maybe nail this down by adding a sun rising or sun not rising. (for dusk/dawn distinction).
Elevation is generally a much better method of achieving a consistent light level as time will cause a much great variation between winter and summer

I saw this in another thread, and though I’ve not used it, could be helpful if used in conjuction with a time before 12:00:00 condition.

Should give a good idea of how bright it is outside. Personally and lately I’ve found myself changing my evening lighting routines more around how bright it is outside vs. what time the sun goes down +/- some offset. That’s great if sunset is at 6pm, but if it’s storming/cloudy - I want the inside light on EARLIER than if it was clear and total sun. I have a front porch Aeon sensor - so I’ve been using that (for both motion for front porch lights, as well as now for illuminance) - but this component could help ‘guess’ the brightness if you don’t have an outdoor sensor:

Anyhow, figured I’d throw it out there.

The code provided by Taras works fine and it is what I asked for.
I think I will have to buy a lux sensor and integrate it to the automation later, for the cloudy days but I am not sure yet if I need it. I will have to see the conditions live in my home and decide.
Thanks all a lot for you help and the share of knowledge