#Template to create sensor of tide height

1 messages Β· Page 1 of 1 (latest)

harsh moss
#

Hey there!! Looking for help creating a sensor I can pass through to esphome for a display.

I have a tide sensor which gives various information from which its definitely possible to guess how high the tide currently is. I just don't know how to do that in template code lol.

Two approaches I've thought of so far are to either figure out which of the given predictions is closest to the current time and then have it so anything above 4 displays high, below 2 displays low and 2.1 to 3.9 being middle.

Or to do it through the attributes that state when the next high and low tides are where if the time difference between the current time and the next high tide is less than 1 it shows high etc.

but I'm really new to all of this and do not understand how I take the data this sensor is giving me and turn it into something I can actually use in my display! Any help is super appreciated. πŸ˜„

#
predictions:
  - - '2023-06-16 03:37:00'
    - 4.6
  - - '2023-06-16 10:17:00'
    - 0.9
  - - '2023-06-16 16:00:00'
    - 4.7
  - - '2023-06-16 22:41:00'
    - 0.9
  - - '2023-06-17 04:23:00'
    - 4.7
  - - '2023-06-17 11:04:00'
    - 0.9
  - - '2023-06-17 16:41:00'
    - 4.8
  - - '2023-06-17 23:27:00'
    - 0.8
  - - '2023-06-18 05:05:00'
    - 4.7
  - - '2023-06-18 11:47:00'
    - 0.9
  - - '2023-06-18 17:21:00'
    - 4.9
  - - '2023-06-19 00:08:00'
    - 0.9
  - - '2023-06-19 05:46:00'
    - 4.7
  - - '2023-06-19 12:25:00'
    - 0.9
  - - '2023-06-19 18:00:00'
    - 4.9
  - - '2023-06-20 00:47:00'
    - 0.9
  - - '2023-06-20 06:25:00'
    - 4.6
  - - '2023-06-20 13:01:00'
    - 1
  - - '2023-06-20 18:37:00'
    - 4.9
  - - '2023-06-21 01:22:00'
    - 1
  - - '2023-06-21 07:01:00'
    - 4.6
  - - '2023-06-21 13:33:00'
    - 1.2
  - - '2023-06-21 19:12:00'
    - 4.8
  - - '2023-06-22 01:55:00'
    - 1.1
  - - '2023-06-22 07:35:00'
    - 4.5
  - - '2023-06-22 14:04:00'
    - 1.3
  - - '2023-06-22 19:47:00'
    - 4.7
next_high_tide_in: 3h 22m
next_high_tide_at: '2023-06-16T17:00:00+01:00'
next_high_tide_height: 4.7m
next_low_tide_in: 10h 3m
next_low_tide_at: '2023-06-16T23:41:00+01:00'
next_low_tide_height: 0.9m
icon: mdi:waves```
vale nebula
#

are those times UTC or Local?

#

@harsh moss

#

I can probably figure it out if you post your current time

charred moth
#

the other attributes do have a timezone

vale nebula
#

right but the next high tide is in 3h 22m, and it points to 17th hour at +1, but the next tide in the list shows 16. I can assume the list is UTC... but I'd like to be 100% sure

charred moth
#

there's also a one hour time difference with the next low tide, and the next low value in the list

#

so I would also assume the list has UTC timestamps

vale nebula
#

Well, either way, I think it would just be the latest value

#

{{ state_attr('sensor.tide', 'predictions')[0][1] }}

harsh moss
#

I think theyre local

#

its info from the UK hydro office and so I think they would do it as BST which is +1 right?

#

tried the code though and it worked perfectly. grabbed the right number straight away!! thank you!!

#

ill let you know how cobbling it together into a bunch of if statements on my esp device goes haha!

harsh moss
#

just noticed that the list isnt in the right order. is there anyway to make it search through the list for the first non-past date and read the value from that?

vale nebula
#

what's not correct about the order? what you posted is showing dates & times in chronological order

#

notice the date on the left, then the time on the right.

#

you have like 5 days of times, sorted in chronological order

harsh moss
#

the first few times are past times. so tldr the api has grabbed all the data for june 16th until june 22nd

#

but it doesnt "move up" and delete the past times, they stay in the log until it calls for the next week of data

#

so the template you gave me would always read the same value and wouldn't update with the actual tide

#

but I think Ive used your idea to bodge it together anyway πŸ˜„

#

using next_high_tide_in I've filtered out all of the other numbers so it's just the first one and im going to write the rest of the code around that

#

tides are like 12 hours between high and low so I figure

if high tide is in greater than 6 hours then use the low tide sensor instead should do it, i think? maybe.... D:

charred moth
#

I need you need to take the difference between the next event, and the previous event, look how much time has past since the previous event, and calculate the tide based on the difference between the previous level and the next level

harsh moss
#

thats a lot more complex than what I was pitching my capabilites at

#

but if you can point me in the right direction ill try!

#

{{ strptime(state_attr("sensor.falmouth_tide", "next_high_tide_at"), "%Y-%m-%dT%H:%M:%S%z") }}

#

ValueError: Template error: strptime got invalid input '2023-06-16 17:00:00+01:00' when rendering template '{{ strptime(state_attr("sensor.falmouth_tide", "next_high_tide_at"), "%Y-%m-%dT%H:%M:%S%z") }}' but no default was specified

#

This would be perfect if it worked, as it cuts out the having to figure out the next high etc myself

#

if it worked it would just tell me the time and then i could check the difference between it and the current time to guess what the tide is currently like

#

less than 2 hours to high and rising = 3 up arrows. more than 10 hours to high and rising = 1 up arrow

#

10 hours to high and falling = 1 down arrow

2 hours to high and falling = 3 down arrows

etc

vale nebula
#

you can always just use | as_datetime too

#

state_attr("sensor.falmouth_tide", "next_high_tide_at") | as_datetime

harsh moss
#

I think I have figured something out that works but It could benefit from a once over from better eyes than mine!

#

its currently reporting High, which it should be!

#

next high tide is in 30 mins so it should be right? I think?

vale nebula
#

that's less than 2 seconds

harsh moss
#

o lol then its definitely not working

vale nebula
#

now() - state_attr("sensor.falmouth_tide", "next_high_tide_at") | as_datetime < timedelta(minutes=30)

#

but that if check doesn't make any sense to be honest

#

because that doesn't tell you high/low tide

#

it just tells you if high tide is in the next 30 minutes

harsh moss
#

does it not display High if its within less than 2 hours (or seconds rather :p)? that was the intention

#

TypeError: float() argument must be a string or a real number, not 'datetime.datetime'

your code gave me this error

#

but its good to learn time delta is a thing you can do all the same πŸ˜„ thank you!

vale nebula
#

what does {{ state_attr("sensor.falmouth_tide", "next_high_tide_at") | as_datetime }} return?

harsh moss
#

TypeError: float() argument must be a string or a real number, not 'datetime.datetime'

vale nebula
#

so it's already a datetime

#
now() - state_attr("sensor.falmouth_tide", "next_high_tide_at") < timedelta(minutes=30)
harsh moss
#

returned true!

#

πŸ˜„ this looks to be the one!!! thank you!

#

I can just repeat it a few times and do the maths to figure out which kind of tide icon high/med/low to display now :DDDDD

harsh moss
#
High
   {% endif %}
{% if state_attr("sensor.falmouth_tide", "next_low_tide_at") - now() < timedelta(minutes=120) %}
Low
{% endif %}
{% if state_attr("sensor.falmouth_tide", "next_high_tide_at") - now() > timedelta(minutes=600) %}
High
{% endif %}
{% if state_attr("sensor.falmouth_tide", "next_low_tide_at") - now() > timedelta(minutes=600) %}
Low
{% endif %}```

created a sensor using this and it looks like its reporting accurately so far. the only issue i can see is th at it doesnt report a status its neither high nor low but it does output "unknown" so I can atleast use that in the esp to output the right iconfile!