Calculating Time

Hi!
I have an automation that could start at any time, and uses Ashley’s Light Fader to fade a specified light. However, whenever it is triggered, it should ensure that the light always turns off at a specific time, so to enter the fade duration into Ashley’s Light Fader, it needs to calculate the time between now and then. How could I do this?

1 Like

Oh, hi, @ThatBlockyPenguin—I’d be happy to help you with this!

Just to ask, are you wanting to fade the light from X% brightness to Y% brightness—and that you also separately want that light to turn off at a specific time?

Or are you wanting to fade the light from X% brightness to 0% brightness, and you also want the 0% brightness part to coincide with a specific time?

Hi @handcoding, amazing, thanks!
To answer your question: yes, the light is fading down to 0%, which I would like to happen at X time.

@ThatBlockyPenguin Just as a starting point, if I might ask, how familiar are you with Jinja/YAML coding (if at all)?

(And if perhaps it might be “not at all,” that’s okay too—I just want to try to gauge how best to phrase my ideas and replies.)

Yeah it would be “not at all” I’m afraid, my only experience with it is with HA, which I only started using as of a few months ago.

Ah, no worries—that’s okay!

And just as one other question—do you happen to have experience with programming in another language, such as maybe JavaScript or the like?

(And like before, it’s okay if you haven’t—I’m just trying to gauge whether you might be familiar with some general programming concepts like variables and loops and such things.)

Yes to this one - I mainly know Java, C#, and JavaScript/TypeScript

1 Like

Post your automation that uses Ashley’s script please.

Sorry, slight miscommunication there - I don’t have anything working yet - I went to create a new automation that acheives this goal, and was unable to proceed because I don’t know how to calculate differences in time in HA.

Well, sorry but we are going to need to know how you plan on triggering this and what time you expect the light to turn off in order to assist. So I recommend creating your automation and then I’ll help you with the time calculation when you cross that bridge.

I plan on triggering it with the press of a Philips Hue smart button. I can get this far in creating an automation, but that’s it, seeing as the next step is to calculate the time until 11:30PM and input this time into Ashley’s Light Fader.

Thus, if I were to create an automation, it would be a trigger with no actions.

The intended use case is to trigger a sunset effect when going to bed, which we know will finish at a predictable time.

{{ (today_at('23:30') - now()).total_seconds() }}

One action. The action would call the Fader script, specifying which light to fade and the fading duration calculated using petro’s template (shown above).

Like so?

alias: Sunset
description: ""
trigger:
  - device_id: ce3edfe3e9feca034e899b24249fd005
    domain: hue
    platform: device
    type: initial_press
    subtype: 1
    unique_id: ca963d11-aebc-4ca9-8bfa-dc9e5c3385c9
condition: []
action:
  - service: script.ashleys_light_fader
    data:
      lampBrightnessScale: zeroToOneHundred
      easingTypeInput: auto
      endBrightnessPercent: 0
      autoCancelThreshold: 10
      endBrightnessEntityScale: zeroToOneHundred
      shouldResetTheStopEntityToOffAtStart: false
      minimumStepDelayInMilliseconds: 100
      isDebugMode: false
      light: light.aaron_s_light
      transitionTime:
        hours: 0
        minutes: 0
        seconds: {{ (today_at('23:30') - now()).total_seconds() }}
mode: single

I agree - now that I have petro’s template, and can actually add a functioning action.

Wait, nevermind - trying to save that changes the code to this:

[...]
      transitionTime:
        hours: 0
        minutes: 0
        seconds:
          "[object Object]": null
[...]

You need quotes around your template…

seconds: "{{ (today_at('23:30') - now()).total_seconds() }}"

Oh brilliant, that works perfectly!
Thanks very much all

1 Like