Convert multiple entities

I have an integration (Theme Park Times) installed which returns queue times for rides in a time format (HH:MM:SS) but I want to be able to sort them. I can use {{ states(’’) }} to give me the absolute value, but I need to do this for probably 20-30 entities. I assume that creating a modified entity based on the original is the way to go, so that I can then use this in dashboards and automations

Is there a way to create the new “corrected” entities in one go or, better still, a more logical way to achieve this, please?

Thank you

Probably a template sensor:

but with that little detail of the problem you’re trying to solve, it’s hard to advise. Give some actual data and information on what you’re trying to get to?

Thank you for such a swift response.

The integration creates an entity for each ride, with a time as the value. For each of these, I need to create a new (template) entity which will have an integer time in minutes (rather than hh:mm:ss).

As you suggest, I could create a template sensor for each separate entity, but that will need 20 or more template entity (I assume) entries; one for each of the originals. What I was hoping for was a way to create each of them with one command or, even better, a more logical way to apprach this.

I want to be able to use the int value in sorts and in automations etc.

Thank you,

What does the value look like, exactly? If it’s genuinely hh:mm:ss, then:

{% set h, m, s = value.split(':')|map('int', default=0) %}
{{ h*60+m }}

will return an integer number of minutes.

What are the entities called? Show us actual data.

Sorry, what I have is (for example)

sensor.crush_s_coaster_disneyland_paris

returns 1:20:00

{{ states('sensor.crush_s_coaster_disneyland_paris') |int }}

returns 80 (which is what I want)

You still haven’t said what output you want. If you’re right about the int giving you minutes, put your entities into this list and it’ll give you a list of dictionaries of entity IDs and states from longest to shortest duration.

{% set ride_entities = ['sensor.crush_s_coaster_disneyland_paris',
                        'sensor.next_ride',
                        'sensor.last_ride'] %}
{% set ns=namespace(rl=[]) %}
{% for r in ride_entities %}
  {% set ns.rl = ns.rl + [{'id': r, 'state': states(r)|int(0)}] %}
{% endfor %}
{{ ns.rl|sort(attribute='state', reverse=True) }}

You can probably get the list without typing them all out by using:

{% set ride_entities = integration_entities('theme_park_times') %}

but I’m guessing at the name.

That’s fantastic. Thank you very much. I’d like to think that this will give me enough to get what I need. I will have a play with this.

The int does indeed give me minutes which is both good and a little frustrating as getting there was easy, but applying it to all of the entities was less so.

Thank you,

Just in case anyone else has the same need. The list of entities is available from

{{ integration_entities('themeparks') }}

Embarrassingly, I’m still stuck. I have it generating the list perfectly (I think). I get

[
  {
    "id": "sensor.crush_s_coaster_disneyland_paris",
    "state": 60
  },
  {
    "id": "sensor.peter_pan_s_flight_disneyland_paris",
    "state": 55
  },
  {
    "id": "sensor.spider_man_w_e_b_adventure_disneyland_paris",
    "state": 45
  },
  {
    "id": "sensor.meet_mickey_mouse_disneyland_paris",
    "state": 45
  },
  {
    "id": "sensor.orbitron_r_disneyland_paris",
    "state": 40

etc

How do I get these new values to set as a (new?) set of entities such that I can use them in triggers in an automation or as entries in a entity list in a dashboard?

Can you be more explicit about what the end goal is as well as which integration you are using? The Theme Parks Wait Time integration in HACS doesn’t seem to match the limited details you have provided.

Since states are always strings, you can sort states in “HH:MM:SS” more easily than states converted using the int method mentioned above, because you will just have to perform the int function again when you do your sort. Here’s an analog that you can test in the template editor:

{{ ["02:30:00", "01:01:00", "00:45:00", "00:01:00"] | sort }}
{{ ["150", "61", "45", "1"] | sort }}

{{ ["150", "61", "45", "1"] | map('int') | sort }}

If you want distinct entities, there isn’t really a way around setting up individual template sensors. You cannot use templating to build YAML “live” in your configuration. You can use the template editor to print something you can copy/paste into your configuration, but it can be as much or more work to do it that way than just doing it manually.

What I am aiming to do is twofold. First, I’d like to use the auto-entities card to display each of the entries which are greater than zero, sorted by longest first.

However, my main requirement is to create an automation which will send me a notification if selected rides drop below a threshold. Ironically the Github page for the integration suggests that this is a helpful use, but I can’t see a simple way to do it. Although, I now wonder whether I could use a template trigger. Ideally I’d have like to have a single automation and a separate list of “selected” rides and a separate trigger value, so that it could be re-used.

I hope that makes some kind of sense.

For a 30 minute threshold, and assuming you have notifications already working:

trigger:
  - platform: numeric_state
    entity_id:
      - sensor.crush_s_coaster_disneyland_paris
      - sensor.peter_pan_s_flight_disneyland_paris
      - sensor.spider_man_w_e_b_adventure_disneyland_paris
    below: 30 
action:
  - service: notify.notify
    data:
      message: "{{ trigger.to_state.name }} now below 30 minutes!"
      title: "Hey!"

I’ve just included the first three entities. To save typing the whole lot, generate the list of entities in the Template Editor for pasting into the YAML view of the automation:

{{ integration_entities('themeparks')|join('\n    - ') }}

You’ll have to fix the first line yourself to match the rest.

So, I can look for the numeric state, even though the entity is (AIUI) holding the value as text? If it’s that simple, then I can see why my simple misunderstanding is making to too difficult.

When I set up the auto-entities, card, the sort there didn’t work as that was certainly sorting on the text (1:30:00 was less than 5:00).

It depends what these entities are actually holding.

What, for example, do these give in the template sensor (try one at a time if one of them gives an error)?

{{ states('sensor.crush_s_coaster_disneyland_paris') }}
{{ states('sensor.crush_s_coaster_disneyland_paris')|int + 1 }}
x {{ states('sensor.crush_s_coaster_disneyland_paris') }} x

The numeric_state trigger treats the string state as a number. If the state is really the string "01:30:00" it’ll fail, but I don’t believe it is, otherwise this wouldn’t be true:

Yes, that works and it does seem to be treating them as numeric in that context. So, my confusion then must be why, when I use the sensors in the auto-entities card, it shows the values as “text” and then sorts them wrongly.
image

It looks as if my main misunderstanding is why the values seem to be being treated (displayed?) differently in different contexts.

Probably something like device_class: duration associated with the sensors (docs).

Try a numeric sort? GitHub - thomasloven/lovelace-auto-entities: 🔹Automatically populate the entities-list of lovelace cards

Eureka! That was it. As simple as that.

Thank you so much for your help and patience in getting this to work.

1 Like