Switch template entities not showing up

Hello,

I’m quite new to home assistant and still learning.

I am trying to get my harmony activities into the UI with switches and also so that I then can start the ps4 or other devices with the activities and integratet this all into homekit. Read this post and did everything accordingly but somehow the switch entities just won’t show up.

Here’s my configuration.yaml

switch:
  - platform: mystrom
    host: 192.168.1.X
    
  - platform: template
    switches: !include switches.yaml

The myStrom switch works fine.

switch.yaml

# Heimkino HARMONY ACTIVITY
heimkino:
  value_template: "{{ is_state_attr('remote.harmony', 'current_activity', 'Heimkino') }}"
  turn_on:
    service: remote.turn_on
    data:
      entity_id: remote.harmony
      activity: 'Heimkino'
  turn_off:
    service: remote.turn_off
    data:
      entity_id: remote.harmony
      

# Netflix HARMONY ACTIVITY
netflix:
  value_template: "{{ is_state_attr('remote.harmony', 'current_activity', 'Netflix') }}"
  turn_on:
    service: remote.turn_on
    data:
      entity_id: remote.harmony
      activity: 'Netflix'
  turn_off:
    service: remote.turn_off
    data:
      entity_id: remote.harmony
       
      
ps4:
  value_template:
  turn_on:
    service: media_player.playstation_4
    data:
     entity_id: playstation_4
  turn_off:
    service: media_player.playstation_4
    data:
     entity_id: playstation_4


It should creat an entity right? and should then show up here so I can choose it.



What am I missing? Why are those switch entities not showing up?

I would appreciate any help :slight_smile:

Best

Noah

That’s the wrong syntax for using !include.

More info here :

Splitting up the configuration - Home Assistant

Thanks for your reply.
So I read the documentation. And updated my config. At least I can’t see a mistake but still not showing up.

configuration.yaml

switch: !include switches.yaml

switches.yaml

- platform: mystrom
  host: 192.168.1.X
    
- platform: template
  switches: 
  
  # Heimkino HARMONY ACTIVITY
    heimkino:
      value_template: "{{ is_state_attr('remote.harmony', 'current_activity', 'Heimkino') }}"
      turn_on:
        service: remote.turn_on
        data:
          entity_id: remote.harmony
          activity: 'Heimkino'
      turn_off:
        service: remote.turn_off
        data:
          entity_id: remote.harmony
          
    
    # Netflix HARMONY ACTIVITY
    netflix:
      value_template: "{{ is_state_attr('remote.harmony', 'current_activity', 'Netflix') }}"
      turn_on:
        service: remote.turn_on
        data:
          entity_id: remote.harmony
          activity: 'Netflix'
      turn_off:
        service: remote.turn_off
        data:
          entity_id: remote.harmony
           
          
    ps4:
      value_template: "{{ is_state('media_player.playstation_4', 'on') }}"
      turn_on:
        service: media_player.playstation_4
        data:
          entity_id: playstation_4
      turn_off:
        service: media_player.playstation_4
        data:
          entity_id: playstation_4
    

If I check the configuration it seems to be fine.
What am I doing wrong?

Have you checked the dev tools -> states page to make sure that they’re not just showing in the UI ?

Yes I always do that. Not there.

Add two extra spaces before everything, and repeat the template

  - platform: template
    switches: 
    # Heimkino HARMONY ACTIVITY
      heimkino:
        value_template: "{{ is_state_attr('remote.harmony', 'current_activity', 'Heimkino') }}"
        turn_on:
          service: remote.turn_on
          data:
            entity_id: remote.harmony
            activity: 'Heimkino'
        turn_off:
          service: remote.turn_off
          data:
            entity_id: remote.harmony
          
  - platform: template
    switches: 
      # Netflix HARMONY ACTIVITY
      netflix:
        value_template: "{{ is_state_attr('remote.harmony', 'current_activity', 'Netflix') }}"
        turn_on:
          service: remote.turn_on
          data:
            entity_id: remote.harmony
            activity: 'Netflix'
        turn_off:
          service: remote.turn_off
          data:
            entity_id: remote.harmony
           
  - platform: template
    switches:          
      ps4:
        value_template: "{{ is_state('media_player.playstation_4', 'on') }}"
        turn_on:
          service: media_player.playstation_4
          data:
            entity_id: playstation_4
        turn_off:
          service: media_player.playstation_4
          data:
            entity_id: playstation_4
    

Fantastic! Now it’s working. Thank you!

Well know I know how important every single space is.

That’s bizarre !
I just checked my old configs for reference and they don’t include extra spaces, and for reference, neither do the examples given in the docs linked above.

Can’t argue with what works though !!

Also, that being the issue, it SHOULD have failed when Config was tested and pointed to the error.

Yes they don’t write it in the documentation.
That’s what I thought but the config test said everything is fine.
Maybe a bug. I’m running Version 0.99.2

I just tried the switches but they are not working :frowning:

It is really quite normal. When you define your switches straight in configuration.yaml, you would use :

switch:
  - platform: template
    switches: 

If you use !include, its like you do a ‘cut and paste’, so the extra spaces need to be there.

makes sense.

So if I turn on my new created harmony switches they just jump back and turn off again and the activity in harmony is not starting. So some sort of configuration error in the switches.yaml

I checked and if I call the service via dev tools. That works fine.

What can else can I check to figure out where the problem is?

Also the PlayStation Switch turns on/off but gives me an error:

Fehler beim Aufrufen des Service switch/turn_on. Unable to find service media_player/playstation_4

OR

Fehler beim Aufrufen des Service switch/turn_off. Unable to find service media_player/playstation_4

I also checked in the dev tools if it works and there it turns the ps4 on/off.

So here’s my switches.yaml

    - platform: template
    switches: 
  
    # Heimkino HARMONY ACTIVITY
      heimkino:
        value_template: "{{ is_state_attr('remote.harmony_hub', 'current_activity', 'Heimkino') }}"
        turn_on:
          service: remote.turn_on
          data:
            entity_id: remote.harmony_hub
            activity: "Heimkino"
        turn_off:
          service: remote.turn_off
          data:
            entity_id: remote.harmony_hub
          
    
    # Netflix HARMONY ACTIVITY
      netflix:
        value_template: "{{ is_state_attr('remote.harmony_hub', 'current_activity', 'Netflix') }}"
        turn_on:
          service: remote.turn_on
          data:
            entity_id: remote.harmony_hub
            activity: "Netflix"
        turn_off:
          service: remote.turn_off
          data:
            entity_id: remote.harmony_hub
           
     # PS4     
      ps4:
        value_template: "{{ is_state('media_player.playstation_4', 'on') }}"
        turn_on:
          service: media_player.turn_on
          data:
            entity_id: media_player.playstation_4
        turn_off:
          service: media_player.turn_off
          data:
            entity_id: media_player.playstation_4

I’m no expert on templates, but isn’t your value template asking if the current activity is already true? I.e. if the current activity state is “Netflix”, then turn on “Netflix”. The logic sounds backwards to me.

As far as I understood value_template is what tells the switch when it‘s on.

Docu:

value_template

(template)(Required)

Defines a template to set the state of the switch.

My understanding is that is_state_attr() is testing the state of the attribute, not setting it.

that’s clear

Docu:

  • is_state_attr('device_tracker.paulus', 'battery', 40) will test if the given entity attribute is the specified state (in this case, a numeric value).

My understanding is, that if the current activity on the harmony hub is heimkino and set the display in the UI to true and idk if it runs turn_on but i don’t think so. But I got it running. If somebody knows that it’s wrong and how I would have to do it correct please let me know. but as long it runs I’m fine with it :slight_smile:

Like this:

heimkino:
        value_template: "{{ is_state_attr('remote.harmony_hub', 'current_activity', 'Heimkino') }}"
        friendly_name: "Heimkino"
        turn_on:
          service: remote.turn_on
          data:
            entity_id: remote.harmony_hub
            activity: "Heimkino"
        turn_off:
          service: remote.turn_off
          data:
            entity_id: remote.harmony_hub
            activity: "PowerOff"

You are effectively agreeing with me :slight_smile:
Your setup is testing if the current activity is “Heimkino”. The returned value may be TRUE or FALSE. But you are not using that result as a condition to execute the turn-on sequence. If you change the current_value attribute in the value_template to be something else. e.g. “Fred_Flintstone”, I’ll bet it has no effect on the turn_on and turn_off actions.

Well yes we agree on what it does. But in my opinion it does what it should do, set the state of the switch. If I would change as you suggested as far as I understand it, the turn_on and turn_off would still work but the switch wouldn‘t display that correctly and wouldn’t be on when the activity on the harmony is on. Right? Or for what would you then use current_value for?
Correct me if I‘m wrong.

Yes, I agree with you now that I’ve looked further into it. Do the switches stay on for you?

yes works fine. I’m currently at another problem of mine where automations are slow and the automation triggers after 30 seconds…