Template: find correct automation part name

HI,
in an automation I use this template:

  action:
    - service_template: frontend.set_theme
      data_template:
        name: >
          {% if states.sun.sun.state == "above_horizon" %}
            default
          {% else %}
            darkred
          {% endif %}
    - delay: 00:00:05
    - service: notify.notify
      data_template:
        message: >-
          It is {{ as_timestamp(now()) | timestamp_custom("%X") }}, 
          Sun is {{states.sun.sun.state}} and Frontend is set to 
          {% if states.sun.sun.state == "above_horizon" %}default 
          {% else %}darkred
          {% endif %}

Id rather use a template for the set Theme (I now have the same if then else construction, which is silly), but don’t know the correct syntax for that. Something like this maybe?
{{ action.service_data_state.attributes.name}}.
A bit like the {{trigger.to_state.attributes.friendly_name}}

Not sure if this is possible at all, but would be very nice.Please have a look?

Marius

create a template sensor that performs that template for you:

sensor:
  - platform: template
    sensors:
      time_based_theme:
        value_template: "{% if is_state('sun.sun','above_horizon') %}default{% else %}darkred{% endif %}"

then use:

{{ states.sensor.time_based_theme }}

ha, thats another interesting solution to remember indeed, thanks.
Not for this specific situation though, id like to template the theme name, not hard code it, as you still do now, albeit in the template sensor.

Seconde reason i asked, is id like to learn all about the tempting options, and lik there is some info on the trigger bit of the automations Automation trigger variables - Home Assistant, there is not a lot about actions.

id like to know whether, like with say trigger.to_state.attributes.friendly_name , a construction can be made with the condition, or action part of the automation so i can have a final data_template : message: theme set is {{theme_set}}.

I don’t think it is possible using frontend.set_theme (which needs a theme name to load the theme) so it should be done using the chosen option in the data_template.

Only other way i could think of now is set the themes per input_select, and use the {{states.input_select.set_day_theme.attributes.friendly_name}} and {{states.input_select.set_night_theme.attributes.friendly_name}} as template…

action:
    - service_template: frontend.set_theme
      data_template:
        name: >
          {% if states.sun.sun.state == "above_horizon" %}
            {{states.input_select.set_day_theme.state}}
          {% else %}
            {{states.input_select.set_night_theme.state}}
          {% endif %}
    - delay: 00:00:05
    - service: notify.notify
      data_template:
        message: >-
          It is {{ as_timestamp(now()) | timestamp_custom("%X") }}, 
          Sun is {{states.sun.sun.state}} and Frontend is set to 
          {% if states.sun.sun.state == "above_horizon" %}{{states.input_select.set_day_theme.attributes.friendly_name}}
          {% else %}{{states.input_select.set_night_theme.attributes.friendly_name}}
          {% endif %}

But, then again, i would be using the same template twice, while im looking for a possibility to use the action bit for a template in the service message.

But using a template sensor won’t hard code it. The sensor itself is variable, based on the time of day. Yes it would be hard coded in the template sensor, but you can change that by adjusting the template sensor to pull whatever info you want. The template sensor simply holds the data for you to easily use.

If you want to pull from your input selects, just adjust that in the template sensor:

sensor:
  - platform: template
    sensors:
      time_based_theme:
        value_template: >
          {% if is_state('sun.sun','above_horizon') %}
            {{states.input_select.set_day_theme.state}}
          {% else %}
            {{states.input_select.set_night_theme.state}}
          {% endif %}

using it would be the same…

{{ states.sensor.time_based_theme }}

so that would lead to:

action:
    - service_template: frontend.set_theme
      data_template:
        name:  {{ states.sensor.time_based_theme }}
    - delay: 00:00:05
    - service: notify.notify
      data_template:
        message: >-
          It is {{ as_timestamp(now()) | timestamp_custom("%X") }}, 
          Sun is {{states.sun.sun.state}} and Frontend is set to 
         {{ states.sensor.time_based_theme }}

getting more compact indeed.
Still, my quest for way to use the various parts in the automation remains. Would you know which is called what?

trying a little extra:

could

value_template: >
  {% if is_state('sun.sun','above_horizon') %}
    {{states.input_select.set_day_theme.state}}
  {% else %}
    {{states.input_select.set_night_theme.state}}
  {% endif %}

be changed into:

value_template: >
            {{states.input_select.set_{{states.sun.sun.state.split('_')[0]}}_theme.state}}

reading from input_select.set_above_theme and input_select.set_below_theme,
selected automatically base on the current state of the sun?

I’m not 100% familiar with the sun.sun sensor, but if the state it returns is in fact ‘above_x’ and ‘below_x’, then yes, that should work.

almost there…

{{states.sun.sun.state.split('_')[0]}} renders above

using that template like this:

{{states.input_select.set_{{states.sun.sun.state.split('_')[0]}}_theme.state}} errors out with:

Error rendering template: TemplateSyntaxError: expected token 'end of print statement', got '{'

whilel in fact it should yield

{{states.input_select.set_above_theme.state}}

which is what i have, and runs fine in the configuration checker:

  set_above_theme:
    name: 'Select Above Theme'
    options:
     - 'default'
     - 'darkblue'
     - 'darkcyan'
     - 'darkorange'
     - 'darkred'
     - 'done'
     - 'matrix'
     - 'midnight'
     - 'minimal'
     - 'PmxMononight'
     - 'stormy_hues'
     - 'teal'
     - 'vintage'
    initial: 'minimal'
    icon: 'mdi:palette'

this:

input_select.set_{{states.sun.sun.state.split(‘_’)[0]}}_theme.state renders:

input_select.set_above_theme.state, which is what i need… apparently the {{ and }} cause issues reading the template?

Then you cannot use {{}} inside as a part of the variable name because the interpreter doesn’t like it. You’d have to use:

{{ state('input_select.set_'+state('sun.sun').split('_')[0] + '_theme') }}

This assumes that you can add strings inside a jinja statement. I’ve never tried it so I don’t know if it works.

working nicely (added the 2 input_selects to the triggers of the automation for testing purposes only. Without the sun.sun.state to triggers, the automation kept being triggered to my surprise, since i would have thought it only to change state at sunset or rise…:

38

sensor:
  - platform: template
    sensors:
      sun_based_theme:
        friendly_name: Sun based theme
        value_template: > 
          {% if is_state('sun.sun','above_horizon') %}
           {{states.input_select.set_above_theme.state}}
          {% else %}
            {{states.input_select.set_below_theme.state}}
          {% endif %}
        icon_template: >
          {% if is_state('sun.sun', 'above_horizon') %}
            mdi:weather-sunny
          {% else %}
            mdi:weather-night
          {% endif %}  

input_select:
  set_below_theme:
    name: 'Select Below theme'
    icon: mdi:weather-night
    options:
     - 'default'
     - 'darkblue'
     - 'darkcyan'
     - 'darkorange'
     - 'darkred'
     - 'done'
     - 'matrix'
     - 'midnight'
     - 'minimal'
     - 'PmxMononight'
     - 'stormy_hues'
     - 'teal'
     - 'vintage'
    initial: 'darkred'

  set_above_theme:
    name: 'Select Above Theme'
    icon: mdi:weather-sunny
    options:
     - 'default'
     - 'darkblue'
     - 'darkcyan'
     - 'darkorange'
     - 'darkred'
     - 'done'
     - 'matrix'
     - 'midnight'
     - 'minimal'
     - 'PmxMononight'
     - 'stormy_hues'
     - 'teal'
     - 'vintage'
    initial: 'minimal'

automation:
  - alias: Sun based theme change
    id: '1511601488030'
    initial_state: 'on' 
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sun.sun
        to: above_horizon
      - platform: state
        entity_id: sun.sun
        to: below_horizon
      - platform: state
        entity_id: input_select.set_below_theme
      - platform: state
        entity_id: input_select.set_above_theme
    condition: []
    action:
      - service_template: frontend.set_theme
        data_template:
          name: >
            {{ states.sensor.sun_based_theme.state }}
      - delay: 00:00:05
      - service: notify.notify
        data_template:
          message: >-
            It is {{ as_timestamp(now()) | timestamp_custom("%X") }}, 
            Sun is {{states.sun.sun.state}} and Frontend is set to 
             '{{ states.sensor.sun_based_theme.state }}'

the sun.sun platform updates alot because you can actually get the sun position from it. If you only want it to trigger at the sunset/sunrise, use:

platform: sun
event: sunset

or

platform: sun
event: sunrise

ah nice yes. other than being more efficient coding, would it have different behavior from what I am using now:

  - platform: state
    entity_id: sun.sun
    to: above_horizon
  - platform: state
    entity_id: sun.sun
    to: below_horizon

Hi, can you tell me where i can find those themes? They are available for all or were created by you? In this case can you share them? I am newbie so i want to learn how to create themes…

HI, welcome,

read all about it: Themes - Home Assistant Community

happy to share my themes, but you might want to read-up first?
Cheers.
Marius

I already read those docs… i am asking you to share as i think viewing directly i will learn more and faster… So if you want i’ll be glad to see and eventually use some of your themes.

here you go, 2 of them, readily available on the fora.

##########################################################################################
## https://community.home-assistant.io/c/projects/themes
## https://community.home-assistant.io/t/dark-theme-or-night-mode-theme/446/56
##########################################################################################


darkred:
# Main colors that can be changed
  dark-primary-color: "#c66900"
  disabled-text-color: "#545454"
  divider-color: "rgba(255, 255, 255, 0.12)"
  light-primary-color: "#e06c6c"
  paper-card-background-color: "#1d1d1d"
  paper-grey-200: "#191919"
  paper-item-icon-color: "#d3d3d3"
  paper-listbox-background-color: "#202020"
  primary-background-color: "#303030"
  primary-color: "#d32f2f"
  primary-text-color: "#cfcfcf"
  secondary-background-color: "#131313"
  sidebar-text_-_background: "#62717b"
# Colors based on variables, see above
  paper-card-header-color: "var(--paper-item-icon-color)"
  paper-item-icon-active-color: "var(--primary-color)"
  paper-item-icon_-_color: "var(--primary-text-color)"
  paper-listbox-color: "var(--primary-text-color)"
  paper-grey-50: "var(--primary-text-color)"
  paper-slider-active-color: "var(--primary-color)"
  paper-slider-knob-color: "var(--primary-color)"
  paper-slider-knob-start-color: "var(--primary-color)"
  paper-slider-pin-color: "var(--primary-color)"
  paper-slider-secondary-color: "var(--light-primary-color)"
  paper-toggle-button-checked-ink-color: "var(--dark-primary-color)"
  paper-toggle-button-checked-button-color: "var(--primary-color)"
  paper-toggle-button-checked-bar-color: "var(--light-primary-color)"
  paper-toggle-button-unchecked-bar-color: "var(--primary-text-color)"
  secondary-text-color: "var(--primary-color)"
  table-row-background-color: "var(--paper-card-background-color)"
  table-row-alternative-background-color: "var(--sidebar-text_-_background)"

matrix:
  name: 'Matrix'
  # MyVariables
  base-hue: '120' #Controls the base (and accent) color hue (0-360) | 0=Red 60=Yellow 120=Green 180=Cyan 240=Blue 300=Magenta 360=Red
  base-sat: '16%' #Controls the saturation of the theme (0%-100%) | 0%=Grey 100%=Full Saturation
  # MyVar
  huesat: 'var(--base-hue), var(--base-sat),'
  # Primary Color
  primary-color: 'hsl(var(--huesat) 20%)'
  # Backgrounds
  primary-background-color: 'var(--primary-color)'
  secondary-background-color: 'hsl(var(--huesat) 16%)'
  paper-listbox-background-color: 'var(--primary-color)'
  paper-card-background-color: 'hsl(var(--huesat) 12%)'
  paper-dialog-background-color: 'var(--paper-card-background-color)'
  table-row-background-color: 'hsl(var(--huesat) 12%)'
  table-row-alternative-background-color: 'hsl(var(--huesat) 10%)'
  # Devider
  divider-color: 'hsla(0, 0%, 0%, 0)'
  dark-divider-opacity: '0'
  light-divider-opacity: '0'
  # Text colors
  primary-text-color: 'hsl(var(--huesat) 60%)'
  text-primary-color: 'hsl(var(--huesat) 60%)'
  secondary-text-color: 'hsl(var(--huesat) 60%)'
  disabled-text-color: 'hsl(var(--huesat) 70%)'
  sidebar-text_-_color: 'hsl(var(--huesat) 90%)'
  sidebar-text-color: 'hsl(var(--huesat) 90%)'
  paper-card-header-color: 'hsl(var(--base-hue), 90%, 50%)'
  # Text Adjustments
  paper-font-headline_-_letter-spacing: '-0.5px'
  paper-font-headline_-_font-weight: '500'
  paper-font-body1_-_font-weight: '500'
  # Nav Menu
  paper-listbox-color: 'hsl(var(--huesat) 50%)'
  paper-grey-50: 'hsl(var(--huesat) 50%)'
  paper-grey-200: 'hsla(var(--huesat) 26%)'
  # Paper card
  paper-item-icon-color: 'hsl(var(--huesat) 30%)'
  paper-item-icon-active-color: 'var(--paper-item-icon-color)'
  paper-item-icon_-_color: 'var(--paper-item-icon-color)'
  paper-item-selected_-_background-color: 'hsla(0, 0%, 0%, 0.2)'
  paper-tabs-selection-bar-color: 'hsla(0, 0%, 0%, 0.2)'
  # Labels
  label-badge-red: 'hsla(0, 0%, 0%, 0)'
  label-badge-border-color: 'var(--label-badge-red)'
  label-badge-background-color: 'var(--paper-card-background-color)'
  label-badge-text-color: 'var(--primary-text-color)'
  # Shadows
  shadow-elevation-2dp_-_box-shadow: 'inset 0px 0px 0px 4px hsl(var(--huesat) 18%)'
  shadow-elevation-16dp_-_box-shadow: 'inset 0px 0px 0px 4px hsl(var(--huesat) 28%)'
  # Switches
  paper-toggle-button-checked-button-color: 'hsl(var(--base-hue), 90%, 50%)'
  paper-toggle-button-checked-bar-color: 'hsl(var(--huesat) 25%)'
  paper-toggle-button-unchecked-button-color: 'hsl(var(--huesat) 25%)'
  paper-toggle-button-unchecked-bar-color: 'hsl(var(--huesat) 5%)'
  # Sliders
  paper-slider-knob-color:      'hsl(var(--base-hue), 90%, 50%)'
  paper-slider-knob-start-color: 'hsl(var(--base-hue), 80%, 25%)'
  paper-slider-pin-color:      'hsl(var(--base-hue), 90%, 50%)'
  paper-slider-active-color:      'hsl(var(--base-hue), 90%, 50%)'
  paper-slider-container-color: 'hsl(var(--huesat) 28%)'
  paper-slider-secondary-color: 'hsl(var(--huesat) 90%)'
  paper-slider-disabled-active-color: 'hsl(var(--base-hue), 80%, 25%)'
  paper-slider-disabled-secondary-color: 'hsl(var(--base-hue), 80%, 25%)'
  paper-dialog-color: 'hsl(var(--base-hue), 20%, 80%)'
2 Likes