Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I start a timer from an attribute? #71

Closed
el97 opened this issue Jan 12, 2023 · 3 comments
Closed

How do I start a timer from an attribute? #71

el97 opened this issue Jan 12, 2023 · 3 comments
Assignees

Comments

@el97
Copy link

el97 commented Jan 12, 2023

Hi! I'm trying to make a timer start based on when an attribute changes. More specific I want a timer to start when the sun goes down and vise versa. With the current code I can only see if the sun is above or below the horizon. What am I doing wrong?
This is what I have tried with and below is what comes out with it

type: custom:timer-bar-card
name: Sunrise
icon: mdi:weather-sunset-up
entity: sun.sun
start_time: 
  attribute: next_setting
end_time:
  attribute: next_rising
debug: true
guess_mode: true
bar_width: 54%
text_width: null
State: below_horizon (state mode = idle)
Mode: idle (auto mode = N/A, used)
Duration: -26751.153 second
Time remaining: 18882.293
Counter: 18882.294
Did you set active_state? Attr: {"next_dawn":"2023-01-12T06:38:20.467703+00:00","next_dusk":"2023-01-12T15:36:38.479040+00:00","next_midnight":"2023-01-12T23:07:40+00:00","next_noon":"2023-01-12T11:07:05+00:00","next_rising":"2023-01-12T07:24:32.894894+00:00","next_setting":"2023-01-12T14:50:24.047322+00:00","elevation":-43.98,"azimuth":58.88,"rising":true,"friendly_name":"Sun"}
@rianadon
Copy link
Owner

rianadon commented Jan 12, 2023

Taking a look at the debug output, your duration is negative (which means there's no progress bar shown) because the start time is Jan 12 at 14:50 whereas the end time is the same day at 7:24.

If you're intending to see a progress bar in the time after sunset but before sunrise (i.e a timer that runs while the sun is down), you'll want the end time to be the next sunrise (which you have), but the start time to be the time the sun set the previous day.

There's no such previous_setting attribute in entity, so you could approximate that two ways:

  1. Fetch the value of next_setting from one day ago using the Home Assistant recorder history
  2. Approximate the previous sunset as the same time as today's sunset, but one day ago.

The first option sounds pretty difficult to me, but for the second you could create a template to calculate the previous sunset, like {{ (as_datetime(state_attr('sun.sun', 'next_setting')) + timedelta(days=-1)).isoformat() }}.


However, it is also my understanding that the sun entity only has two states: above horizon and below horizon. If you turn off guess mode and provide only an end time, the timer will use the time the sensor last changed its state as the start time, which will be when the sun last either set or rose.

So you could also use something like this as your configuration (no template needed):

type: custom:timer-bar-card
name: Sun
entities:
  - entity: sun.sun
    name: Sunrise
    icon: mdi:weather-sunset-up
    active_state: below_horizon
    end_time:
      attribute: next_rising
    translations:
      above_horizon: Not yet ☀️
  - entity: sun.sun
    name: Sunset
    icon: mdi:weather-sunset-down
    active_state: above_horizon
    end_time:
      attribute: next_setting
    translations:
      below_horizon: Not yet 🌙
text_width: 5em
bar_radius: 4px

I really love this example of the card since it shows off a lot of functionality. Thank you so much for the idea. I'll probably put this configuration on the README sometime, which is why I wrote such a long example.

image

I just changed my location before the screenshot, which messed with the sun's last changed date. It stays dark for more than 6 hours here 😄

@el97
Copy link
Author

el97 commented Jan 12, 2023

Thank you so much for the help! This is exactly what I wanted. Can't wait to see it in the README so other people can use it 👍

@el97 el97 closed this as completed Jan 12, 2023
@el97
Copy link
Author

el97 commented Jan 12, 2023

Better to keep the issue open as a reminder

@el97 el97 reopened this Jan 12, 2023
@rianadon rianadon self-assigned this Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants