Dynamic group name based on day

I’m using Dark Sky for a weather sensor. I’m trying to basically display a 7-day forecast on my dashboard. However, I can’t find a way to dynamically change the group name of my sensor group to make it easy to read.

Groups.yaml entry example:

Weatherday2:
  name: Weather in 2 days
  entities:
    - sensor.dark_sky_summary_2
    - sensor.dark_sky_daytime_high_temperature_2
    - sensor.dark_sky_overnight_low_temperature_2
    - sensor.dark_sky_humidity_2
    - sensor.dark_sky_precip_accumulation_2
    - sensor.dark_sky_precip_probability_2
    - sensor.dark_sky_wind_speed_2
    - sensor.dark_sky_wind_gust_2
    - sensor.dark_sky_icon_2

Here is what my card looks like for this entry:

Rather than say “Weather in 2 days” I would prefer for that to say “Friday’s weather” (being that today is Wednesday). Of course, tomorrow, it would need to say “Saturday’s weather”. Can this be done?

By the looks of the forecast, I may have some time to sit inside and work on this in 2 days :slight_smile:

It’s not really documented, but if you go to the Services page, and select group.set in the Service drop-down, you’ll see that there is indeed a service available for creating/changing groups. You could use that in an automation that is triggered every day to re-create the group, using a data_template for the group name. (I use the service whenever one of my cameras is turned off or on to update a tab that then just shows the cameras that are actually on.)

I don’t use Lovelace (yet, except for some brief playing with it a while ago), but I wonder if it could make this easier.

1 Like

Thanks! I’ll look into this.

Presumably, I would need 7 automations, one that triggers at 12:00am each day, correct? Then each of the 7 automations would change the group name on each of the 7 groups to the next day.

One automation, seven steps, each step creating/updating one of the seven groups.

EDIT: You’d need to use a template to calculate the name of the group. Something along the lines of:

{{ ('M','Tu','W','Th','F','Sa','Su')[now().weekday()] }}

EDIT 2: Actually, it might be easier to do in a Python Script, called by an automation that is triggered every midnight.

I’ll have to start researching templates. I’m unfamiliar with how to use those.

I’m getting close but could use a little help getting over the finish line.

I successfully got this template to return the correct output on the HA template engine. It returns “Thursday’s Weather” (Today is Wednesday)

{{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+1] }}'s Weather

However, when I add it to my automation:

- id: '1547684288304'
  alias: Change Dark Sky Group Name
  trigger:
  - at: 00:01
    platform: time
  condition: []
  action:
  - service: group.set
    data:
      entity_id: group.Weathertomorrow
      name: {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+1] }}'s Weather

I get the error:

Error loading /config/configuration.yaml: while parsing a flow mapping
  in "/config/automations.yaml", line 726, column 14
expected ',' or '}', but got '['
  in "/config/automations.yaml", line 726, column 88

A few things. First, if you’re going to use a template in a data item, you need to use data_template instead of data. Next, you either need to put quotes around the entire template, or use multi-line YAML. Next, you don’t use entity_id: group.Weathertomorrow, you use object_id: Weathertomorrow. (Actually, I’d always suggest making object_id’s all lower case.) Lastly, you’ll need to specify the entities in the group. So:

- id: '1547684288304'
  alias: Change Dark Sky Group Name
  trigger:
  - at: 00:01
    platform: time
  condition: []
  action:
  - service: group.set
    data_template:
      object_id: Weathertomorrow
      name: >
        {{ ['Monday','Tuesday','Wednesday','Thursday',
            'Friday','Saturday','Sunday'][now().weekday()+1] }}'s Weather
      entities: 
      - sensor.dark_sky_summary_2
      - sensor.dark_sky_daytime_high_temperature_2
      - sensor.dark_sky_overnight_low_temperature_2
      - sensor.dark_sky_humidity_2
      - sensor.dark_sky_precip_accumulation_2
      - sensor.dark_sky_precip_probability_2
      - sensor.dark_sky_wind_speed_2
      - sensor.dark_sky_wind_gust_2
      - sensor.dark_sky_icon_2

THANKS!!! That it working. I didn’t need to list each entity in the group, just listing the group worked.

- id: '1547684288304'
  alias: Change Dark Sky Group Name
  trigger:
  - at: 00:01
    platform: time
  condition: []
  action:
  - service: group.set
    data_template:
      object_id: weathertomorrow
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+1] }}'s Weather

So, step #2 of my problem, if you are still willing to help :). I need to change a total of 7 groups (day 1 to day 7 in the future). I tried expanding the code and added several data_templates, but that didn’t work.

- id: '1547684288304'
  alias: Change Dark Sky Group Name
  trigger:
  - at: 00:01
    platform: time
  condition: []
  action:
  - service: group.set
    data_template:
      object_id: weathertomorrow
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+1] }}'s Weather
    data_template:
      object_id: weatherday2
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+2] }}'s Weather
    data_template:
      object_id: weatherday3
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+3] }}'s Weather
    data_template:
      object_id: weatherday4
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+4] }}'s Weather
    data_template:
      object_id: weatherday5
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+5] }}'s Weather
    data_template:
      object_id: weatherday6
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+6] }}'s Weather

Any idea how I can expand this to all the groups?

- id: '1547684288304'
  alias: Change Dark Sky Group Name
  trigger:
  - at: 00:01
    platform: time
  condition: []
  action:
  - service: group.set
    data_template:
      object_id: weathertomorrow
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+1] }}'s Weather
  - service: group.set
    data_template:
      object_id: weatherday2
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+2] }}'s Weather
  - service: group.set
    data_template:
      object_id: weatherday3
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+3] }}'s Weather
  - service: group.set
    data_template:
      object_id: weatherday4
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+4] }}'s Weather
  - service: group.set
    data_template:
      object_id: weatherday5
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+5] }}'s Weather
  - service: group.set
    data_template:
      object_id: weatherday6
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][now().weekday()+6] }}'s Weather

Thanks again! That solved that problem. On to the next. :slight_smile:

It appears the template code is stopping at Sunday (+4) My assumption is that it will always stop at Sunday, so tomorrow (Thursday) it will only go to +3. Is there a better way to write the template so that it starts over on Monday?

In all the templates, try changing

now().weekday()+N

to

(now().weekday()+N)%7

That did it!! Thank you so much for your help!!

If anyone else wants to do this, here is the final automation input.

- id: '1547684288304'
  alias: Change Dark Sky Group Name
  trigger:
  - at: 00:01
    platform: time
  condition: []
  action:
  - service: group.set
    data_template:
      object_id: weathertomorrow
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][(now().weekday()+1)%7] }}'s Weather
  - service: group.set
    data_template:
      object_id: weatherday2
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][(now().weekday()+2)%7] }}'s Weather
  - service: group.set
    data_template:
      object_id: weatherday3
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][(now().weekday()+3)%7] }}'s Weather
  - service: group.set
    data_template:
      object_id: weatherday4
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][(now().weekday()+4)%7] }}'s Weather
  - service: group.set
    data_template:
      object_id: weatherday5
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][(now().weekday()+5)%7] }}'s Weather
  - service: group.set
    data_template:
      object_id: weatherday6
      name: > 
        {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][(now().weekday()+6)%7] }}'s Weather
  - service: group.set
    data_template:
      object_id: weatherday7
      name: > 
        Next {{ ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'][(now().weekday()+7)%7] }}'s Weather

Well, the upgrade to 0.86.4 broke this somehow. I even tried to manually trigger it and it didn’t work. It is changing the friendly_name on my states page, but not on the Lovelace front end. Does this require a different automation with Lovelace?

Here’s the code in my automation

- id: 'changedarkskygroupname'
  alias: Change Dark Sky Group Name
  trigger:
  - at: 00:01
    platform: time
  condition: []
  action:
  - service: group.set
    data_template:
      object_id: weathertomorrow
      name: '{{ [''Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'',''Saturday'',''Sunday''][(now().weekday()+1)%7]
        }}''s Weather
        '
  - service: group.set
    data_template:
      object_id: weatherday2
      name: '{{ [''Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'',''Saturday'',''Sunday''][(now().weekday()+2)%7]
        }}''s Weather
        '
  - service: group.set
    data_template:
      object_id: weatherday3
      name: '{{ [''Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'',''Saturday'',''Sunday''][(now().weekday()+3)%7]
        }}''s Weather
        '
  - service: group.set
    data_template:
      object_id: weatherday4
      name: '{{ [''Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'',''Saturday'',''Sunday''][(now().weekday()+4)%7]
        }}''s Weather
        '
  - service: group.set
    data_template:
      object_id: weatherday5
      name: '{{ [''Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'',''Saturday'',''Sunday''][(now().weekday()+5)%7]
        }}''s Weather
        '
  - service: group.set
    data_template:
      object_id: weatherday6
      name: '{{ [''Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'',''Saturday'',''Sunday''][(now().weekday()+6)%7]
        }}''s Weather
        '
  - service: group.set
    data_template:
      object_id: weatherday7
      name: 'Next {{ [''Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'',''Saturday'',''Sunday''][(now().weekday()+7)%7]
        }}''s Weather
        '

Well, I’m still on 0.84.6, and I don’t use Lovelace, so I can’t really help you with this. I suggest you start a new topic and maybe someone with recent Lovelace experience can help.