Help with template in automation

Hi
I have the following automation. Until yesterday it was working fine, but I had problem because I wanted to run only one time per day. Mainly if the lux sensor is below 75 lux for 3 min turn on the lights.
Sometime later although I need to turnoff these lights, but they turned on after 3 min :slight_smile:

So, I added the following condition template in order to run only once. (I found the formula in the forum)
However today I am in dark :frowning:

Lux sensor showing 50 but my lights are off.

Can someone help?

##  Floorlight on Low Light ###
- id: Floorlight on Low Light
  alias: Floorlight on Low Light
  trigger:
    platform: numeric_state
    entity_id: sensor.motion_sensor
    below: 75
    for:
      minutes: 3
  condition:
    condition: template
    value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.floorlight_on_low_light.attributes.last_triggered | default(0)) | int > 80000)}}'
  action:
    - service: switch.turn_on
      entity_id: switch.sonoff_10001bb5c7
    - service: light.turn_on
      entity_id: light.77626641ecfabc976395

How about using a couple of time conditions to only allow the light to be automatically turned on early in the morning or evening?

Or even sunrise/set conditions (with offsets). So the light can only be automatically turned on and hour or so before sunrise or after sunset?

I have already a sunset automation but sometimes when it is cloudy there is a need to turn on lights earlier. This is why I would like to integrate the lux sensor.
Could this be done with input boolean? I haven’t used it before and really can’t understand the concept

That’s ok. You are still triggering off the Lux level in this automation. Just set the before and after sunset offsets as wide as you want to allow the light to be tuned on at any time it might be cloudy. You are using this as a condition. Not a trigger.

That way when you turn the light off late at night it wont turn on again because it is outside the window of time allowed in the conditions. But it will still turn on when it gets dark near sunset or sunrise.

E.g.

Leave the trigger the same (Lux level).

But add conditions that only allows it to do the actions from an hour before sunrise to an hour after sunrise, or from an hour before sunset to an hour after sunset.

It makes sense. Sometimes in the winter and cloudy though it happens even in the noon the need to turn on the lights. Ideally I would like the particular automation to be independent, and triggered only by the lux sensor.

*Sunset in the winter at 17:00
In a cloudy day we turn on the lights at 14:00

Do you want the lux to work, except for when you specifically don’t? An input_boolean in a condition would work for this.

      - condition: state
        entity_id: input_boolean.enable_lux
        state: 'on'

Thanks but if you may can you explain how to setup this? Never used input boolean before.
I can not understand for example why it will turn on and when it will turn off (I mean the state of the input boolean)

Just add it to the configuration.yaml and reboot. It will look like a switch, on / off.

# Example configuration.yaml entry
input_boolean:
  notify_home:
    name: Notify when someone arrives home
    icon: mdi:car

@popboxgun

Hi
I am trying to add the input boolean

my automation now is like this

##  Floorlight on Low Light ###
- id: Floorlight on Low Light
  alias: Floorlight on Low Light
  trigger:
    platform: numeric_state
    entity_id: sensor.motion_sensor
    below: 75
    for:
      minutes: 3
  condition:
    condition: and
    conditions:
      - condition: state
	    entity_id: input_boolean.enable_lux
          state: 'off'
	  - condition: state
	    entity_id: sensor.someone_home
		  state: true	  
  action:
    - service: switch.turn_on
      entity_id: switch.sonoff_10001bb5c7
    - service: light.turn_on
      entity_id: light.77626641ecfabc976395
	- service: input_boolean.turn_on
	  entity_id: input_boolean.enable_lux

My problem is that i get a message which I don’t know what to do

end of the stream or a document separator is expected at line 40, column 6:
    	    entity_id: input_boolean.enable_lux

it has to do with the condition,
Can you figure out what’s wrong?

Also at the and of the automation I added a line to turn on the input boolean.
Will that be ok?

state should be lined up.

- condition: state
  entity_id: input_boolean.enable_lux
  state: 'off'
- condition: state
  entity_id: sensor.someone_home
  state: true	 

Yup, you can turn it on in an automation if it gives the desired result.

I tried that but I got the same error.
I also tried the following and again same error.

##  Floorlight on Low Light ###
- id: Floorlight on Low Light
  alias: Floorlight on Low Light
  trigger:
    platform: numeric_state
    entity_id: sensor.motion_sensor
    below: 75
    for:
      minutes: 3
  condition:
      - condition: state
	    entity_id: input_boolean.enable_lux
        state: 'off'
	  - condition: state
	    entity_id: sensor.someone_home
		state: true	  
  action:
    - service: switch.turn_on
      entity_id: switch.sonoff_10001bb5c7
    - service: light.turn_on
      entity_id: light.77626641ecfabc976395
	- service: input_boolean.turn_on
	  entity_id: input_boolean.enable_lux

this is the input boolean state
Annotation 2020-04-30 221156

I’m getting random tabs in your code when I copy/paste what you have, try using vscode as your editor or use spacebar.

Give this a try.

##  Floorlight on Low Light ###
- id: Floorlight on Low Light
  alias: Floorlight on Low Light
  trigger:
    platform: numeric_state
    entity_id: sensor.motion_sensor
    below: 75
    for:
      minutes: 3
  condition:
    - condition: state
      entity_id: input_boolean.enable_lux
      state: 'off'
    - condition: state
      entity_id: sensor.someone_home
      state: 'true'
  action:
    - service: switch.turn_on
      entity_id: switch.sonoff_10001bb5c7
    - service: light.turn_on
      entity_id: light.77626641ecfabc976395
    - service: input_boolean.turn_on
      entity_id: input_boolean.enable_lux

Thanks. I was going mad with this. I have notepad++ but I will try vscode soon.
Now I am about to test the automation

notepad++ was doing the same for me.

Although now I can save the automation, when I reload them I get an error in notifications

Invalid config
The following integrations and platforms could not be set up:

automation
Please check your config.

When I delete it all is ok.
Do you have any clue? it is driving me mad

Start commenting out sections with pound like below to see which line/lines are causing the issue. Everything checks out for me.

#    - condition: state
#      entity_id: input_boolean.enable_lux
#      state: 'off'

Here two simple things you can do in Notepad++ to make your life easier.

Select this menu option:
Settings > Preferences > Language

In the Tab Settings section, select yaml, change Tab Size from 4 to 2, press the Enter key then click Close.

Now Notepad++ knows that for any file name with a .yaml or .yml extension, when you press the TAB key it will enter two space characters instead of an actual tab character.

Enable this option and it will display space characters as a tiny dot (real tab characters will be displayed as a line with an arrowhead, so you can easily see the difference).
View > Show Symbol > Show Whitespace and TAB

NOTE
If you are editing a file whose extension is not .yaml or .yml but you want Notepad++ to treat it like a YAML file, simply select Language > YAML.

At the very least, enable this option in Notepad++ so you can see where there are space and tab characters.
View > Show Symbol > Show Whitespace and TAB

A space character will appear as a tiny dot whereas a tab character will appear as a short line with an arrowhead.

thanks. I will do that. (apparently did not knew these potentials)

@popboxgun
the problem now is in this

    - condition: state
      entity_id: sensor.someone_home
      state: true

if i remove these lines it passes the reload automation

this is the sensor

- platform: template
  sensors:
    someone_home:
      friendly_name: Someone Home
      icon_template: >-
        {% if is_state('binary_sensor.someone_home','on') %}
          mdi:home-account
        {% else %}
          mdi:home-outline
        {% endif %}
      value_template: "{{ is_state('person.makis','home') or  is_state('person.kiriaki','home') or  is_state('person.margarita','home')}}"

this is the state
Annotation 2020-04-30 233023

Try

state: 'true'