Hours of Daylight

But honestly I’m not sure it still works – the WU API that is.

Thanks I was thinking the same too.

Phil question about adding your sun2 to HACS. I tried to add it as a integration but it would not let me add it. Says “Could not add ‘pnbruckner/ha-sun2’ with type ‘integration’ at this time”. Am I adding it wrong? I saw on the repository about adding to HACS but it was not 100% clear on how.


Added as a custom repository

Yeah that is what I tired to do but it would not let me add it as an integration. I may need to reboot been acting wonky today.

try adding it by entering


That is use the full url…
2 Likes

Since you posted that I see what I was doing wrong. I had the HTTPS:// part still on it. Duh. Thanks

This is incredibly useful. Thanks !

1 Like

Is this still available via HACS? I can’t seem to find it for the life of me. I tried all sorts of combinations.

yes it is but you will need to add it as a custom repo

1 Like

Thank you @DavidFW1960. I found it now. I’m a HACS n00b :slight_smile:

1 Like

Hi all, I came here looking for just Length of Day calculations and followed this (and another) thread with great interest (and learned a lot - yay HA!). What @pnbruckner has done here is wonderful! However, I was just looking for a simple Length of Day since here in AK it varies from ~20hrs of daylight to 20hrs of darkness summer to winter. Here is what I did for that single parameter if anybody else is interested. It seems to be working well and should be pretty exact. And it’s extremely simple, as long as timeanddate.com doesn’t change their web site very often. :wink: I set the scan_interval (thanks @RobC) to hourly so as not to poll unnecessarily, but I may play with increasing that further. I really only need it once a day, but I’m not sure how to (or if I can) set the start time of that period (I assume it is whenever HA is restarted).

 # Day Length (scrape)
sensor:
  - platform: scrape
    scan_interval: 3600
    resource: https://www.timeanddate.com/astronomy/usa/anchorage 
    name: length_of_day_st
    select: ".table > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(2)"

Find your location/city on the website, find it in the URL, and change the code above and it should work for you. I left the format as it was and it looks like this (even showing the change from yesterday in parentheses):

Cheers.

image

You can even use a specific location so you should be able to check it using your HA location.

https://www.timeanddate.com/astronomy/@52.0,5.0

I wonder if you can parameterize (templatize) the “resource” parameter to pick up your lat/long from your config file somehow.

I’m not sure, but you should be able to hard code it in the url or (if you value privacy) use the secrets.yaml file.

I did some experimenting, just for fun.

I entered this working snippet in the template editor:

{% set lat = state_attr('zone.home', 'latitude') | string %}
{% set lon = state_attr('zone.home', 'longitude') | string %}
{% set res = "https://www.timeanddate.com/astronomy/@" + lat + "," + lon %}
{{ res }}

Then, I added a sensor like this:

# Day Length (scrape)
sensor:
  - platform: scrape
    scan_interval: 3600
    # resource: https://www.timeanddate.com/astronomy/usa/anchorage
    # resource: https://www.timeanddate.com/astronomy/@51.92,4.57
    resource: > 
      {% set lat = state_attr('zone.home', 'latitude') | string %}
      {% set lon = state_attr('zone.home', 'longitude') | string %}
      {% set res = "https://www.timeanddate.com/astronomy/@" + lat + "," + lon %}
      {{ res }}
    name: length_of_day_st
    select: ".table > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(2)"

The configuration check succeeds, but when I restart HA I get an error unfortunately:

Logger: homeassistant.components.rest.data
Source: components/rest/data.py:69 
Integration: rest (documentation, issues) 
First occurred: 17:13:09 (2 occurrences) 
Last logged: 17:13:12

Error fetching data: {% set lat = state_attr('zone.home', 'latitude') | string %} {% set lon = state_attr('zone.home', 'longitude') | string %} {% set res = "https://www.timeanddate.com/astronomy/@" + lat + "," + lon %} https://www.timeanddate.com/astronomy/@ failed with Request URL missing either an 'http://' or 'https://' protocol.

So it looks like the scrape resource isn’t scriptable/templatable.

2 Likes

It looks as if the zone.home lat/long values aren’t defined at the time this sensor is defined (i.e., rest before sensor). I wonder if there is a published order in which things are processed in HA at startup. It would be nice to be able to have some level of control over order, either through the list in config, or some other way. That might provide limited capability to work around these chicken & egg issues, and would definitely provide a LOT of capability to really frack things up! :wink: The error seems off though, since it clearly has the https://, it’s just truncated after the @. Thanks for trying tho. Seems you nailed it otherwise. Cheers.

1 Like

For future reference. I settled with a !secret for now:

sensor:

  # Day Length (scrape)
  # https://community.home-assistant.io/t/hours-of-daylight/59422/153
  - platform: scrape
    name: Daylight Hours
    scan_interval: 3600
    # resource: https://www.timeanddate.com/astronomy/usa/anchorage
    # resource: https://www.timeanddate.com/astronomy/@51.92,4.57
    resource: !secret timeanddate_astronomy_resource
    select: ".table > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(2)"
1 Like

Is there a way to parse the output and record it as timestamp on hassio?

I think I found the solution:

- platform: scrape
  name: Daylight Duration
  scan_interval: 3600
  unit_of_measurement: minutes
  resource: !secret timeanddate_astronomy_resource
  select: ".table > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(2)"
  value_template: >
    {% set h = value.split(" ")[0] %}
    {% set m = value.split(" ")[2] %}
    {% set dtime= (h | int) * 60 + (m | int) %}
    {{dtime}}