String manipulation for input_text

I am trying automate a change of scene from “away” variant to “home” variant.

I want to do this by using a text_input as argumanet in a scene.turn_on. It is called “input_text.active_scene”, and I just want to add “_away” to the input_text and use it as argument in scene.turn_on.

And I want to do the opposite when going from away to home.
Simply remove the last 5 characters in “input_text.active_scene” (remove “_away”) and use the result as a argument in scene.turn_on.

Any suggestions?

Why text input? So you want to type the name of the scene you want to run?

I am using the inut_text in order to show the currect active scene in the front end as well, and it has the same name as the scene in the code. My thing was to reuse that in this automation.

So it displays the entity_id or the friendly name?

It contains the entity_id

And I’m going to assume you are using a device_tracker for the automation, i.e. the home/away? Do you have that entity_id?

No, it is actually a boolean, also for front end and simulation purposes:

- alias: "Set home"
  trigger:
    - platform: state
      entity_id: alarm_control_panel.visonic_alarm_2
      to: 'armed_home'
    - platform: state
      entity_id: alarm_control_panel.visonic_alarm_2
      to: 'disarmed'
  action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.away_mode
      
- alias: "Set away"
  trigger:
    platform: state
    entity_id: alarm_control_panel.visonic_alarm_2
    to: 'armed_away'
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.away_mode

It is in theese two automations I would like to add more actions to switch scene between “home” and “away” varaints.

Ok good to know.

So, use the input_boolean to trigger, don’t even bother adding anything to the text box, just leave it as is.

- alias: Away / Home Scene Automation
  trigger:
    - platform: state
      entity_id: input_boolean.away_mode
  condition:
    - condition: template
      value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
  action:
    - service: scene.turn_on
      data_template:
        entity_id: >
          {% set scene = states('input_text.active_scene') %}
          {% if scene.endswith('_away') or scene.endswith('_home') %}
            {% set scene = scene[:-5] %}
          {% endif %}
          {% if trigger.to_state.state == 'on' %}
            {{ scene }}_home
          {% else %}
            {{ scene }}_away
          {% endif %}

WoW, thanks! I would never had solved this on my own. I am just a happy and stubborn amatuer.
If I also would like to update my input_text with the new value from {{ scen }} in the same automation, would that be possible with somethibng like this as a second serivce in the automation?

   - service: input_text.set_value
  entity_id: input_text.active_scene
  data_template:
    value: >-
      {% set scene = states('input_text.active_scene') %}
      {% if scene.endswith('_away') or scene.endswith('_home') %}
        {% set scene = scene[:-5] %}
      {% endif %}
      {% if trigger.to_state.state == 'off' %}
        {{ scene }}
      {% else %}
        {{ scene }}_away
      {% endif %}

I mean your scene should update already. You said that it always displays the current scene that’s active?

Yea, but in a very bad way.
I update it after each setting of a new scene, can probably de done better…
Each automation for setting of new scene ends with…

- service: input_text.set_value
  entity_id: input_text.active_scene
  data_template:
    value: >-
      {% if is_state('input_boolean.away_mode', 'on') %}
        scene.house_nights_away
      {% else %}
        scene.house_nights
      {% endif %}

Can you post all your automations for setting scenes?

I will not post all, but one should be enought for you to see how bad my programming skills are : )
I have another boolean as well for vacation mode, that make all weekdays act like week ends. I also add a random delay when turning off lights in away mode.

- alias: "Turn off all lights on week ends"
  trigger:
    platform: time
    at: '02:00:00'
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: 'binary_sensor.workday_today'
        state: 'off'
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: 'on'
  action:
    - delay: >
        {% if is_state('input_boolean.away_mode', 'off') %}
          00:00:00
        {% else %}
          {{ range(1*60,20*60+1)|random|timestamp_custom("%H:%M:%S",False) }}
        {% endif %}
    - service: scene.turn_on
      data_template:
        entity_id: >-
          {% if is_state('input_boolean.away_mode', 'on') %}
            scene.house_nights_away
          {% else %}
            scene.house_nights
          {% endif %}
    - service: input_text.set_value
      entity_id: input_text.active_scene
      data_template:
        value: >-
          {% if is_state('input_boolean.away_mode', 'on') %}
            scene.house_nights_away
          {% else %}
            scene.house_nights
          {% endif %}

After adding your solution and my addition, it actually works as intended. : )

You should just be able to remove that last section of your automation. Then use this automation:

- alias: Post Last Scene
  trigger:
    - platform: event
      event_type: call_service
      event_data:
        domain: scene
        service: turn_on
  action:
    - service: input_text.set_value
      data_template:
        value: "{{ trigger.event.data.entity_id }}"
1 Like

I knew that there was a simpler and less bulky way of writing that code. : )
Thanks alot for the great help and optimizing of my code!

try it out… I don’t know if it works. It should work though.

Actually it dont, I added the entity id,

- alias: Post Last Scene
  trigger:
    - platform: event
      event_type: call_service
      event_data:
        domain: scene
        service: turn_on
  action:
    - service: input_text.set_value
      entity_id: input_text.active_scene
      data_template:
        value: "{{ trigger.event.data.entity_id }}"

it sets an empty string to input_text

whats the event data look like in the logs?