I’m using the multiscraping sensor to get tide info, the table cell I’m pulling from is the result for today:
- platform: multiscrape
name: home assistant scraper
resource: https://www.thesiteimscraping.com/page
scan_interval: 30
selectors:
firsthightidetime:
name: First High Tide
select: "div.stationTables > div.grid-12.indent-medium > div:nth-child(1) > table > tbody > tr:nth-child(1) > td.time"
Using this method I created these sensors:
So I’d like to format the time of the tides as a timestamp, but I only have hours.
{{ as_timestamp(states('sensor.secondlowtidetime')) }}
gives me ‘None’
{{ as_timestamp(strptime(states('sensor.secondlowtidetime'), '%H:%M')) }}
also provides ‘None’
{{ strptime(states('sensor.secondlowtidetime'), '%H:%M') }}
1900-01-01 23:30:00
{{ as_timestamp(strptime(states('sensor.secondlowtidetime'), "%H:%M")) }}
-2208904200.0
looks like we’re getting somewhere, finally a timestamp. But how to advance the year?
{% set day = (now().day) %}
{% set year = (now().year) %}
{% set month = (now().month) %}
{% set time = states('sensor.secondlowtidetime') %}
{% set tide = (year,month,day,time) %}
{{ tide }}
{{year}}-{{month}}-{{day}} {{states('sensor.secondlowtidetime')}}
gives me:
sweet - this looks ready for the as_timestamp(). But it never works! How do I format it so that as_timestamp is happy?
Troon
(Troon)
May 24, 2021, 7:17pm
2
Your problem is probably due to the non-zero-padded month (and date, but you can’t see that here). Try this:
{% set tide = "23:30" %}
{{ (now()|string)[:11] + tide }}
{{ ((now()|string)[:11] + tide)|as_timestamp }}
now()|string
returns an ISO8601 string, the first 11 characters are the date and the first space.
Ok, that helped a ton!
ok. So the scrape sensor doesn’t support device_class: timestamp, instead I created a template sensor based on the code you shared with me:
- platform: template
sensors:
nexthightide:
friendly_name: "Next high tide"
device_class: timestamp
value_template: >-
{% set firsthigh = states('sensor.firsthightidetime') %}
{{ ((now()|string)[:11] + firsthigh)|as_timestamp }}
and this displays:
Do you understand why it says invalid timestamp in an entities card?
On my phone, so I can’t type it all out…
I can see why you’re confused. The device class of timestamp assumes it’s formatted in the ISO format, not a Unix timestamp as epoch seconds.
Use this:
Filter timestamp_utc
converts a UNIX timestamp to its string representation representation as date/time in UTC timezone.
Or the _local
variant if needed.
Also check the warning in the docs .
sheesh documentation examples are thin here.
I’m not having any luck plunking timestamp_utc into my template sensor, can’t find the right syntax.
Next time show us at least one thing you’ve tried.
Superfluous example:
{{ now() | as_timestamp | timestamp_local }}
Then:
{{ ((now()|string)[:11] + firsthigh) | as_timestamp | timestamp_local }}