After: ?variable? (yaml)

Hi,
there is way to use a variable after " after: ?
My variable is “vtramonto”

Thanks

condition:
  - condition: time
    before: '06:00:00'
    after: vtramonto   <----**** how is the correct syntax??? ***
mode: single
variables:
  vtramonto: >-
{{(as_timestamp(state_attr("sun.sun","next_setting"))|timestamp_local)[-8:]}}

Hi and welcome to the forum.

Please have a read of point 11 here, then edit your post to correctly format your configuration examples.

You can only use input_datetime helpers in that position.

This example is from the Time condition documents:

condition:
  condition: time
  after: input_datetime.house_silent_hours_start
  before: input_datetime.house_silent_hours_end
1 Like

Thanks Tom.
I tried with this code but it does not work (I need ‘tramonto_cest’):

input_datetime:
  tramonto_cest:
    has_time: true
    has_date: false
    initial: '{{(as_timestamp(state_attr("sun.sun","next_setting"))|timestamp_local)}}'
  tramonto_cest2:
    has_time: true
    has_date: false
    initial: '21:50'

In event editor the string

{{ states("input_datetime.tramonto_cest") }}

returns “unkwnows”.

and the string

{{ states("input_datetime.tramonto_cest2") }}

returns 21:50:00

The first input_datetime doesn’t work because you used a template for initial but if you check the documentation for the initial option you will see it doesn’t support templates.

The second input_datetime works because you used a time string for initial which is the only thing it supports.

You’re making an erroneous assumption that every option supports a template. Only some options support templates and the documentation usually tells you which ones do.

1 Like

Ok Tars, many thanks.
Workaround?
Sorry, I am newbie…
Bye

Workaround for what exactly? If you mean for the Time Condition then consider using a Template Condition. Let me know if you need help composing the template.

1 Like

Taras,
I need to turn light (after open door) if time is between sunset and sunrise.

condition:
    platform: sun
    after: sunset
    before: sunrise

The problem is that “sunset” and “sunrise” are UTC time, not CEST time.
So I found the way to convert them in CEST, using

'{{(as_timestamp(state_attr("sun.sun","next_setting"))|timestamp_local)}}'

but it not works with input_datetime.

So I need to use CEST sunrise and sunset in my condition.
See rows code with “???”

Many Thanks

alias: mc_Ingresso (2)
description: turn on only after sunset and before sunrise
trigger:
  - platform: state
    entity_id: binary_sensor.sonoff_a480011888
    from: 'off'
    to: 'on'
    for: '00:00:00'
condition:
  - condition: time
    before: ??? sunrise-time-cest (not utc)
    after: ??? sunset-time-cest (not utc)
action:
  - service: switch.turn_on
    target:
      entity_id: switch.sonoff_1000ca80a3
mode: single

They are displayed in UTC but converted to local time when used by sun.sun to determine when sunrise and sunset occur in your location. It uses the timezone you set in Configuration > General to determine your local time.

Taras,
are you sure? My location is correct (Italy, CEST) but the value sun.sun are Utc!
(the sunset of today is 20.23)
Many thanks for your help !!!
PS: I will try with sunset & sunrise value.

utc

Me too and my timezone is in North America. Both Sun Trigger and Sun Condition work correctly for my location.

1 Like