Set state without communicating with device?

Cool idea. Pun intended. Do you have one you recommend. It should be light and of course integrate with HA well. I am finding a lot of my zwave devices I am porting over from my old system don’t work 100% on HA.

Hello everyone! I was following this thread trying to find a solution for a similar problem that I have, that I hope any of you can help me to solve it. Given this post is from Aug, maybe any of you already found a solution. Believe me that I have searched a lot in here and took lot of examples from this community but I could’t make it work. Maybe because it is impossible.
Basically what I want to do is synchronising a Broadlink Switch, which we know it is stateless, with my harmony device.
So, the switch turns ON and OFF my tv through Broadlink hub. And in the other hand I have my harmony that also turns on my TV with a select_option.
Therefore I want to change the Broadlink switch state from OFF to ON and from OFF to ON, when I turned ON the TV using Harmony. Why? Basically sometimes Harmony gets down so using Broadlink I can count with backup method for handling the TV.
In the following example, the switch is not only changing the state, it is also sending the IR frequency to the TV. I just want to change its state.

### Start Harmony Activity
- alias: Start Harmony Activity
  trigger:
   - platform: state
     entity_id: input_select.harmony
  action:
   - service: remote.turn_on
     entity_id: remote.harmony_hub
     data_template:
       activity: >
         {% if is_state("input_select.harmony", "Watch Tv") %}
           35077645
         {% elif is_state("input_select.harmony", "Watch Netflix") %}
           35078071
         {% else %}
         {% endif %}

Then the event that changes the input_select

- alias: Update Harmony input select
  trigger:
   - platform: state
     entity_id: remote.harmony_hub
  action:
  - service: input_select.select_option
    data_template:
      entity_id: input_select.harmony
      option: "{{ states.remote.harmony_hub.attributes.current_activity }}"

Here below is what I’m trying. Basically capturing the input value and if its watch tv, I wanted to change the status of the switchtv without sending the command.
I know this script fails and it is because the service is incorrect. Do anyone know a way to do it? Is there a service that allows me to change the state of a switch?
Thank you a lot!

- alias: Harmony Changed State
  trigger:
   - platform: state
     entity_id: input_select.harmony
  action:
    service: homeassistant.turn_on
    data_template:
      entity_id: switch.tv
      state: >
       {% if is_state("input_select.harmony", "Watch TV") %}
         on
       {% else %}
       {% endif %}

It would be really nice if there was a differentiation between an device state and a command which gets sent to/from the device. This would actually alllow to trigger specificly on the value returned from the device and not trigger on all values which are sent to a device in a rule.

Check my post on this forum about television ping template sensor switch

Use a template switch for the tv where the state equals the bolean input
Create a Boolean input to track the state.
If you want to change the state without sending an ir command then toggle the Boolean input.
If you want to change the state and send it command then toggle the template switch.

The template switch on and off command send the ir signal and toggle the input Boolean.

Alternatively download the sure app for Android and find your discrete ir codes for on and off

All right! I will check it. Thank you.

Hi everyone. I’m new to HA and I’m struggling with the same problem here. I’m trying to change the state of the switch without sending a command to the device. I was trying to use switch template and input boolean as you suggested but without success. I guess I’m doing something wrong here. Is it possible for you [kiwijunglist] to provide us a simple example of how to achieve what you suggested? It would be very helpful. I’m quite out of ideas here.

Thank you very much for your help.

1 Like

It would be great yes!

see my other thread with example, post a new thread with your code if not working.

Example:

switch:
  - platform: template
    switches:
      my_switch:
        friendly_name: My Switch
        value_template: states("input_boolean.switch_state")
        turn_on:
          - service: script.command_a
          - service: input_boolean.turn_on
            data:
              entity_id: input_boolean.switch_state
        turn_off:
          - service: script.command_b
          - service: input_boolean.turn_off
            data:
              entity_id: input_boolean.switch_state

and

input_boolean:
  switch_state:
    name: Status of my switch

When you turn on the switch.my_switch it runs script.command_a
When you turn off the switch.my_switch it runs script.command_b
If you want to change the state of switch.my_switch without running the script then you can change the state of the input_boolean.
You can replace “- service: script.command_a” with whatever you want the switch to do.

1 Like

Thank you very much!!!

@kiwijunglist Thanks for sharing this. I have a few questions.

  1. will “My Switch” act like it is stateless? For example if I send turn_on and then send it again will the script run both times? I would think it will which is something I have been looking for. I have a switch which runs a shell_script that turns on/off my AC. But I can also just turn it on or off manually so I need the switch to run the command even if the state is already the same. So if the switch is currently OFF and I call turn_off I still want the shell_script service to run.
  2. Not really a question but I copy pasted your exact code here and replaced -service: script.command_a
    with
    service: shell_script.turn_on_ac
    but regardless of the state of the switch the command isn’t getting run. Is the code you pasted from your system or perhaps from memory and missing something?

Thanks again, I hope I can get this working because currently I have a hack to create stateless switches using a curl command to sent the state to “unknown” but I find it a bit clunky and would like a regular stateless switch.

Yes, Myswitch will act like a hardware switch. Meaning it will process every on and every off, regardless of the state of the switch.

His code is correct. What errors are you getting in your logs?

2018-10-09 09:57:53 ERROR (MainThread) [homeassistant.components.switch.template] Received invalid switch is_on state: states("input_boolean.switch_state"). Expected: on, off, true, false

My code which is as I stated the same with only the service changed. This code is all in configuration.yaml correct?

input_boolean:
  switch_state:
    name: Status of My Switch
.
.
.
switch:
  - platform: template
    switches:
      my_switch:
        friendly_name: My Switch
        value_template: states("input_boolean.switch_state")
        turn_on:
          - service: shell_command.turn_on_heat
          - service: input_boolean.turn_on
            data:
              entity_id: input_boolean.switch_state
        turn_off:
          - service: shell_command.turn_off_heat
          - service: input_boolean.turn_off
            data:
              entity_id: input_boolean.switch_state

Here is how it looks in ui.

i think you need input_boolean.switch_state.state

Not sure where you wanted me to change this since it has no context but I assumed you mean.
Change value_template: states(“input_boolean.switch_state”)
to value_template: states(“input_boolean.switch_state.state”)

Unfortunately…same result. (Did you mean something else?)

2018-10-09 10:47:08 ERROR (MainThread) [homeassistant.components.switch.template] Received invalid switch is_on state: states("input_boolean.switch_state.state"). Expected: on, off, true, false

Found the solution on the template switch page

Needed value_template: “{{ is_state(‘input_boolean.switch_state’, ‘on’) }}”

switch:
  - platform: template
    switches:
      my_switch:
        friendly_name: My Switch
        value_template: "{{ is_state('input_boolean.switch_state', 'on') }}"
        turn_on:
          - service: shell_command.turn_on_heat
          - service: input_boolean.turn_on
            data:
              entity_id: input_boolean.switch_state
        turn_off:
          - service: shell_command.turn_off_heat
          - service: input_boolean.turn_off
            data:
              entity_id: input_boolean.switch_state

Totally works as a stateless switch. Nice!

3 Likes

Sweet. Glad you got there. I just wrote the sample code for you from memory so I forget the brackets.

It may be less clunky to use a python_script which you can call from an HA script or automation. For instance:

hass.states.set(‘switch.power_bose’,‘on’)
ampstate = hass.states.get(‘switch.power_bose’)
logger.error(ampstate)

That will do it and leave the state as “on”. If the device polls or pushes a new state, obviously it will be overwritten.

For other who come across this topic, I set the state of my otherwise stateless (assumed state) RF lights using REST commands to call the HA API. For example:

# Update light status to off
light_status_update_off:
  url: 'http://localhost:8123/api/states/light.{{ endpoint }}'
  method: POST
  headers: 
    authorization: !secret api-token-with-bearer
    content-type: 'application/json'
  payload: >-
    {
      "state": "off",
      "attributes": {
        "friendly_name": "{{ friendly_name }}",
        "assumed_state": "{{ assumed_state }}",
        "supported_features": {{ supported_features }}
      }
    }

In my experience, you have to set all of the possible attributes, even if you don’t actually want to change them. Otherwise, those attributes that you don’t set disappear until the next HA action, e.g., switching the lights on from the frontend. You can store the current state of an attribute as a template variable and then pass it on to the REST command. See here for the solution in context: Light status

2 Likes