ianByrne / HASS-ukho_tides

Home Assistant integration for tide information
MIT License
27 stars 5 forks source link

Not an Issue: questions ;) #3

Closed chsims1 closed 3 years ago

chsims1 commented 3 years ago

Thank you very much for the great integration. I'm a HA novice, and finding it challenging to get at details, since I think the documentation is very much designed as a reference to those who are already familar with it.

I an add the simple sensor to a card fine with station id/name and state of the tide (rising/falling), but how do I access attributes such as tide times & heights? I did skim through the files very quickly but attribute names didn't spring out at me.

Also, what are high & low tide offsets?

chsims1 commented 3 years ago

I can now see 'attribution', 'predictions', 'next_high_tide_in', 'next_high_tide_height' etc. for the entity in the HA UI in 'Developer Tools/STATES', but can't see how to work them in to my sensor.yaml, or editing the code from the UI of an 'entities' card Also, is high tide time supported, or is it a case of calculating it from 'next_high_tide_in'?

Sorry for what I'm sure are very basic questions more concerned with HA syntax, than your integration in particular.

ianByrne commented 3 years ago

Hello, and thanks for your questions!

The offsets are an amount in minutes to offset the high/low tide times. The use case would be if you didn't live particularly close to a station, you could tweak the sensor to say something like "my local tide is generally at its highest 10mins before station X predicts it".

As you've found, the 'attributes' of a sensor are the extra properties that are being read from the sensor. In this case, these include things like "Next high/low tide in" and "Next high/low tide height". If you wish to display one of these attributes in your dashboard, you can use the "Entity" card which lets you drill down into these attributes:

image

image

For more advanced things such as templating, you can get the attributes using the state_attr function, eg:

state_attr('sensor.hammersmith_bridge_tide', 'next_high_tide_in')

There isn't currently a good way to extract the timestamp of the next high/low tide. You could indeed try to calculate it from the next_high_tide_in value, but it would not be very accurate. An example of how you could do that with templating would be the following:

sensor:
  - platform: template
    sensors:
      next_high_tide_time:
        friendly_name: "Next High Tide Time"
        value_template: >
          {% set next_high_tide = {
            "hours": state_attr("sensor.hammersmith_bridge_tide", "next_high_tide_in").split(" ")[0][:-1] | int,
            "minutes": state_attr("sensor.hammersmith_bridge_tide", "next_high_tide_in").split(" ")[1][:-1] | int
          } %}
          {{ now() + timedelta(hours=next_high_tide.hours, minutes=next_high_tide.minutes) }}
        icon_template: mdi:calendar-clock

But yeah, I would not recommend this approach - it's quite hacky and as mentioned, would not be very accurate due to the way HASS refreshes the sensor data, etc. We should be able to add the timestamps as additional attributes easy enough - I can see if I get time to take a crack at it in the next few days

chsims1 commented 3 years ago

Thanks for the very clear explanations. I didn't see the attribute option on the entity card: that might have been me using an entities card (can't check at the moment). The state.attrreferencing seems ok, but wouldn't have thought it up myself without a clear example. Thanks again for your help.

I do think that actual tide times would definitely be a useful thing to add if and when you have time.

Cheers!

Ian

On Sun, 22 Aug 2021, 18:14 Ian Byrne, @.***> wrote:

Hello, and thanks for your questions!

The offsets are an amount in minutes to offset the high/low tide times. The use case would be if you didn't live particularly close to a station, you could tweak the sensor to say something like "my local tide is generally at its highest 10mins before station X predicts it".

As you've found, the 'attributes' of a sensor are the extra properties that are being read from the sensor. In this case, these include things like "Next high/low tide in" and "Next high/low tide height". If you wish to display one of these attributes in your dashboard, you can use the "Entity" card which lets you drill down into these attributes:

[image: image] https://user-images.githubusercontent.com/1918098/130362161-f76cad5a-d630-457a-918c-2669efd507ce.png

[image: image] https://user-images.githubusercontent.com/1918098/130362227-6c8e873b-3535-4028-bdd9-219a556d7a96.png

For more advanced things such as templating https://www.home-assistant.io/docs/configuration/templating/, you can get the attributes using the states function, eg:

states('sensor.hammersmith_bridge_tide', 'next_high_tide_in')

There isn't currently a good way to extract the timestamp of the next high/low tide. You could indeed try to calculate it from the next_high_tide_in value, but it would not be very accurate. An example of how you could do that with templating would be the following:

sensor:

  • platform: template sensors: next_high_tide_time: friendly_name: "Next High Tide Time" value_template: > {% set next_high_tide = { "hours": state_attr("sensor.hammersmith_bridge_tide", "next_high_tide_in").split(" ")[0][:-1] | int, "minutes": state_attr("sensor.hammersmith_bridge_tide", "next_high_tide_in").split(" ")[1][:-1] | int } %} {{ now() + timedelta(hours=next_high_tide.hours, minutes=next_high_tide.minutes) }} icon_template: mdi:calendar-clock

But yeah, I would not recommend this approach - it's quite hacky and as mentioned, would not be very accurate due to the way HASS refreshes the sensor data, etc. We should be able to add the timestamps as additional attributes easy enough - I can see if I get time to take a crack at it in the next few days

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ianByrne/HASS-ukho_tides/issues/3#issuecomment-903301034, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQWUQ5HDJCRXMGAOURABSTT6EWA7ANCNFSM5CRWPEPQ .

ianByrne commented 3 years ago

I've now added next high/low tide attributes:

image

You should see an update available when you check HACS.

Thanks for the suggestion, do please let me know if you have any others!

chsims1 commented 3 years ago

Just got round to testing. Seems there is a DST + extra hour problem here. My "next high tide in" is correct with local time (UK), but "next high tide at" is two hours early (and indeed is showing an earlier time than 'now').

To be clear: it is now 22:25 (BST), tonight's high tide is 22:49 (BST), 'next high tide in' says in 24 minutes (correct), but 'next high tide at' says 20:49

BIG EDIT: ahh, I formatted the value from 'next high tide at' to be just hours and minutes with:

value_template: "{{as_timestamp(state_attr('sensor.spurn_head_tide', 'next_high_tide_at')) | timestamp_custom('%H:%M')}}"

Without the formatting, I get : '2021-08-29 21:49:00+01:00', which is correct. How on earth do I format this correctly for the result of '22:49'? I hate time/date/timezones formatting in Python; it seems to be unnecessarily complicated.

ianByrne commented 3 years ago

Hmm this sounds like a right old puzzler. What timezone is the machine that your HASS is running on? For me, my home server is on BST:

image

When I go to Configuration --> Entities and expand the Attributes tab, the times are correct:

image

I copied your value template and it is also returning the correct time for me:

image

(Side note though, when querying them via the Dev Tools --> States page, the timestamps seem to be returned in UTC):

image

Can you please compare on your end? Perhaps the system time of your HASS server vs your client machine is causing some conflicts. Can you also please confirm that you have the latest version of the integration installed? I had actually released two version in quick succession, with the latter one addressing a timezone issue. It's a long shot, but perhaps you somehow managed to nab the earlier version:

image

chsims1 commented 3 years ago

OK, silly mistakes at my end ... system timezone & HA timezone were set to UTC. Changed to 'Europe/London'

However, I'm still getting a UTC time for the tide rather than local time.

With my value_template, I get next high tide at "22:37"

With the raw attribute. I get next high tide at "2021-08-30 22:37:00+01:00"

For the 'time to" attribute, I get "4h 10m" from a current BST time of "19:27" ... correct for the actual tide at 23:37 BST.

I'm pretty convinced this is something simple in my understanding at this end. I'll make a post on the HA forum in the morning.

ianByrne commented 3 years ago

Timezones and DST never cease to cause me a migraine 😄 I'll see if I can try to reproduce the issue tonight

ianByrne commented 3 years ago

Can you please go to Developer Tools --> Template, paste the following, and let me know the results?

{{ utcnow().tzinfo }}
{{ now().tzinfo }}
{{ now().astimezone().tzinfo }}
ianByrne commented 3 years ago

After much banging my head against the wall, I think I have now fixed the issue (8cb1ec0)

In the end I had a misconfigured server - I thought it was running in BST but it was in fact UTC. When I fixed that, I was then able to reproduce the issue, and then finally fix it.

Please update through HACS and then let me know how it looks on your end

chsims1 commented 3 years ago

That's fixed it!. Thanks very much for your efforts Ian.