Template query with array

No one is interested in your sensor :slight_smile: But… with this one I meant

{% set ts_now = ((as_timestamp(now())/1800)|round(0,'floor')|int * 1800) %}
        {{ state_attr('sensor.octopus_energy_electricity_14e0453806_1900013341923_current_rate', 'rates')
         }}

Here.

Very weird indeed, what version of HA are you on?

Home Assistant 2023.2.5
Supervisor 2023.01.1
Operating System 9.5
Frontend 20230202.0 - latest

Thx… I am raising this on discord now, I know of a guy who may shed more light…this is beyond me

Many thanks - I’m concurrently at my daughter’s birthday party but should be able to jump on discord in an hour…

I woke up my HAOS dev stuff and updated it to belwo (same)…and also there…no issues …so let’s hope discord brings help
Home Assistant 2023.2.5
Supervisor 2023.01.1
Operating System 9.5
Frontend 20230202.0 - latest

Have a look here…posisbly a bug Temple attribute with list not created nicely - Configuration - Home Assistant Community (home-assistant.io)

Thanks, and thanks once again for your help. I am sorry it was so complicated.

I (rather stupidly, and through my own poor decision making) was somewhat distracted from my daughters birthday party because of this!

Do you think I should try and raise it as a bug somewhere?

What’s curious is that:

      {{ state_attr('sensor.octopus_energy_electricity_14e0453806_1900013341923_current_rate', 'rates')
          | selectattr('rate', 'equalto', 23.037 )
          | map(attribute='from') | first }}

using the rate to look up the from time works and returns:

2023-02-19 00:00:00+00:00

You should raise it I would say… and refer also to the other case.
on the other curious thing… I cannot tell as I have none of your issues

Got feedback from TheFes (one mentioned)

That's indeed a python datetime tuple. You can use .isoformat() to convert it to a string. Then you need to use as_datetime to use it as a datetime again

As I have no example, cannot help at all

1 Like

Oh, hey - the comments in the other thread, in particular from @123 along with this comment you gave me have me here:

{% set dt_required = (as_datetime((as_timestamp(now())/1800)|round(0,'floor')|int * 1800)) %}
{{ state_attr('sensor.octopus_energy_electricity_14e0453806_1900013341923_current_rate', 'rates')
          | selectattr('from', 'equalto', dt_required ) | map(attribute='rate') | first}}

This works!!!

Thanks so much!

Good but still does not explain why the other one worked for me…anyhow…moving on :slight_smile:

1 Like

I believe you can streamline the template that gets the current time in ISO format. You should also append a default filter, in the rare case there’s no match found.

{% set t_now = utcnow().replace(minute=0, second=0, microsecond=0).isoformat() %}
{{ state_attr('sensor.octopus_energy_electricity_14e0453806_1900013341923_current_rate', 'rates')
  | selectattr('from', 'equalto', t_now)
  | map(attribute='rate') | first | default('') }}
1 Like

So you did look at it :slight_smile: … now… why oh why does my template work with the same (old/original) code and James’ not? Is it possibly a formatting within the sensor when I copied the data into it?
EDIT: not asking more time …just if-you-know (have an idea)

FWIW, I saw the Issue that jamesking posted in the Core repository. I thought the Issue’s example wasn’t optimal so I revised it, tested it on the sample of data posted in the Issue and confirmed it worked.

I was going to post my working example in the Issue but then noticed it contained a link to this topic (which I never read). Anyway, as far as I’m concerned, the Issue is reporting a non-existent bug.