How to trigger action when state of input_select changes, but only from local event, not API call

OK, so I’m a noob hacking my way through this and maybe I’m going about this the wrong way, but here’s what I’m trying to do:

I have a Pro Control remote and processor (basically RTI) which I use to control my lounge TV. I have set that up to make an API call to HA to change the state of an input_select entity, which shows the current activity, either SkyQ, Amazon, Netflix, Plex, or Off.

That is all working OK.

What I want to do now is have the ability to send the command the other way on selecting an option in the input_select box in Lovelace. I have set up URLs that I can call on the RTI processor that trigger the correct macros - these work in a browser.

The issue I’m trying to overcome is not to trigger the input_select action when the state is changed by the remote sending the API call to update the current activity,

Reading through the reference materials I thought I could specify only to act on local events, but it doesn’t seem to work.

Here’s what’s in my configuration.yaml:

rest_command:
  living_room_skyq:
    url: http://192.168.10.182:8080/livingroomskyq
  living_room_amazon:
    url: http://192.168.10.182:8080/livingroomamazon
  living_room_netflix:
    url: http://192.168.10.182:8080/livingroomnetflix
  living_room_plex:
    url: http://192.168.10.182:8080/livingroomplex
  living_room_off:
    url: http://192.168.10.182:8080/livingroomoff

Here’s whats in my automations.yaml:

- id: '1618527999000'
  alias: Local select living room TV activity
  description: ''  
  trigger:
  - platform: event
    event_type: state_changed
    origin: LOCAL
    entity_id: input_select.living_room_tv_activity
  condition: []
  action:
    service: >
      {% if is_state('input_select.living_room_tv_activity', 'Sky Q') %} rest_command.living_room_skyq
      {% elif is_state('input_select.living_room_tv_activity', 'Amazon') %} rest_command.living_room_amazon
      {% elif is_state('input_select.living_room_tv_activity', 'Netflix') %} rest_command.living_room_netflix
      {% elif is_state('input_select.living_room_tv_activity', 'Plex') %} rest_command.living_room_plex
      {% elif is_state('input_select.living_room_tv_activity', 'Off') %} rest_command.living_room_off
      {% else %}
      {% endif %}

The config won’t load because it doesn’t like the origin: LOCAL statement. Error I get is:

Logger: homeassistant.config
Source: config.py:454
First occurred: 15:53:46 (1 occurrences)
Last logged: 15:53:46

Invalid config for [automation]: [origin] is an invalid option for [automation]. Check: automation->origin. (See /config/configuration.yaml, line 9).

Could anybody help me with this? I’m no programmer (but excellent at copy, paste and tweak) - I could be making a schoolboy error, or just barking up the wrong tree.

Thanks

What does the state_changed event look like when you trigger it locally vs with the remote? Once you know that information, you can tailor your event to only trigger off one or the other.

Thanks for your reply. How would I find out what the event looks like? I don’t seem to be able to find much detail in the logbook or system log. Could you point me to where I need to look?

Register for the event in developer tools → events page. There’s a subscribe button. Put state_changed in the text box and listen to the events. Keep in mind all state changes will show up there.

Also, the likelyhood of these being different is extremely low, so other methods might be needed instead. But we can start there. In all honesty, I think you’ll be moving to a select entity instead of input select, but I’d like to see the details before we do that.

So here’s what I got when the state got changed by the API call from the remote:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "input_text.living_room_tv",
        "old_state": {
            "entity_id": "input_text.living_room_tv",
            "state": "Plex",
            "attributes": {},
            "last_changed": "2022-02-22T17:50:31.311533+00:00",
            "last_updated": "2022-02-22T17:50:31.311533+00:00",
            "context": {
                "id": "42da32cf8e9f965dc31324031263ac32",
                "parent_id": null,
                "user_id": "68e1befe7df84d538d6bf7765dc43b6c"
            }
        },
        "new_state": {
            "entity_id": "input_text.living_room_tv",
            "state": "Sky Q",
            "attributes": {},
            "last_changed": "2022-02-22T17:51:03.758115+00:00",
            "last_updated": "2022-02-22T17:51:03.758115+00:00",
            "context": {
                "id": "5379a11afe814a18566aeb945c0b6c75",
                "parent_id": null,
                "user_id": "68e1befe7df84d538d6bf7765dc43b6c"
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2022-02-22T17:51:03.758115+00:00",
    "context": {
        "id": "5379a11afe814a18566aeb945c0b6c75",
        "parent_id": null,
        "user_id": "68e1befe7df84d538d6bf7765dc43b6c"
    }
}

Straight away I can see the origin key, but it says local. So I guess I’m not going to get this to work…

Any thoughts on how to go about this would be gratefully received.

So what is automatically setting the input number when the remote does it?

Post that please

1 Like

The remote is currently changing the state of an input_text entity, just by using a text string. I didn’t want to risk a loop if I got the input_select code wrong wrong.

That’s what you saw in the event log from the listener - the input_text entity changing.

Yes but how are you detecting the remote changing it? I get that you populate something but you’re somehow reacting to the remote signal. How are you doing that, if it’s an automation, post it

I was hoping to use the automation trigger in the first post, but because it can’t differentiate between locally pressed vs API called it’s a non starter.

But I guess some kind of state change logic is needed. I maybe need to use an intermediary entity as the status, and separate the call from the remote and the call from HA to update that status.

Not sure what the best entity to use is - the input_text entity is ugly in the interface.

You still aren’t answering my question. How does the state of your remote get into home assistant? If you don’t have an answer for that, then this whole thing is a non starter. When you physically press the remote, what changes inside home assistant?

Sorry, I misunderstood. The remote makes an API call with a JSON payload e.g.:

POST /api/states/input_text.living_room_tv HTTP/1.1 Host: 192.168.10.33 Authorization: Bearer xxxxxxxx Content-Type: application/json Content-Length: 17 {"state":"Sky Q"}

And I can change that to whatever it needs to be

Ok and then there is an automation that triggers off that? Can you post that automation

Nevermind, I see what you’re doing now that I’m on desktop

My attempt at that was in the first post. I was hoping to be able to pick the activity from the drop down and send the appropriate command back to the remote, and also if the activity was changed on the remote for it to update the active item in the list, without it then triggering another call back to the remote.

It’s how the Logitech Harmony integration does it - pick activity from a list, or active item in list is updated by the remote.

Use this template select and change your post to this:

template:
- select:
  - unique_id: remote
    name: Remote
    state: unknown
    select_option:
    - condition: "{{ option in ['Sky Q', 'Amazon', 'Netflix', 'Plex', 'Off' ] }}"
    - service: >
        {% if option == 'Sky Q' %} rest_command.living_room_skyq
        {% elif option == 'Amazon' %} rest_command.living_room_amazon
        {% elif option == 'Netflix' %} rest_command.living_room_netflix
        {% elif option == 'Plex' %} rest_command.living_room_plex
        {% elif option == 'Off' %} rest_command.living_room_off
        {% else %}
        {% endif %}
    options: >
      {{ [ 'unknown', 'Sky Q', 'Amazon', 'Netflix', 'Plex', 'Off' ] }}

You should have a select with the entity_id: select.remote.

Now, change your post to

POST /api/states/select.remote HTTP/1.1 Host: 192.168.10.33 Authorization: Bearer xxxxxxxx Content-Type: application/json Content-Length: 17 {"state":"Sky Q"}

EDIT:

The state will be overriden by your post and it will not trigger the selection of the option. The option will be executed when a user uses the select. The state of the select will update whenever you post.

Normally, you’d collect the post as a trigger and use that as a state, but updating the state your way works too. Keep in mind that after startup, it won’t know the state and will assume ‘unknown’ as the state. This is why it was added to the dropdown. You could have it assume off as well, but you’ll have to make that change yourself.

That’s great - I’ll give that a try and let you know how I get on later.

Many thanks

OK, so I’ve had a go at this. When HA starts up, I get the drop down I can choose from and it sends the correct command to trigger the remote.

HASS select active

However when I start the activity from the remote, the drop down changes to just say “remote” in the entry field, and there are no options when you click on it.

HASS select inactive

I also can’t see anything in the Event Listener, although the API call is fired.

The remote says the last command sent is:

POST /api/states/select.remote HTTP/1.1 Host: 192.168.10.33 Authorization: Bearer xxxxxxx Content-Type: application/json Content-Length: 17 {"state":"Sky Q"}	

I only changed the target in the API call. Strange that no event fires, and the select entity loses its options, and its icon as a result of a call that doesn’t even seem to get through.

Looking in the Developer Tools → States section, it is actually showing the correct state, so it must be getting through.

I’m a bit confused, but it seems almost there, but I don’t know where the issue is. There’s almost no documentation on the Select entity.

Any further help will be gratefully received.

Just to add, when the select picker starts and all looks good, once you select an option and it fires the macro on the remote, the API call comes back and the box greys out, same as when the activity is started from the remote.

So the problem seems to be with the state change command causing something weird with the Select entity.

I must have been doing something wrong in the Event Listener - I’ve now seen the event from the API call from the remote:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "select.remote",
        "old_state": {
            "entity_id": "select.remote",
            "state": "Sky Q",
            "attributes": {},
            "last_changed": "2022-02-22T20:37:14.350147+00:00",
            "last_updated": "2022-02-22T20:37:14.350147+00:00",
            "context": {
                "id": "857dc739dca9f0f9454c6d448a3e1866",
                "parent_id": null,
                "user_id": "68e1befe7df84d538d6bf7765dc43b6c"
            }
        },
        "new_state": {
            "entity_id": "select.remote",
            "state": "Amazon",
            "attributes": {},
            "last_changed": "2022-02-22T21:07:58.688469+00:00",
            "last_updated": "2022-02-22T21:07:58.688469+00:00",
            "context": {
                "id": "8520f61ffe518292db78bb2f9f91a7c3",
                "parent_id": null,
                "user_id": "68e1befe7df84d538d6bf7765dc43b6c"
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2022-02-22T21:07:58.688469+00:00",
    "context": {
        "id": "8520f61ffe518292db78bb2f9f91a7c3",
        "parent_id": null,
        "user_id": "68e1befe7df84d538d6bf7765dc43b6c"
    }
}

So that is firing, and the state is actually changing. The problem is in the display of the entity somehow.