Split up entity id's

Hi All,

I have some sensors:

sensor.toogoodtogo_poke_perfect_zuidas_magic_box_stock_available
sensor.toogoodtogo_juicebrothers_gustav_mahlerlaan_magic_box_stock_available
sensor.toogoodtogo_le_pain_quotidien_gelderlandplein_magic_box_stock_available
sensor.toogoodtogo_stadsbakker_jongejans_gelderlandplein_magic_box_stock_available

I want split them up in follow:

toogoodtogo_poke_perfect_zuidas_magic_box      stock_available
toogoodtogo_juicebrothers_gustav_mahlerlaan_magic_box       stock_available
toogoodtogo_le_pain_quotidien_gelderlandplein_magic_box       stock_available
toogoodtogo_stadsbakker_jongejans_gelderlandplein_magic_box      stock_available

"{{ trigger.entity_id.split('_', 1)[1] }}"

This part removed the sensor part. But how can I remove the stock_available also?
As you can see not all the entities have same number of _ in them so can’t use that one.

Cheers,

"{{ trigger.entity_id | replace('_stock_available', '') | replace('sensor.', '') }}"

Would using the trigger friendly name be a better option?

I was trying something but I think I not take right path…

sensor.toogoodtogo_albert_heijn_amsterdamseweg_amstelveen_magic_box

state:

0

attributes:

price: 4.99
stock_available: true
url: 'http://share.toogoodtogo.com/item/52294'
unit_of_measurement: portions
friendly_name: 'TooGoodToGo - Albert Heijn  - Amsterdamseweg, Amstelveen (Magic Box)'
icon: 'mdi:food-off'
templates:
  icon_color: >-
    if (state === 'on') return 'rgba(251,214,67,1)'; return
    'rgba(71,116,157,1)';
icon_color: 'rgba(71,116,157,1)'

I have now a automation. Only that missing is a condition when the stock_available
only goes from false to true.

- alias: "TooGoodToGo - All shops"
  trigger:
    - platform: state
      entity_id:
        - sensor.toogoodtogo_broodhuys_meijer_magic_box
        - sensor.toogoodtogo_albert_heijn_amsterdamseweg_amstelveen_magic_box
        - sensor.toogoodtogo_albert_heijn_to_go_amsterdam_wtc_magic_box
        - sensor.toogoodtogo_boulangerie_noe_gustav_mahlerlaan_amsterdam
        - sensor.toogoodtogo_de_drie_graefjes_stadionplein_amsterdam
        - sensor.toogoodtogo_hema_gelderlandplein
        - sensor.toogoodtogo_lidl_amsterdamseweg_188_groente_fruit_box
        - sensor.toogoodtogo_lidl_bankrashof_groente_fruit_box
        - sensor.toogoodtogo_starbucks_gustav_mahler_amsterdam
        - sensor.toogoodtogo_sushi_time_wtc_amsterdam_magic_box
        - sensor.toogoodtogo_the_roastary_magic_box
        - sensor.toogoodtogo_poke_perfect_zuidas_magic_box
        - sensor.toogoodtogo_juicebrothers_gustav_mahlerlaan_magic_box
        - sensor.toogoodtogo_le_pain_quotidien_gelderlandplein_magic_box
        - sensor.toogoodtogo_stadsbakker_jongejans_gelderlandplein_magic_box

  variables:
    stock: "{{ states(trigger.entity_id) }}"
    price: "{{ state_attr(trigger.entity_id, 'price') }}"
    name: "{{ state_attr(trigger.entity_id, 'friendly_name') }}"

  #  condition: "{{ trigger.to_state.attributes.stock_available == 'true' }}"

  action:
    - service: notify.mobile_app_peter_iphone
      data:
        message: "TooGoodToGo"
        title: "{{ name }} have {{ stock }} to buy. Price: {{ price }} "

To get everything but stock_availabiltiy…

trigger.to_state.object_id.split('_')[:-2] | join('_')

To check to see if it ends with stock_availability…

trigger.to_state.object_id.split('_')[-2:] | join('_') == 'stock_availability'

You could also just do trigger.entity_id.endswith(‚Äėstock_availability‚Äô)

This is working now. Only need a condition for stock_available from false to true.

- alias: "ToGoodToGo - All shops"
  trigger:
    - platform: state
      entity_id:
        - sensor.toogoodtogo_broodhuys_meijer_magic_box
        - sensor.toogoodtogo_albert_heijn_amsterdamseweg_amstelveen_magic_box
        - sensor.toogoodtogo_albert_heijn_to_go_amsterdam_wtc_magic_box
        - sensor.toogoodtogo_boulangerie_noe_gustav_mahlerlaan_amsterdam
        - sensor.toogoodtogo_de_drie_graefjes_stadionplein_amsterdam
        - sensor.toogoodtogo_hema_gelderlandplein
        - sensor.toogoodtogo_lidl_amsterdamseweg_188_groente_fruit_box
        - sensor.toogoodtogo_lidl_bankrashof_groente_fruit_box
        - sensor.toogoodtogo_starbucks_gustav_mahler_amsterdam
        - sensor.toogoodtogo_sushi_time_wtc_amsterdam_magic_box
        - sensor.toogoodtogo_the_roastary_magic_box
        - sensor.toogoodtogo_poke_perfect_zuidas_magic_box
        - sensor.toogoodtogo_juicebrothers_gustav_mahlerlaan_magic_box
        - sensor.toogoodtogo_le_pain_quotidien_gelderlandplein_magic_box
        - sensor.toogoodtogo_stadsbakker_jongejans_gelderlandplein_magic_box

  variables:
    stock: "{{ states(trigger.to_state.entity_id) }}"
    stock_available: "{{ state_attr(trigger.to_state.entity_id, 'stock_available') }}"
    price: "{{ state_attr(trigger.to_state.entity_id, 'price') }}"
    name: "{{ state_attr(trigger.to_state.entity_id, 'friendly_name') }}"

  #condition: "{{ is_state('stock_available', 'True') }}"

  action:
    - service: notify.mobile_app_peter_iphone
      data:
        message: "TooGoodToGo"
        title: "{{ stock }},{{ stock_available }},{{ price }},{{ name }} "

Hi Petro,

Is there a website where you can easily do some test with this split function?
Or do you know a page that explain it ?

Want use it again, but don’t know how this split is working… :slight_smile:

Use the template editor to test

trying. But would be nice to have some basic explanation haha :slight_smile:

Now try and play with this:

{% set variable1 = 'sensor.ha_cpu_temperature' %}

**TEST 1**
{% set list1 = variable1.replace('sensor.', '') %}
{% set list12 = list1.split('_', 4) %}
{% for item in list12 %}
{{ item }}
{% endfor %} 

** TEST 2**
{% set list2 = variable1.split('_', 1)[0] %}
{% for item in list2 %}
{{ item }}
{% endfor %} 

** TEST 3**
{% set list3 = variable1.split('.', 1)[0] %}
{% for item in list3 %}
{{ item }}
{% endfor %}

:slight_smile:

It’s python, check out the python docs for .split(). Everything you’re doing is fine but you keep giving a max split value for some reason, which is almost never needed.

1 Like

Read some nice articles and extract words from entity_id and give them also a capital letter

binary_sensor.bedroom_door_contact
binary_sensor.diningroom_door_contact
binary_sensor.frondoor_door_contact
binary_sensor.imac_door_contact
"{{ trigger.entity_id.replace('binary_sensor.','').split('_door_contact')[0].capitalize() }}"

Bedroom
Diningroom
Frontdoor
Imac

Thanks to help me the right way!

Try this:

"{{ trigger.entity_id[14:-13] | title }}"

Damn… haha