home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.03k stars 29.68k forks source link

timestamp and datetime don't apear to be working correctly in templating #88447

Closed ghost closed 1 year ago

ghost commented 1 year ago

The problem

I have a sensor, from the following custom integration: https://github.com/BottlecapDave/HomeAssistant-OctopusEnergy The sensor's state attributes yaml is as follows, as shown in Developer Tools/States is as follows:

rate:
  value_exc_vat: 21.939999999999998
  value_inc_vat: 23.037
  valid_from: '2023-02-19T15:00:00+00:00'
  valid_to: '2023-02-19T15:30:00+00:00'
  tariff_code: E-1R-SILVER-FLEX-22-11-25-J
is_export: false
is_smart_meter: false
rates:
  - from: '2023-02-19T00:00:00+00:00'
    to: '2023-02-19T00:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T00:30:00+00:00'
    to: '2023-02-19T01:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T01:00:00+00:00'
    to: '2023-02-19T01:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T01:30:00+00:00'
    to: '2023-02-19T02:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T02:00:00+00:00'
    to: '2023-02-19T02:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T02:30:00+00:00'
    to: '2023-02-19T03:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T03:00:00+00:00'
    to: '2023-02-19T03:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T03:30:00+00:00'
    to: '2023-02-19T04:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T04:00:00+00:00'
    to: '2023-02-19T04:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T04:30:00+00:00'
    to: '2023-02-19T05:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T05:00:00+00:00'
    to: '2023-02-19T05:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T05:30:00+00:00'
    to: '2023-02-19T06:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T06:00:00+00:00'
    to: '2023-02-19T06:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T06:30:00+00:00'
    to: '2023-02-19T07:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T07:00:00+00:00'
    to: '2023-02-19T07:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T07:30:00+00:00'
    to: '2023-02-19T08:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T08:00:00+00:00'
    to: '2023-02-19T08:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T08:30:00+00:00'
    to: '2023-02-19T09:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T09:00:00+00:00'
    to: '2023-02-19T09:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T09:30:00+00:00'
    to: '2023-02-19T10:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T10:00:00+00:00'
    to: '2023-02-19T10:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T10:30:00+00:00'
    to: '2023-02-19T11:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T11:00:00+00:00'
    to: '2023-02-19T11:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T11:30:00+00:00'
    to: '2023-02-19T12:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T12:00:00+00:00'
    to: '2023-02-19T12:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T12:30:00+00:00'
    to: '2023-02-19T13:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T13:00:00+00:00'
    to: '2023-02-19T13:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T13:30:00+00:00'
    to: '2023-02-19T14:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T14:00:00+00:00'
    to: '2023-02-19T14:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T14:30:00+00:00'
    to: '2023-02-19T15:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T15:00:00+00:00'
    to: '2023-02-19T15:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T15:30:00+00:00'
    to: '2023-02-19T16:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T16:00:00+00:00'
    to: '2023-02-19T16:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T16:30:00+00:00'
    to: '2023-02-19T17:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T17:00:00+00:00'
    to: '2023-02-19T17:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T17:30:00+00:00'
    to: '2023-02-19T18:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T18:00:00+00:00'
    to: '2023-02-19T18:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T18:30:00+00:00'
    to: '2023-02-19T19:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T19:00:00+00:00'
    to: '2023-02-19T19:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T19:30:00+00:00'
    to: '2023-02-19T20:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T20:00:00+00:00'
    to: '2023-02-19T20:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T20:30:00+00:00'
    to: '2023-02-19T21:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T21:00:00+00:00'
    to: '2023-02-19T21:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T21:30:00+00:00'
    to: '2023-02-19T22:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T22:00:00+00:00'
    to: '2023-02-19T22:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T22:30:00+00:00'
    to: '2023-02-19T23:00:00+00:00'
    rate: 23.037
  - from: '2023-02-19T23:00:00+00:00'
    to: '2023-02-19T23:30:00+00:00'
    rate: 23.037
  - from: '2023-02-19T23:30:00+00:00'
    to: '2023-02-20T00:00:00+00:00'
    rate: 23.037
  - from: '2023-02-20T00:00:00+00:00'
    to: '2023-02-20T00:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T00:30:00+00:00'
    to: '2023-02-20T01:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T01:00:00+00:00'
    to: '2023-02-20T01:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T01:30:00+00:00'
    to: '2023-02-20T02:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T02:00:00+00:00'
    to: '2023-02-20T02:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T02:30:00+00:00'
    to: '2023-02-20T03:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T03:00:00+00:00'
    to: '2023-02-20T03:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T03:30:00+00:00'
    to: '2023-02-20T04:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T04:00:00+00:00'
    to: '2023-02-20T04:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T04:30:00+00:00'
    to: '2023-02-20T05:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T05:00:00+00:00'
    to: '2023-02-20T05:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T05:30:00+00:00'
    to: '2023-02-20T06:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T06:00:00+00:00'
    to: '2023-02-20T06:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T06:30:00+00:00'
    to: '2023-02-20T07:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T07:00:00+00:00'
    to: '2023-02-20T07:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T07:30:00+00:00'
    to: '2023-02-20T08:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T08:00:00+00:00'
    to: '2023-02-20T08:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T08:30:00+00:00'
    to: '2023-02-20T09:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T09:00:00+00:00'
    to: '2023-02-20T09:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T09:30:00+00:00'
    to: '2023-02-20T10:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T10:00:00+00:00'
    to: '2023-02-20T10:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T10:30:00+00:00'
    to: '2023-02-20T11:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T11:00:00+00:00'
    to: '2023-02-20T11:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T11:30:00+00:00'
    to: '2023-02-20T12:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T12:00:00+00:00'
    to: '2023-02-20T12:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T12:30:00+00:00'
    to: '2023-02-20T13:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T13:00:00+00:00'
    to: '2023-02-20T13:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T13:30:00+00:00'
    to: '2023-02-20T14:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T14:00:00+00:00'
    to: '2023-02-20T14:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T14:30:00+00:00'
    to: '2023-02-20T15:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T15:00:00+00:00'
    to: '2023-02-20T15:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T15:30:00+00:00'
    to: '2023-02-20T16:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T16:00:00+00:00'
    to: '2023-02-20T16:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T16:30:00+00:00'
    to: '2023-02-20T17:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T17:00:00+00:00'
    to: '2023-02-20T17:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T17:30:00+00:00'
    to: '2023-02-20T18:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T18:00:00+00:00'
    to: '2023-02-20T18:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T18:30:00+00:00'
    to: '2023-02-20T19:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T19:00:00+00:00'
    to: '2023-02-20T19:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T19:30:00+00:00'
    to: '2023-02-20T20:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T20:00:00+00:00'
    to: '2023-02-20T20:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T20:30:00+00:00'
    to: '2023-02-20T21:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T21:00:00+00:00'
    to: '2023-02-20T21:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T21:30:00+00:00'
    to: '2023-02-20T22:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T22:00:00+00:00'
    to: '2023-02-20T22:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T22:30:00+00:00'
    to: '2023-02-20T23:00:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T23:00:00+00:00'
    to: '2023-02-20T23:30:00+00:00'
    rate: 71.6415
  - from: '2023-02-20T23:30:00+00:00'
    to: '2023-02-21T00:00:00+00:00'
    rate: 71.6415
unit_of_measurement: GBP/kWh
device_class: monetary
icon: mdi:currency-gbp
friendly_name: Octopus Energy Electricity 14E0453806 1900013341923 Current Rate

However, when trying to use a template to find a match with a timestamp here, it is not working - for example:

{% set ts_now = ((as_timestamp(now())/1800)|round(0,'floor')|int * 1800) %}
{{(ts_now | timestamp_custom ('%Y-%m-%dT%H:%M:%S+00:00', local=false)|string)}}
        {{ state_attr('sensor.octopus_energy_electricity_14e0453806_1900013341923_current_rate ', 'rates')
          | selectattr('from', 'equalto', (ts_now | timestamp_custom ('%Y-%m-%dT%H:%M:%S+00:00', local=false)) )
          | map(attribute='rate')|first }}

After trying to figure out why this wasn't working, in the following thread, it appears that when templating these timestamps are seen as datetime entities instead? https://community.home-assistant.io/t/template-query-with-array/537252 There is another user experiencing a similar problem: https://community.home-assistant.io/t/temple-attribute-with-list-not-created-nicely/536461/6

Please see the thread for the apparent issue where it becomes a datetime rather than timestamp. I am subscribed to this bug so I can help with anything required

What version of Home Assistant Core has the issue?

2023.2.5

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

OCtopus Energy, and others (see linked forum threads)

Link to integration documentation on our website

N/A unofficial integration

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

issue-triage-workflows[bot] commented 1 year ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.