Set state without communicating with device?

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

This very easy to do using Node Red and an Http Request Node if any one needs it .
Just use http request node with this url: http://your.hassio.ip.address:your_hassio_port_#/api/states/your.entity_id
and get a long lived token from frontend and set token type to bearer and http type to post.
Then pass the desired state to the http node with any other node you want like inject or change node with msg.payload reflecting the state desired in json format.

In this case the trigger fires all the same

Is it possible to set the state in this way and then push it into the device?
I would like to avoid that the device will override the new state.

Please explain what you are trying to do. Everything in this thread is bad practice and/or device dependent. Pushing information to devices is usually done through service calls. If you’re trying to change the state of a device and push information back to said device, then you most likely should be creating template entities. But this depends on the device in question.

Hi Petro,
thanks for your answer!

I’m new to Home Assistant and I am studying what could I do with this platform.
I am trying to develop an integration for a switch. When this switch is turned on through Home Assistant I would like to activate another switch implemented through a different integration.

That would be a service call to turn on the other switch. Or just let the users create an automation with home assistant

Hi Petro,
thank you again for your answer and sorry for my late reply.

I had a look at the services documentation, but it is not clear to me how can I use these functionalities inside an integration source code. Is it possible?
Can you give me an example or link me to the right documentation page?

Thank you for your time!

If you’re creating an integration you need to look at the developer documents, not the user documents. Developer documents are linked at the top of the page.

Sorry, I’m not going to write the code for you. This is python, you need to be able to read / understand an api if you plan on writing your own integration.

Typically users do not write their own integrations as this is the hardest thing to do, they make basic automations using the UI. If this is what your after, follow the automation documentation in the docs section at the top of the page.

You’re right, unfortunately, I was not able to find this function in developer documents.
I looked into the accessing the core section but I only found a link to the ServiceRegistry that is not very clear for me.

If you know where I can find more details about this functionality you are welcome, otherwise no problem. I appreciate the hints you have offered me so far.

The first item is the service call in what you linked… This is the api. hass.services is the ServiceRegistry, every call on that object is listed there. Unfortunately, you’ll have to read the descriptions of the methods that you want to use. Hint: The first method is aysnc_call. If you wanted to…

what would you use…

1 Like

Ok, in this way, I was able to turn on the second switch when the first one was turned on.
Thanks for your support.
I left here a snippet of code that could be useful for other readers.

def turn_on(self):
  self.attr_is_on = True

  second_switch_id = "my_second_switch"
  self.hass.services.call(
    domain="switch",
    service="turn_on",
    service_data={"entity_id": "switch."+switch_id},
  )
1 Like

For people who are not that familiar with NodeRed, APIs, “GET”, REST, “POST” commands, I suggest checking the “Set State” HACS approach shared on the links below. For me, this is by far the most straightforward approach.

Direct Github page:
https://github.com/xannor/hass_py_set_state

https://community.home-assistant.io/t/set-state-without-calling-service/429307/6