Skip to content

Instantly share code, notes, and snippets.

@pelrun
Last active January 30, 2024 08:24
Show Gist options
  • Save pelrun/91feae869aa9bfe9faa610a1fbbee9b3 to your computer and use it in GitHub Desktop.
Save pelrun/91feae869aa9bfe9faa610a1fbbee9b3 to your computer and use it in GitHub Desktop.
Home Assistant script to set an entity's state, used with the IKEA Tradfri remote control.
if 'entity_id' not in data:
logger.warning("===== entity_id is required if you want to set something.")
else:
data = data.copy()
inputEntity = data.pop('entity_id')
inputStateObject = hass.states.get(inputEntity)
if inputStateObject:
inputState = inputStateObject.state
inputAttributesObject = inputStateObject.attributes.copy()
else:
inputState = 'unknown'
inputAttributesObject = {}
if 'state' in data:
inputState = data.pop('state')
logger.debug("===== new attrs: {}".format(data))
inputAttributesObject.update(data)
hass.states.set(inputEntity, inputState, inputAttributesObject)
@letroll
Copy link

letroll commented Jul 28, 2020

thank you, it's really useful !

@pelrun
Copy link
Author

pelrun commented Jul 28, 2020

If you're actually using this for a tradfri remote, note that it's not been necessary for a long time - zigbee2mqtt improved their support for the device and it just works out of the box. If you're using it to do other state manipulations... carry on :)

@daniellandau
Copy link

daniellandau commented Nov 17, 2020

I can't believe this doesn't have more stars, perfect! Works for switches just as well.

@jaycollett
Copy link

Just saved my day mate! Very odd that HA doesn't have this as a built-in action type...

@alibenpeng
Copy link

Just saved my day mate! Very odd that HA doesn't have this as a built-in action type...

My thoughts exactly. Very odd in deed.

Well, actually my original thought was more along the lines of "WTF? Why is this even necessary? Why isn't this built-in?". But I like yours better, for it's way more polite.

@mapo31
Copy link

mapo31 commented Sep 17, 2021

Hello,

When i set this in my automation :

  - service: python_script.set_state
    data:
      entity_id: sensor.energy_spent
      state: ''

I got this error : "Message malformed: expected dictionary @ data['action'][0]"

Do you have an idea on what is happening ?

@pelrun
Copy link
Author

pelrun commented Sep 17, 2021

Are you sure "sensor.energy_spent" holds a state? It looks more like a value type to me.

@mapo31
Copy link

mapo31 commented Sep 17, 2021

yes you're right it s a value type, but when I m using set state from developer tools, setting state to 0, it clear the value, I was thinking that this script will do the same...

@mapo31
Copy link

mapo31 commented Sep 17, 2021

sorry it works when I do it like this :

service: python_script.set_state
data:
  entity_id: sensor.energy_spent
  state: '0'

thanks for your help !

@sasukebinbin
Copy link

State reverse back 5 seconds after set_state when running latest HA.

@bverkron
Copy link

bverkron commented Feb 7, 2022

State reverse back 5 seconds after set_state when running latest HA.

Is this happening with an entity that was previously holding its state for a long time after being set with this python script? It might depend on the entity you're setting the state of. Some update very frequently. I've set states in the Developer panel before and they updated and override my change within a very short period.

@sasukebinbin
Copy link

My entity is create by broadlink RF. Got Switch,Fan. It should be an offilne entity.
Previously this entity just send RF code to control switch or Fan.
The reason I use this python script, I built an RF receiver so that when I use original remote controle to send RF code I can use my RF receiver to receive the code and set the correct state for the entity.

@bverkron
Copy link

bverkron commented Feb 7, 2022

Interesting use case. If you set the state in HA under the Development panel does it revert? Testing that would either confirm it's the python script or rule it out. Also does the Log book or logs show anything?

@sasukebinbin
Copy link

Yes. I have tested under dev tool. It behave same.
And just checked logs, no any logs shows about this.
Current log level set to debug.
Please advise how should I troubleshoot this.

@danielbrunt57
Copy link

Yes. I have tested under dev tool. It behave same.

I.e If you set the state in Dev Tools it reverts automatically? In this case, nothing can be done to override the constant and continual state updating that HA/componets inherently do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment